Set the attribute of the value of input fields from the MYSQL database when selecting data from the drop-down list

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 = $('#departure).text();
    var flight_arrival = $('#arrival).text();

    $('#flight_number').on('change', function() {

        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 , , , , , - [ ] This is a screenshot


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%

+3
2

, .

dataType: "json"

$.ajax({
        url: "getFlightData.php",
        type: "get",
        data: '?flight_number=$flight_number',
        success: function(data){ ... },
        dataType: "json", //<--------- this 
});

$.ajax()


json php

JSON:

header('Content-Type: application/json');

JSON-P:

header('Content-Type: application/javascript');
0

, , . @satyrwilder javascript.

, datatime- , jquery + php

getFlightDate.php

<?php
header('Content-Type: application/json');
include "dbConnect.php";
function datetime()
{
    return date( 'Y-m-d\TH:i:s', time());

}

$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' => datetime($row['departure']),
    'arrival' => datetime($row['arrival'])
   );
  array_push($data, $row_data);

}
echo json_encode($data);
?>

print.php

<?php
include "dbConnect.php";
$flight_numbers = mysql_query("SELECT flight_number FROM flights"); 
?>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<title>Test</title>
</head>
<body>
<select id="flight_number">
<?php while($row = mysql_fetch_array($flight_numbers)) 
{
Print "<option>".$row['flight_number'] . "</option> ";
} 
?>
</select>
<br>
<input type="text" id="destination">
<input type="datetime-local" id="departure" />
<input type="datetime-local" id="arrival" />
<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);

    });
    });
});
</script>
</body>
</html>

, datetime json_encode, - , 2014-12-05T08:30:59Y-m-d\TH:i:s

0

All Articles