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;
?>
but test.php is not taking posted value. So how to post sel?
You call
test.phptwice, once using$.postand 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$itemvaluejust is empty.Just use:
and in your PHP:
btw: Your code is vulnerable to SQL injections.