这是一个非常常见的问题,但是尝试了一些没有用的东西。我试图根据从下拉列表中选择的值来填充数据库中的文本框值。 我的index.php
<?php
# Connect
mysql_connect('localhost', 'user', 'pass') or die('Could not connect: ' . mysql_error());
# Choose a database
mysql_select_db('db_name') or die('Could not select database');
# Perform database query
$query = "SELECT * from customer_reg";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>whatever</title>
</head>
<body>
<label for="ddcust_nm">Customer Name: </label>
<select id="customer_name">
<option value="0">Please select</option>
<?php
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="' . $row['id'] . '">' . $row['customer_name'] . '</option>';
}
?>
</select>
<div>
<label for="address">Street Address</label>
<input id="address" name="address" class="element text large" type="text">
</div>
<div>
<span class="floatLeft">
<label for="phoneno">phone No:</label>
<input id="phoneno" name="phoneno" class="element text medium" style="width:14em" type="text">
</span>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
function insertResults(json){
$("#address").val(json["StreetAddress"]);
$("#phone").val(json["City"]);
}
function makeAjaxRequest(placeId){
$.ajax({
type: "POST",
data: { placeId: placeId },
dataType: "json",
url: "search.php",
success: function(json) {
insertResults(json);
}
});
}
$("#customer_name").on("change", function(){
var id = $(this).val();
if (id === "0"){
clearForm();
} else {
makeAjaxRequest(id);
}
});
</script>
</body>
</html>
Search.php
<?php
mysql_connect('localhost', 'user', 'pass') or die('Could not connect: ' .
mysql_error());
mysql_select_db('db_name') or die('Could not select database');
$placeId = $_POST['placeId'];
$query = "SELECT * from customer_reg";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
if ($placeId == $row['id']){
echo json_encode($row);
}
}
?>
我可以从下拉列表中获取价值,但是,在下拉列表中选择任何值之后,什么也不会发生。 我在每个功能中都尝试过警报,但它什么也没显示。 我尝试单击(我知道我不应该这样做),但没有触发 我从HERE
获得了这段代码请帮助