PHP函数没有通过ajax调用

时间:2014-10-27 09:24:44

标签: javascript php jquery ajax

我对PHP函数进行了ajax调用:

$.ajax({

  url: 'Link',
  type: 'POST',
  dataType : 'json',
  data: {'txtFromOrderDate' : '2014-08-01','txtToOrderDate' : '2014-08-05'},
  success: function() {
  window.location = 'Link';
  }

  });

PHP函数:

public function createZipAction($txtFromOrderDate,$txtToOrderDate)
    {


    date_default_timezone_set('Australia/Melbourne');
    $date = date('m:d:Y H:i:s', time());



    $exportBatch = $date; 

    $order = $this->getTableGateway('order');

    $select = new Select();
    $select->from('order')
        ->join('user', 'order.user_id = user.id', array('email'))

         ->where ("order.created between ".$txtFromOrderDate." and '2014-08-03' ");
         //->where ('order.created between '.$txtFromOrderDate.'  and '.$txtToOrderDate);


    $data = $order->selectWith($select)->toArray();

    $batchDir = __DIR__ . '/../../../../../data/export/batch/' . $exportBatch;
    if(is_dir($batchDir) == false)
    mkdir($batchDir);

    $csvFile = fopen($batchDir . '/order.csv', 'w');

    $i = 0;
    foreach($data as $record) {
        if($i==0) fputcsv($csvFile, $this->getCsvHeader($record));
        fputcsv($csvFile, $this->updateCsvLine($record));
        $pngTmpFile = $this->saveTmpImage($record['plate_id']);
        $this->savePlatePdf($pngTmpFile, $exportBatch, $record['id']);

        unlink($pngTmpFile);
        $i++;
    }

    fclose($csvFile);

    $filter = new \Zend\Filter\Compress(array(
        'adapter' => 'Zip',
        'options' => array(
            'archive' => $batchDir . '.zip'
        )
    ));

    $filter->filter($batchDir);

    $fileToDownload=$batchDir . '.zip';

    $this->downloadOrderCSVAction($fileToDownload);

    echo "exported: $i records.";
    die();
    }

这里当我为这个函数提供日期时,它没有得到日期。

但是当我在php函数中写日期硬编码为:

$txtFromOrderDate='2014-08-01'

$txtToOrderDate='2014-08-05'

然后进一步的功能按预期工作。

可能是什么问题???

请帮帮我。

1 个答案:

答案 0 :(得分:1)

当您发布到PHP(在您的情况下通过AJAX)时,这些数据变量不会设置为全局变量。它们设置在$_POST数组中。

您可以直接使用它们或将它们设置为全局变量(只需确保先检查它们是否存在)。

if (isset($_POST['youVariable')) {
    $yourVariable = $_POST['yourVariable'];
}