使用$.Post()将值发布到PHP,并使用$.getJSON返回
我试图在$.post函数中使用$.getJSON,并在文本框(id=desc)中显示返回值。 这是我的密码:使用$.Post()将值发布到PHP,并使用$.getJSON返回,php,jquery,Php,Jquery,我试图在$.post函数中使用$.getJSON,并在文本框(id=desc)中显示返回值。 这是我的密码: function dataload(){ var currentId = $(this).attr('id'); var e = document.getElementById(""+currentId); var sel = e.options[e.selectedIndex].text; $("#"+currentId).change(functi
function dataload(){
var currentId = $(this).attr('id');
var e = document.getElementById(""+currentId);
var sel = e.options[e.selectedIndex].text;
$("#"+currentId).change(function (){
$.post("test.php", {data:sel}, function(data){
$.getJSON("test.php", function(data){
$("#desc").val(data[0].description);
});
});
});
}
test.php:
<?php
include('config.php');//db connection
$itemvalue=($_POST["data"]);
$item = mysql_query("SELECT description FROM invoice WHERE item='$itemvalue'");
$data = array();
while($row = mysql_fetch_array($item, true)) {
$data[] = $row;
}
$data = json_encode($data);
echo $data;
?>
调用test.php
两次,一次使用$.post
,一次使用$.getJSON
。在第一次调用中,POST参数应该正确地传递给您的PHP,但是您不会对请求的结果做任何事情。在第二次调用中,您不传输任何POST参数,$itemvalue
只是为空
只需使用:
$.getJSON("test.php", {data:sel}, function(data){
// [...]
在PHP中:
$itemvalue=$_GET["data"]; // $.getJSON performs a GET request
顺便说一句:您的代码容易受到SQL注入的攻击。您调用了两次test.php
,一次使用$.post
,一次使用$.getJSON
。在第一次调用中,POST参数应该正确地传递给您的PHP,但是您不会对请求的结果做任何事情。在第二次调用中,您不传输任何POST参数,$itemvalue
只是为空
只需使用:
$.getJSON("test.php", {data:sel}, function(data){
// [...]
在PHP中:
$itemvalue=$_GET["data"]; // $.getJSON performs a GET request
顺便说一句:你的代码容易受到SQL注入的攻击。该死,这正是我写的。我总是喜欢使用它,这样我就可以很容易地看到发生了什么,或者以后再修改它。该死,这正是我写的。我总是喜欢只使用它,这样我就可以很容易地看到发生了什么,或者以后更改它。