我想在选择选择框时从数据库中输入<input type="text"></input>
个文本。这是我的jQuery代码
<script type="text/javascript" src="jquery.js"></script>
<script>
$(document).ready(function() {
$('#noID').change(function() {
var noID = $(this).val();
$.ajax({
type: 'POST',
url: 'ambilusername.php',
data: 'noID=' + noID,
success: function(response) {
$('#username').val(response);
}
});
});
});
</script>
这是我的&#34; ambilusername.php&#34;
<?php
include "../include/koneksi_db.php";
$noID = $_POST["noID"];
$username = mysql_query("SELECT * FROM booking_tamu WHERE noID='$noID'");
while($k = mysql_fetch_array($username)){
echo "$k[email]";
}
?>
这是我的完整index.php代码:
<?php
include "../include/koneksi_db.php";
?>
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script>
$(document).ready(function() {
$('#noID').change(function() {
var noID = $(this).val();
$.ajax({
type: 'POST',
url: 'ambilusername.php',
data: 'noID=' + noID,
success: function(response) {
$('#username').val(response);
}
});
});
});
</script>
</head>
<body>
<form method="post" action="?page=prosesTambahAkun">
<table class="table-data" width=100% border=1>
<tr><td colspan="2" class="head-data">Tambah Data Akun</td></tr>
<tr>
<td class="pinggir-data">ID Pemesan</td>
<td>
<select name="noID" id="noID" required>
<option value="">------Pilih ID Pemesan------</option>
<?php
$query = "SELECT no_booking, COUNT(no_booking) AS 'jumlah' FROM booking_tamu GROUP BY no_booking ASC";
$hasil = mysql_query($query);
while ($data = mysql_fetch_array($hasil)){
echo "<option value='".$data['no_booking']."'>".$data['no_booking']."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td class="pinggir-data">Username</td>
<td>
<input type="text" id="username" name="username" size="30px" readonly></input>
</td>
</tr>
<tr><td colspan="2" align="center" class="head-data">
<input type="submit" value="Tambah" name="tambah">
</td></tr>
</table>
</form>
</body>
</html>
问题是当我在选择框中选择值时,文本框不会在其上放置一些内容。你能帮帮我吗?
答案 0 :(得分:0)
问题是表格列的命名:
改变这个:
$username = mysql_query("SELECT * FROM booking_tamu WHERE noID='$noID'");
到此:
$username = mysql_query("SELECT * FROM booking_tamu WHERE no_booking='$noID'");