我有一个PHP表单,其中有一个名为“Traveling from:”的字段。此字段是一个下拉框,有4个选项(路径)可供选择。
此字段的HTML如下;
<form id="bookingform" action="http://www.example.com/wp-content/themes/theme1/ajax-getvalues.php" method="get">
<p>
<b>Travelling from:</b><br>
<select name="from" id="from" style="-webkit-appearance: menulist-button; width: 300px; position: absolute; height: 30px; font-size: 13px; padding: 5px;">
<option value="">Select a Route...</option>
<?php
while ($row= mysqli_fetch_array($route)) {
echo "<option value='". $row['Route'] ."'>" .
$row['Route'] .
"</option>";
}
?>
</select>
</p><br><br>
</form>
在这个文件的head部分中,我有以下代码监听选择列表更改并进行AJAX调用;
<script type="text/javascript">
$(document).ready(function($) {
var route_id = 'from'; //Route ID
$('#'+route_id).change(function(e) {
//Grab the chosen value on route change
var selectroute = $(this).val();
$.ajax({
url: 'http://www.example.com/wp-content/themes/theme1/ajax-getvalues.php?from='+ encodeURIComponent(selectroute),
success: function(data) {
alert(data);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status + " "+ thrownError);
}
});
});
});
</script>
上面代码中的警告会显示下拉框中所选的值,这会增加我的困惑。
最后,PHP文件(ajax-getvalues.php)具有以下代码来接收值;
然而,selectroute似乎是空的。
我在PHP文件中使用以下代码来测试$ select路由是否为空;
<?php
$connection = mysqli_connect("localhost", "user", "password", "dbname");
$selectroute = mysqli_real_escape_string($connection, $_GET['from']);
if ($selectroute != "") {
$result = mysqli_query($connection, "SELECT DATE_FORMAT(SailingTime,'%h:%i %p') AS FormattedTime FROM wp_timetable WHERE Route_ID = 1 AND '$selectvalue' BETWEEN StartDate AND EndDate");
}
echo '<option value="">Please select a time...</option>';
while($row = mysqli_fetch_array($result))
{
echo '<option value="'.$row['FormattedTime'].'">' . $row['FormattedTime'] . "</option>";
echo $row['FormattedTime'] ."<br/>";
}
mysqli_free_result($result);
mysqli_close($connection);
基本上,如果$selectroute
不为空,则会在表单上的另一个下拉列表中生成结果。但结果不会产生 - 因此$selectroute
为空。
答案 0 :(得分:1)
问题是成功回调函数的两倍selectroute
只是表单元素的值。
var url = 'http://www.example.com/wp-content/themes/theme1/ajax-getvalues.php';
$.ajax({
url: url + '?from=' + encodeURIComponent(selectroute),
success: function(data) {
alert(data);
// I think you want to append the ajax response to the form.
$('#bookingform').append($(data));
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status + " "+ thrownError);
}
});
同样在您的PHP脚本中,您不能使用$selectroute
做任何事情并且有一个未初始化的变量$selectvalue
<?php
$connection = mysqli_connect("localhost", "user", "password", "dbname");
$selectroute = mysqli_real_escape_string($connection, $_GET['from']);
// initialize $selectvalue to today date
$selectvalue = '2015-03-12';
$query = "SELECT DATE_FORMAT(SailingTime,'%h:%i %p') AS FormattedTime
FROM wp_timetable
WHERE Route_ID = 1
AND '$selectvalue' BETWEEN StartDate AND EndDate";
if ($selectroute != "") {
$result = mysqli_query($connection, $query);
if ($result) {
echo '<select>';
echo '<option value="">Please select a time...</option>';
while($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row['FormattedTime'].'">' .
$row['FormattedTime'] .
'</option>';
}
echo '</select>';
mysqli_free_result($result);
}
}
mysqli_close($connection);
?>