[英]how i get the value of a post variable, send by ajax in php
i send with ajax a id to a php side. 我用ajax发送一个id到php端。 The php side should answer with a MySql row with this ID. php端应该使用此ID回答MySql行。
ajax: 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:
<?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? $ _Post变体出了什么问题?
Thanks 谢谢
Now i get the error message "200 Error: jsonCallback was not called", with this code: 现在我收到错误消息“200错误:jsonCallback未被调用”,使用以下代码:
<?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 .
在PHP中 concat
使用.
and in javascript you do with +
并在javascript中你做+
使用点(。)进行连接,而不是+
$array = toString('SELECT * FROM `RMap` WHERE `ID` ='.$_POST["ID"]);
'SELECT * FROM `RMap` WHERE `ID` ='+$_POST["ID"]
In PHP, you concatenate strings with a .
在PHP中,您使用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
. 当您通过ajax发送数据时检查firebug console
:响应您将获得ID
Undefined
。
If ID
is undefined then how your query will works ? 如果ID
未定义,那么您的查询将如何运作?
So first of all need to solve that undefined error and then check your query. 因此,首先需要解决该未定义的错误,然后检查您的查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.