如何在下拉列表更改时自动填充mysql查询结果中的文本框值
How do I autofill textbox values from mysql query result onChange of dropdown?
我希望customer_address
、customer_city
、customer_state
和customer_zip
自动填充customer_name
下拉列表中的onChange
。我环顾四周,但被难住了。
请帮忙。
<form action="" method="post" name="booking_form" class="booking" style="width: auto">
<select name="customer_name" onChange="???????">
<option value="index.php">Click to select your school </option>
<?php
// Make a MySQL Connection
$username="";
$password="";
$database="";
mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$id = $row['id'];
$school = $row['customer_name'];
$phone = $row['customer_phone'];
$address = $row['customer_address'];
$city = $row['customer_city'];
$state = $row['customer_state'];
$zip = $row['customer_zip'];
$notes = $row['customer_notes'];
?>
<option value="<?php echo $school; ?>"><?php echo "$school".", "."$city"; ?></option>
<?php }
?></select>
<input type="text" name="customer_address">
<input type="text" name="customer_city">
<input type="text" name="customer_state">
<input type="text" name="customer_zip">
</form>
您可以生成一个包含所有sql结果的javascript对象,如下所示:
var schoolInfo = { id1 : {name:"Name 1", address:"address line" ...},
id2: {} .... }
然后将每个设置为包括自定义属性"sid":
<option value="school" sid="id1">school, city</option>
对于使用jQuery的元素的onchange事件处理程序:
.change(function(){
var sid = $(this).attr('sid');
$("input[name='customer_address']").val(schoolInfo[sid].address);
$("input[name='customer_city']").val(schoolInfo[sid].city);
$("input[name='customer_zip']").val(schoolInfo[sid].zip);
});
自从我最初的帖子以来,有一些事情。。。mysql_
不再安全。此外,将所有内容都放在一个文件中也不好,最好将sql&业务逻辑,从您的视图中查找MVC
模式。然而,这里是快速&脏方法:您可以在此处看到最终结果:http://jsfiddle.net/webaholik/kcce6Ls9/
<?php
$sql = "SELECT id,
customer_name AS name,
customer_phone AS phone,
customer_address AS address,
customer_city AS city,
customer_state AS state,
customer_zip AS zip,
customer_notes AS notes
FROM table";
$schoolInfo = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
$array = array();
foreach ($schoolInfo as $item) {
$array[$item['id']] = $item;
}
$json = 'var SchoolInfo = ' . json_encode($array) . ';';
?>
<form action="" method="post" name="booking_form" class="booking"
style="width: auto">
<select id="customer_name" name="customer_name">
<option value="index.php">Click to select your school</option>
<?php foreach($schoolInfo as $row){?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['name'].", ".$row['city']; ?></option>
<?php } ?>
</select>
<input type="text" name="customer_address">
<input type="text" name="customer_city">
<input type="text" name="customer_state">
<input type="text" name="customer_zip">
</form>
<script>
<?php echo $json;?>
$("#customer_name").change(function(){
var sid = $(this).val();
$("input[name='customer_address']").val(SchoolInfo[sid].address);
$("input[name='customer_city']").val(SchoolInfo[sid].city);
$("input[name='customer_zip']").val(SchoolInfo[sid].zip);
});
</script>
尝试发布一个php脚本并与Mysql建立连接,然后返回值。
<select name="customer_name">
<option id='selects' value="index.php">Click to select your school </option>
Javascript
$("select[name='customer_name']").change(function(){
$.post("-YOUR PHP FILE-", customer_name: $(this).val(), function(data){
schools = data.split(",");
for(var i in schools){
$("select[name='customer_name']").append("<option>"+schools[i]+"</option>");
}
})
})
PHP
<?php
//You connected and got an array contains your school names($schools)
$schools = implode(",",$schools);
echo $schools;
?>
我想你想做那样的事。
相关文章:
- 当查询不在displayField中时,引导Ajax Typeahead不显示结果
- 如何将这些查询结果放入表中
- 有关PouchDB的复杂startkey/endkey查询未返回预期结果
- 如何在下拉列表更改时自动填充mysql查询结果中的文本框值
- 将数据库中的超链接添加到查询结果
- onchange选择get value并执行查询,然后在同一页面上显示结果
- 为什么我的文本框没有用查询结果更新
- 如何将mysql查询的结果获取到html文本框中
- 使用jquery显示mysql查询结果
- 需要呈现MongoDB查询返回结果的特定索引/位置
- 如何使用 angularjs 以表格格式显示 parse.com 的查询结果
- Meteor:在模板接受Mongo查询结果之前修改它的最佳实践
- 未捕获的范围错误:WebSQL查询结果中的项索引超出范围错误
- 返回解析云代码关系查询的结果
- 自定义查询字符串分析器返回不正确的结果
- 表单提交后保留数据库查询结果
- 从sqlite数据库查询数据结果文件名
- 如何显示查询回调数据结果仅从开始
- 在模板帮助程序中连接两个查询的结果
- 将结果查询到 JSON Laravel 5