I have a PHP form which has a field called "Travelling from:". This field is a drop-down box which has 4 options (routes) to choose from.

The HTML for this field is as follows;

<form id="bookingform" action="http://www.example.com/wp-content/themes/theme1/ajax-getvalues.php" method="get"> 
        <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>
            while ($row= mysqli_fetch_array($route)) {
                echo "<option value='". $row['Route'] ."'>" . 
                          $row['Route'] . 

Within the the head section of this file, I have the following code which listens for the select list change and makes the AJAX call;

 <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();
             url: 'http://www.example.com/wp-content/themes/theme1/ajax-getvalues.php?from='+ encodeURIComponent(selectroute),
             success: function(data) {
             error: function (xhr, ajaxOptions, thrownError) {
                 alert(xhr.status + " "+ thrownError);

The alert in the above code appears with the selected value from the dropdown box as expected - which adds to my confusion.

And finally, the PHP file (ajax-getvalues.php) has the following code to receive the value;

Yet $selectroute seems to be empty.

I used the following code in the PHP file to test if $select route was empty;

$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/>";


Basically, results are produced in another drop-down on the form if $selectroute is not empty. But the results are not produced - therefore $selectroute is empty.

2015/03/12 16:30
  • get
  • php
  • html
  • ajax
  • 点赞
  • 收藏
  • 回答