I'm trying to use $.getJSON inside $.post function and display returned value in textbox (id=desc). Here is my code:
我正在尝试在$ .post函数中使用$ .getJSON并在textbox中显示返回值(id = desc)。这是我的代码:
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;
?>
but test.php is not taking posted value. So how to post sel?
但是test.php没有采用发布值。那么如何发布sel?
1 个解决方案
#1
4
You call test.php
twice, once using $.post
and once using $.getJSON
. On the first call, the POST param should be passed correctly to your PHP - but you don't do anything with the result of the request. On the second call, you don't transmit any POST param and $itemvalue
just is empty.
你调用test.php两次,一次使用$ .post,一次使用$ .getJSON。在第一次调用时,POST参数应该正确传递给您的PHP - 但是您不会对请求的结果做任何事情。在第二次调用时,您不传输任何POST参数,$ itemvalue只是为空。
Just use:
$.getJSON("test.php", {data:sel}, function(data){
// [...]
and in your PHP:
在你的PHP中:
$itemvalue=$_GET["data"]; // $.getJSON performs a GET request
btw: Your code is vulnerable to SQL injections.
顺便说一句:您的代码容易受到SQL注入攻击。
#1
4
You call test.php
twice, once using $.post
and once using $.getJSON
. On the first call, the POST param should be passed correctly to your PHP - but you don't do anything with the result of the request. On the second call, you don't transmit any POST param and $itemvalue
just is empty.
你调用test.php两次,一次使用$ .post,一次使用$ .getJSON。在第一次调用时,POST参数应该正确传递给您的PHP - 但是您不会对请求的结果做任何事情。在第二次调用时,您不传输任何POST参数,$ itemvalue只是为空。
Just use:
$.getJSON("test.php", {data:sel}, function(data){
// [...]
and in your PHP:
在你的PHP中:
$itemvalue=$_GET["data"]; // $.getJSON performs a GET request
btw: Your code is vulnerable to SQL injections.
顺便说一句:您的代码容易受到SQL注入攻击。