我正在嘗試構建我的第一個jQuery Web應用程序,但是我碰到了一個障礙,似乎無法弄清楚這一點。將php鏈接到jquery
我有一個PHP頁面和一個HTML頁面。 HTML頁面有一個帶有三下拉列表的表單。 PHP頁面連接到數據庫,但我不確定如何從php頁面傳遞查詢結果來填充html/javascript頁面上的下拉列表。
這是我的代碼到目前爲止。
HTML:
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#selector").submit(function() {
$.ajax({
type: "GET",
url: "DBConnect.php",
success: function(msg){
alert(msg);
}
});
var select_car_make = $('#select_car_make').attr('value');
var select_car_model = $('#select_car_model').attr('value');
var select_car_year = $('#select_car_year').attr('value');
alert("submitted");
}); //end submit
});
</script>
<h1 style="alignment-adjust:center">Car information:</h1>
<hr />
<div id="results">
<form action="get.php" id="selector" method="get" name="sizer">
<table width="451" height="70" border="0">
<th width="145" height="66" scope="row"><label for="select_car_make"></label>
<div align="center">
<select name="select_car_make" id="select_car_make" onchange="">
</select>
</div></th>
<td width="144"><label for="select_car_model"></label>
<div align="center">
<select name="select_car_model" id="select_car_model">
</select>
</div></td>
<td width="140"><label for="select_car_year"></label>
<div align="center">
<select name="select_car_year" id="select_car_year">
</select>
</div></td>
</tr>
</table>
<input name="completed" type="submit" value="submit" />
</form>
這裏是PHP頁面:
<?php
$DBConnect = mysqli_connect("localhost", "root", "password", "testing")
or die("<p>Unable to select the database.</p>" . "<p> Error code " . mysqli_errno($DBConnect) . ": " . mysqli_error($DBConnect)) . "<p>";
echo "<p>Successfully opened the database.</p>";
$SQLString1 = " SELECT car_make FROM cars";
$QueryResult = mysqli_query($DBConnect, $SQLString1)
您可以將HTML頁面保存爲PHP頁面,然後將其保存在HTML標籤標籤中,並使用PHP來填充數據庫中的數據。 – NightHawk 2011-06-03 18:52:49