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">
<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>
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();
$.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>
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;
<?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);
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.