I am trying to send a selection request and set the result as an attribute of a value for different input fields, the request should be sent when selecting a value from the drop-down list. After some research, I found that this can be achieved through jQuery.
jQuery will send the query to the php file containing my query and the selection result, and then will return the values in json format. At the moment, everything works fine, my php file works and returns valid json data, but I can not get this data into the input fields that I have. Here is my script that should run the php file and return the results in json, and then add the results to the text fields.
Check my violin code
<script>
var flight_destination = $('#destination).text();
var flight_departure = $('
var flight_arrival = $('#arrival).text();
$('
var flight_info = $('#flight_number :selected').text();
$.ajax({
url: "getFlightData.php",
type: "get",
data: '?flight_number=$flight_number',
success: function(data){
var flight_destination = data[1];
var flight_departure = data[2];
var flight_arrival = data[3];
}
}
$('#destination').val(flight_destination);
$('#departure').val(flight_departure);
$('#arrival').val(flight_arrival);
})
</script>
getFlightData.php
<?php
include "dbConnect.php";
$flight_number = $_GET['flight_number'];
$query = mysql_query("SELECT * FROM flights WHERE flight_number='$flight_number'");
$data = array();
while($row = mysql_fetch_array($query))
{
$row_data = array(
'flight_number' => $row['flight_number'],
'destination' => $row['destination'],
'departure' => $row['departure'],
'arrival' => $row['arrival']
);
array_push($data, $row_data);
}
echo json_encode($data);
?>
: line. data:'flight_number='+$('#flight_number').val(), data:{'flight_number':$('#flight_number').val()},
json ,
2
this , , , , , - [ ]

3
@satyrwilder . script
$(function(){
var flight_destination = $('#destination');
var flight_departure = $('#departure');
var flight_arrival = $('#arrival');
var flight_number = $('#flight_number');
$('#flight_number').on('change', function() {
var flight_info = $('#flight_number :selected').text();
$.ajax({
url: "getFlightData.php",
type: "get",
dataType: "json",
data: { 'flight_number' : flight_number.val() }
})
.done(function(data) {
$("#destination").val(data[0].destination);
$("#departure").text(data[0].departure).val(data[0].departure);
$("#arrival").text(data[0].arrival).val(data[0].arrival);
});
});
});
datetime. , 100%