0
votes

So, I'm at a bit of a lost when it comes to submitting a form and returning data based on what was selected in a drop-down list.

My current drop-down list is populated with flights, gathered from a database.

My question is, once a flight is picked from the drop-down list and the submit button has been pressed, how do I go about returning more information? (Once a flight is picked, I want to show the list of passengers on that flight)

<!DOCTYPE html>
<html>
<head>
    <style>
        body { /*Set default text color used in body*/
            color: #ff8306;
        }
    </style>
    <title>1234</title>
</head>
<body bgcolor="#0a06ff"> <!-- Set default background color-->
<p align="center">List of all passengers: <br> 
    <?php    
    $sqlRequest = "SELECT firstName, lastName FROM passenger ORDER BY lastName"; //Select the passenger first namw and last, in alphabetic order of lastname
    $result = $connection->query($sqlRequest); //save result

    if($result->num_rows > 0) { //print out the names in alphabetic order of lastname in rows
        while($row = $result->fetch_assoc()) {
            echo $row["firstName"]. " ". $row["lastName"]. "<br>";
        }
    }

    ?>
</p> 

<form align="center" action="flit.php" method="post">
    <fieldset> 
        <legend>Flight Information</legend> 
            <?php
                $sqlRequest = "SELECT flightnum, origination, destination FROM flight";
                $result = $connection->query($sqlRequest); 

                echo "Select a flight:". "<br>";
                echo "<Select Flight='flight'>";
                while ($row = $result->fetch_assoc()) { //populate dropdown with flights
                    echo "<option value='". $row['flightnum']. "'>". $row['flightnum']. ": ". $row['origination']. " to ". $row['destination']. "</option>";
                }
                echo "</select>"; 
                echo '<button type="Submit" value="Submit">Submit</button>';

            ?>
     </fieldset>
    </form>
  </body>
</html>

Just in case, here are my tables,

Flight table:

flight (flightnum int auto_increment primary key, origination char(25), destination char(25),miles int)

Passenger Table:

passenger(passnum int auto_increment primary key, lastName char(10), firstName char(10) )

Manifest Table

manifest(flightnum int, flightDate date, passnum int, seatnum char(5),

primary key(flightnum, flightDate, passnum),

foreign key(flightnum) references flight(flightnum) ,

foreign key(passnum) references passenger(passnum))

1
Basically, you get the value submitted by the form, and use it in the where clause to select the records you want. What have you tried in writing flit.php? That's the page you submit your form to. - Sloan Thrasher
If you want a better way than the post, go to this link stackoverflow.com/questions/43313917/… .The database it is PostgreSQL but you can change for any type. Do not forget to vote if you like the solution. - Jose Marques

1 Answers

-1
votes

You want to use an AJAX request. Send the selection back to the server, do your query based on that selection and return the data back to the form.

This can all be accomplished pretty easily if you're using jQuery, a little harder if you're using straight JavaScript. This also means you're going to have to separate your PHP from your html code and put it in a different file (which is easier to maintain anyway).

There are tons of tutorials. If you get stuck on a specific AJAX question, open up a new question.