我正在嘗試使用jQuery ajax api從mysql數據庫中檢索數據。我正在使用的SQL查詢工作正常,因爲我通過創建用戶表單和傳統的內容來測試它,看它是否從我的數據庫中檢索到正確的數據。使用jQuery AJAX從MYSQL中檢索數據的難度
但是,我現在想從我的mysql表中檢索數據,而無需使用jQuery ajax函數重新加載頁面。當談到jQuery時,我是一個新手,我之前沒有使用過它,如果有人能夠爲我提供一個很好的例子,我會非常感激。我在網上看過各種文章和指南,但我無法理解它。
這是一種形式:
<form id="restaurant_reservation">
<label for="date">Reservation Date?</label>
<input type="text" name="date" id="date" value="yyyy-mm-dd" />
<label for="capacity">Amount of Customers?</label>
<input type="text" name="capacity" id="capacity" />
<label for="license">Smoking or Non-Smoking Area?</label>
<select id="license">
<option value="0" id="no">Smoking</option>
<option value="1" id="yes">Non-Smoking</option>
</select>
</form>
這裏是PHP代碼:
<?php
$user_reservation_date = $_POST['user_date'];
$user_customer_amount = $_POST['customer_amount'];
$user_smoking_non_smoking = $_POST['user_smoking_selection'];
$my_query = "SELECT * FROM restaurant
WHERE $user_reservation_date = date_available
AND $user_customer_amount >= max_seating
AND $user_smoking_non_smoking = smoking_choice";
$result =& $db->query($my_query);
if (PEAR::isError($result)) {
die($result->getMessage());
}
echo '<div id="output">';
echo'<table">';
echo '<th>restaurant name</th>';
echo '<th>Max Customer Seating</th>';
echo '<th>Smoking or Non Smoking</th>';
echo '<th>Average price per head</th>';
while($row =& $result->fetchRow()) {
echo '<tr>';
echo '<td>'.$row['rest_name'].'</td>'
echo '<td>'.$row['max_seating'].'</td>'
echo '<td>'.$row['smoking_choice'].'</td>'
echo '<td>'.$row['avg_price_per_head'].'</td>'
echo '</tr>';
}
echo '</table>';
?>
這裏是我的jQuery代碼嘗試:
$(function(){
$('#date').keyup(function(){
var user_input= $('#date').val();
});
});
$(function(){
$('#date').keyup(function(){
var user_input1=$('#date').val();
$.ajax({
type: 'POST',
data: ({user_date : user_input1}),
url: 'search.php',
success: function(data) {
$('#output_div').html(data);
}
});
});
});
我使用相同的代碼但改變窗體中其他兩個字段的#values。
我想使用jQuery ajax獲取此表單的輸入和選定值並將它們存儲在我的php文檔中的一個變量中,這樣我就可以在我的sql查詢中使用用戶表單數據來檢索相應的餐館。
我真的很喜歡學習如何做到這一點,我真的apreciate任何幫助。非常感謝您的閱讀。如果我在正確描述我的問題時含糊不清,我很抱歉。謝謝。
你看過'jquery.ajax'文檔嗎? http://api.jquery.com/jQuery.ajax/ – undefined 2012-04-25 15:25:29
那麼,你的PHP代碼在哪裏? – Blazemonger 2012-04-25 15:27:19
嗨,是的,我一遍又一遍地讀了它,但我很難理解它的概念。在我看來,爲了理解他們的文檔,你必須對jQuery有一定的知識水平。你能幫我一下嗎?謝謝。 – 2012-04-25 15:27:59