使用$ .post()将值发布到PHP并使用$ .getJSON返回
我正在尝试在$ .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的:
但是test.php没有采用发布值。 那么如何发布sel?
你调用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注入攻击。