[英]How do I populate second textbox based on the value entered in first textbox using jquery?
I am trying to populate the text fields based on the value entered in the first textbox field from the database but I am not getting any response when I enter some value into the first textbox field. 我试图根据在数据库中第一个文本框字段中输入的值填充文本字段,但是当我在第一个文本框字段中输入一些值时我没有得到任何响应。 Please check the code 请检查代码
Javascript: Javascript:
<script type="text/javascript">
$("#tin").blur(function () {
$.post("tin_handle.php",
{
tin: $(this).val()
},
function (data){
$("#cname").val(data.cname);
$("#caddress").val(data.caddress);
});
</script>
Tin_handle.php: Tin_handle.php:
<?php
$tn = trim($_POST['tin']);
require_once("sqlconnect.php");
$q="SELECT CONCAT(address,',',city,',',state) AS caddress,cname,tin FROM company WHERE tin=$tn;
$r = @mysqli_query ($dbc, $q);
//$arr=array();
while ($row = mysql_fetch_array($r))
{
$arr=array('cname'=>$cname, 'caddress'=>$caddress);
echo json_encode($arr);
}
?>
Have you checked a JavaScript error console? 您是否检查过JavaScript错误控制台? Where is the { on the first line of your JavaScript matched? JavaScript第一行中的{在哪里匹配?
First, I've never used mysqli. 首先,我从未使用过mysqli。 Does mysqli_query
work with mysql_fetch_array
? mysqli_query
是否可以与mysql_fetch_array
?
In any case, one potential problem I see is here: 无论如何,我在这里看到一个潜在的问题:
while ($row = mysql_fetch_array($r)) {
$arr=array('cname'=>$cname, 'caddress'=>$caddress);
echo json_encode($arr);
}
That won't be a correct json unless the result count is 1. For example if you get two results you'll generate an incorrect JSON: 除非结果计数为1,否则它将不是正确的json。例如,如果获得两个结果,则将生成错误的JSON:
{"cname":"CNAME1","caddress":"CADDRESS1"}{"cname":"CNAME2","caddress":"CADDRESS2"}
If you need to read multiple rows, the loop should be: 如果需要读取多行,则循环应为:
$arr = array();
while ($row = mysql_fetch_array($r)) {
//...
$arr[] = array('cname'=>$cname, 'caddress'=>$caddress);
}
echo json_encode($arr);
But then the javascript code will get an array back, rather than an object, so you'll have to change it too. 但是,然后javascript代码将返回一个数组,而不是一个对象,因此您也必须更改它。
If this doesn't solve your problem, add some more info/code to your question so that we could understand it better 如果这样做不能解决您的问题,请向您的问题添加更多信息/代码,以便我们更好地理解
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.