当用户选择客户名称时,我想填写地址&该客户的城市在运行时从数据库。因为我正在调用javascript函数更改和从我调用php函数addfun()并尝试传递和keyvalue进行查询。我写下面的代码,但那不起作用。
<script language='javascript' type='text/javascript'>
function ILovePHP(frm, a) {
var formName = frm.name;
alert(formName);
b=<?php addfun(formName,a);?>;
alert(b);
}
</script>
<?php
$attrs = array("onChange" => "ILovePHP(this.form, this.value);");
require_once "HTML/QuickForm.php";
require_once "HTML/QuickForm/Renderer.php";
$form = new HTML_QuickForm('customer_master', 'post');
$form->addElement('select','custName','Select Customer Name',$cnameAry,$attrs);
$form->addElement('text', 'custAdd', 'Customer Address');
$form->addElement('text','custCity', 'City');
$form->addElement('text','custState', 'State');
$form->display();
function addfun($frm, $p)
{
$query="Select Address, City from entityMaster where eid=$p ";
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('GenInsurance');
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$frm->setDefaults(array( 'custAdd' => $row['Address']));
$frm->setDefaults(array( 'custCity' => $row['Address']));
print $frm;
return $frm;
}
?>
答案 0 :(得分:1)
使用jquery和ajax从php文件中获取地址和城市:
创建一个名为ajax.php的文件:
if(isset($_GET['name']))
{
$p = $_GET['name'];
$query="SELECT Address, City FROM entityMaster WHERE eid=$p ";
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('GenInsurance');
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$output = array('custAdd' => $row['Address'], 'custCity' => $row['Address']);
echo json_encode($output);
}
Onchange获取城市和地址:
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
$('#custName').change(function(){
$.getJSON("ajax.php", { name: $(this).val() } , function(data){
$.each(data, function(key, value) {
if(key == "custCity"){
$('#custCity').val(value);
}
if(key == "custAdd"){
$('#custAdd').val(value);
}
});
});
});