0

I have ajax call to PHP function as :

$.ajax({

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

  });

PHP function as:

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();
    }

Here when i supply dates to this function, Its not getting dates.

But when i write dates hard-code in php function as:

$txtFromOrderDate='2014-08-01'

$txtToOrderDate='2014-08-05'

Then further function works as expected.

what can be the issue???

Please help me.

2
  • $txtFromOrderDate = $_POST['txtFromOrderDate']; etc ... when you POST to PHP, those variables are not set as globals, but are set in the $_POST array. Commented Oct 27, 2014 at 9:28
  • Show us the code where you supply the method with the POST variables. Also, the query does not appear to be safe. Someone can send a malicious date (i.e. SQL code) to the script and do some nasty stuff. You should sanitize the input before using it. Commented Oct 27, 2014 at 9:31

1 Answer 1

1

When you POST to PHP (via AJAX in your case), those data variables are not set as globals. They are set in the $_POST array.

You can use them directly or set them to your global variables (just ensure you check they exist before-hand).

if (isset($_POST['youVariable')) {
    $yourVariable = $_POST['yourVariable'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I was new with PHP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.