L
L
lilikon2017-02-09 23:35:53
PHP
lilikon, 2017-02-09 23:35:53

Ajax + PHPExcel not working?

First time using PHPExcel
have ajax

$(document).ready(function () {
    $('#exelTicket').submit(function (event) {
      event.preventDefault();
      var formValid = true;			
      var expolistall = $("#expolistall").val();
      var formData = new FormData();
      formData.append('expolistall', expolistall);
      $.ajax({
        type: "POST",
        url: "ajax/exelticket.php",
        data: formData,
        contentType: false,
        processData: false,
        cache: false,
        success: function (data) {
          var $data =  JSON.parse(data);
          $('#error').text('');
          if ($data.result == "success") {
            $('#exelTicket').hide();
            $('#msgExelTicket').removeClass('hidden');
          } else {
            $('#error').text('');
          }
        }
      });
    });
  });

php
<?php
define('INCLUDE_CHECK',true);
require_once('../system/mysql.php');
require_once('../system/session.php');
require_once('../system/connect.php');
$data['result']='error';
function validStringLength($string,$min,$max) {
  $length = mb_strlen($string,'UTF-8');
  if (($length<$min) || ($length>$max)) {
    return false;
  } else {
    return true;
  }
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  $data['result']='success';	
  if (isset($_POST['expolistall'])) {
    $expolistall = $_POST['expolistall'];
    if (!validStringLength($expolistall,1,500)) {
      $data['expolistall']='Поля имя содержит недопустимое количество символов.';   
      $data['result']='error';     
    }
  } else {
    $data['result']='error';
  }
}
else
{
  $data['result']='error';
}
if ($data['result']=='success') {
  //тут данные из базы, пока тестовый запуск так что их нет
  
  require_once('../Classes/PHPExcel.php');
  $objPHPExcel = new PHPExcel();	
  $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', 'Hello')
        ->setCellValue('B2', 'world!')
        ->setCellValue('C1', 'Hello')
        ->setCellValue('D2', 'world!');				
  $objPHPExcel->getActiveSheet()->setTitle('Название страницы');	
  $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  $objWriter->save('php://output');
  $data['result']='success';
}
echo json_encode($data);
?>


When trying to execute, it displays strange characters in the logs.
If you just pull the PHPExcel code into a separate file, it works, creates a file and immediately downloads it by the browser.
How to make it work?
Apologies in advance for my crooked code, this is my first time using PHPExcel

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Immortal_pony, 2017-02-10
@lilikon

stackoverflow.com/a/36412422/4287929

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question