使用$ .post()将值发布到PHP并使用$ .getJSON返回 [英] Post value to PHP using $.post() and return with $.getJSON
问题描述
我正在尝试在$ .post函数中使用$ .getJSON并在textbox(id = desc)中显示返回值。
这是我的代码:
I'm trying to use $.getJSON inside $.post function and display returned value in textbox (id=desc). Here is my code:
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没有取值。那么如何发布sel?
but test.php is not taking posted value. So how to post sel?
推荐答案
你打电话给 test.php
两次,一旦使用 $。发布
,一次使用 $。getJSON
。在第一次调用时,POST参数应该正确传递给您的PHP - 但您不会对请求的结果执行任何操作。在第二次调用时,您不传输任何POST参数, $ itemvalue
只是为空。
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.
只需使用:
$.getJSON("test.php", {data:sel}, function(data){
// [...]
和你的PHP:
$itemvalue=$_GET["data"]; // $.getJSON performs a GET request
btw:您的代码容易受到SQL注入攻击。
btw: Your code is vulnerable to SQL injections.
这篇关于使用$ .post()将值发布到PHP并使用$ .getJSON返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!