如果您不想刷新整个页面,则需要使用一些 Javascript/Ajax。JQuery 允许使用.post()方法轻松执行您需要的操作。
首先将 JQuery 文件添加到您的 HTML 标头中
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
然后根据您的请求创建一个 .php 文件(例如:update_owner.php):
<?php
// 1. Connect to your SQL database
// ...
// 2. Get the type
$type = $_POST['type'];
// 3. Perform your query
$results = mysql_query("SELECT * FROM owner where type=".$type);
// 4. Get the only result you want (the first row)
$row = mysql_fetch_array( $results );
// 5. Return the result in json format (assuming that the name
echo json_encode(array("responseCode" => 200, "row" => $row));
然后添加一些javascript(使用JQuery):
$("select#type").change(function() {
// Get the value of your input field
var type = $(this).val();
// Set the URL
var url = 'update_owner.php';
// Start send the post request
$.post(url,
function(data){
// The response is in the data variable
if (data.responseCode == 200) {
// Write the name in the right cell (with #owner id)
$("#owner").html( data.row.name );
}
else {
// Bad request
}
},"json"
); // Make sure the the response is in json format
return false;
});
应该这样做
PS:我为我糟糕的英语道歉......我是法国人