简体   繁体   中英

how i get the value of a post variable, send by ajax in php

i send with ajax a id to a php side. The php side should answer with a MySql row with this ID.

ajax:

$.ajax({
dataType : 'jsonp',
type: "POST",
async: false,
traditional: true,
jsonpCallback: 'jsonCallback',
contentType: "application/json",
data : {
'ID' : '2'
},
url : 'http://myside.de/sqlExecute.php',
success : function(sqlArray) {
console.log("Eingange " +sqlArray.ID);
console.log("Eingange " +sqlArray.Name);
},
error: function(a){
alert("error");
}

});

php:

<?php
include('DatabaseConnector.php');
$array = toString('SELECT * FROM `RMap` WHERE `ID` ='+$_POST["ID"]);
$json = json_encode($array);
print $_GET['callback'] . "(" . $json . ")"
?>

this works:

<?php
include('DatabaseConnector.php');
$array = toString('SELECT * FROM `RMap` WHERE `ID` =2');
$json = json_encode($array);
print $_GET['callback'] . "(" . $json . ")"
?>

what is wrong on the $_Post variant?

Thanks

UPDATE

Now i get the error message "200 Error: jsonCallback was not called", with this code:

 <?php
 include('DatabaseConnector.php');
 $array = toString('SELECT * FROM `RMap` WHERE `ID` ='.$_POST["ID"]);
 $json = json_encode($array);
 print $_GET['callback'] . "(" . $json . ")"
 ?>

Your code should be like

$array = toString('SELECT * FROM `RMap` WHERE `ID` =' . $_POST["ID"]);

In php you concat with . and in javascript you do with +

使用点(。)进行连接,而不是+

$array = toString('SELECT * FROM `RMap` WHERE `ID` ='.$_POST["ID"]);
'SELECT * FROM `RMap` WHERE `ID` ='+$_POST["ID"]

In PHP, you concatenate strings with a . (dot) instead of a + (plus sign).

ie Your code should look like:

$array = toString('SELECT * FROM `RMap` WHERE `ID` =' . $_POST["ID"]);

When you send data by ajax check in firebug console : Response you will get ID is Undefined .

If ID is undefined then how your query will works ?

So first of all need to solve that undefined error and then check your query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM