1

I am trying to send a parameter in an AJAX call. I need to send it in a way that the receiver file can understand it as a date to be part of a mysql query.

The page receives the value as URL parameter, like this:

http://.....php?dia=2016-08-15

Then on this page I make an AJAX call to send the param as POST data, like this:

var loadDataJRZ = function(){
    $.ajax({
        type: "POST",
        data: {dia: "<?php echo $_GET['dia']?>"},

And then in the receiver file I should get the param like this: $dia = $_POST['dia'];

If I execute the receiver file directly in the browser, the output is ok. But running the web page, the output is an empty string.

This is the receiver file code:

$dia = $_POST['dia'];

$sql = "SELECT *
        FROM dbo.LoteJuarez 
        WHERE  fecha = '$dia'  
        ORDER BY fecha, hora DESC";

I guess it is a matter of variable type, then I get the value dia = 903 if I send the AJAX call without "", like this

 data: {dia: <?php echo $_GET['dia']?>},

and I get dia="2016-08-15" sending the call with ""

data: {dia: "<?php echo $_GET['dia']?>"},

Taking into account that a direct execution from the receiver file shows the query result as it should be, what should I change to make it work?

Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
mvasco
  • 4,965
  • 7
  • 59
  • 120
  • When you try to `console.log(typeof );` what do you get? – aldrin27 Aug 15 '16 at 23:59
  • Just send a JSON with the type: `{"type": "date", "value": "2016-08-15"}`. – Kevin Kopf Aug 16 '16 at 00:05
  • 1
    You are being a little coy with the amount of code you are showing. But try this as a starter for 10 `data: {dia: },` – RiggsFolly Aug 16 '16 at 00:05
  • @aldrin27, the output is NaN – mvasco Aug 16 '16 at 00:18
  • What do you see in page source code if you use `data: {dia: "=$_GET['dia']?>"},`? – skobaljic Aug 16 '16 at 00:18
  • @RiggsFolly, I have already tried with {dia: }, as you can see in my question. Thank you – mvasco Aug 16 '16 at 00:19
  • @skobaljic, the output for this is dia :"2016-08-15" – mvasco Aug 16 '16 at 00:20
  • And in Browser inspector (Net tab) what data has been posted than? If correct data has been posted, than the issue is in PHP. Sometimes PHP won't accept POST variables (only accepting form data), try to debug $_POST and check if 'dia' is there, than try to use $_REQUEST['dia']. – skobaljic Aug 16 '16 at 00:23
  • [Little Bobby](http://bobby-tables.com/) says `$dia = $_POST['dia'];` [is at risk for SQL Injection Attacks](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – [Jay Blanchard](http://stackoverflow.com/users/1011527/jay-blanchard) – Jeff Puckett Aug 16 '16 at 02:07
  • Well show a little more code and the solution may be more obvious – RiggsFolly Aug 16 '16 at 08:29

0 Answers0