我有一个名为 customers_shipto 的表,其中包含列 (ID、shiptonick、address)
我需要使用与 AJAX 的特定记录相关的地址列中的数据自动填充文本区域“shipto”
我的选择看起来像这样
<select name="shiptonick" id="shiptonick">
<option value="">Click Here to Change Ship To</option>
<?
$sql = "SELECT * FROM customers_shipto WHERE customer_id = '$customer_id' ORDER BY shiptonick ASC";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
?>
<option value="<?=$row['ID']?>"><?=$row['shiptonick']?></option>
<? } ?>
</select>
和 textare 看起来像这样
<textarea name="shipto" id="shipto" cols="30" rows="5"></textarea>
我假设我会调用像 getaddress.php 这样的文件来将结果返回给 AJAX
$sql = "SELECT * FROM customers_shipto WHERE ID = '$ID'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo $row['address'];
}
更新:这是 AJAX 的一些示例代码,我认为它与我想要的很接近,但不是调用“提交”,而是需要为该特定选择调用“onChange”,以便它执行此代码。
<script type"text/javascript">
$(document).ready(function(){
$("form#submit").submit(function() {
// we want to store the values from the form input box, then send via ajax below
var ID = $('#ID').attr('value');
var shiptonick = $('#shiptonick').attr('value');
$.ajax({
type: "POST",
url: "includes/getaddress.php?",
data: "ID="+ ID+
"&shiptonick="+ shiptonick,
success: function(data){
//Return address here and place in textarea njavascript code
}
});
return false;
});
});
</script>
我很亲密吗?:)