我从php得到响应,但不打印在文本框中


I am getting response from php but not printing in text box

我有两个文件,一个用于HTML和jQuery,另一个用于PHP。
我从PHP页面得到响应,但我不能在HTML文本框中显示。

下面是我的代码:
<script type="text/javascript">
function fill_amt()
{
  var sname=$("#sname").val();
  $.post("try_insert.php",{sname:sname,cmd2:"fill_amt"},function(data){
  $("#tot_amt").html(data);
  });
}
</script>

下面是我的HTML代码:

<input type="text" id="tot_amt" name="tot_amt">
这是我的PHP代码
$sql = mysql_query("select total_fee from admission where first_name='".$sname."'")or die(mysql_query());
$val=mysql_fetch_row($sql);
$tot=$val;
echo json_encode($tot);

使用说明:

$("#tot_amt").val(data);而不是$("#tot_amt").html(data);

你的JS代码应该是

<script type="text/javascript">
function fill_amt()
{
  var sname=$("#sname").val();
  $.post("try_insert.php",{sname:sname,cmd2:"fill_amt"},function(data){
     var json=JSON.parse(data); // parsing data form JSON string and convert it into object
     $("#tot_amt").val(json.total);
  });
}
</script>
PHP代码应该是
$sql = mysql_query("select total_fee from admission where first_name='".$sname."'")or die(mysql_query());
$val=mysql_fetch_assoc($sql);
$tot=$val['total'];
echo json_encode(array("total"=>$tot)); // give response as json string