我正在尝试使用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