Jump to content

Recommended Posts

Hello all.

I have a single PHP page (called "routes.php") which contains a form with 3 dropdown lost boxes and a Submit button. Depending on what the user selects in the boxes, when they click Submit, the script runs off to a MySQL database and pulls down the appropriate data and builds a table in the "routes.php" page.

My problem is this: How can I get the selections in the dropdown boxes to remain in place as, when the "routes.php" page is refreshed to build the table, the boxes are all reset to their default value?

The form/MySQL code is below:

[CODE]<form action="routes.php" method="post">

<select name="cbointdom" size="1">
<option value="%e%">All</option>
<option value="International">International</option>
<option value="Domestic">Domestic</option>
</select>&nbsp;&nbsp;

<select name="cboday" size="1">
<option value="%day%">All</option>
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
<option value="Saturday">Saturday</option>
<option value="Sunday">Sunday</option>
</select>&nbsp;&nbsp;

<select name="cboaircraft" size="1">
<option value="%B%">All</option>
<option value="Beechcraft Baron">Beechcraft Baron</option>
<option value="Beechcraft King Air">Beechcraft King Air</option>
<option value="Boeing 737-400">Boeing 737-400</option>
<option value="Boeing 747-400">Boeing 747-400</option>
<option value="Boeing 777-300">Boeing 777-300</option>
</select>&nbsp;&nbsp;

<input type="submit" value="Search" />

</form>

  <p style="word-spacing: 0; text-indent: 0; line-height: 120%; margin: 0">&nbsp;</p>
  <p style="word-spacing: 0; text-indent: 0; line-height: 120%; margin: 0">&nbsp;</p>



<?php include("dbinfo.inc.php"); mysql_connect("127.0.0.1",$username,$password);
@mysql_select_db($database) or die("Unable to select database");

$cintdom = $_POST["cbointdom"];
$cday = $_POST["cboday"];
$caircraft = $_POST["cboaircraft"];

$result = mysql_query("SELECT * FROM FLIGHTS WHERE intdom LIKE \"$cintdom\" AND day LIKE \"$cday\" AND aircraft LIKE \"$caircraft\" ORDER BY 'flightid' ASC") or die(mysql_error());

echo "<table border='1' bordercolor='black' cellspacing='0' cellpadding='2' width=\"900\">";
echo "<tr bgcolor=\"#99CCFF\"> <th align=\"left\" width=\"80\"><font face=\"Arial Unicode MS\" size=\"1\">FLIGHT TYPE</th> <th align=\"left\" width=\"60\"><font face=\"Arial Unicode MS\" size=\"1\">DAY OF FLIGHT</th> <th align=\"left\" width=\"60\"><font face=\"Arial Unicode MS\" size=\"1\">FLIGHT NUMBER</th> <th align=\"left\" width=\"140\"><font face=\"Arial Unicode MS\" size=\"1\">DEPARTURE AIRPORT</th> <th align=\"left\" width=\"90\"><font face=\"Arial Unicode MS\" size=\"1\">DEPARTURE TIME</th> <th align=\"left\" width=\"140\"><font face=\"Arial Unicode MS\" size=\"1\">ARRIVAL AIRPORT</th> <th align=\"left\" width=\"90\"><font face=\"Arial Unicode MS\" size=\"1\">ARRIVAL TIME</th> <th align=\"left\" width=\"100\"><font face=\"Arial Unicode MS\" size=\"1\">FLIGHT DISTANCE</th> <th align=\"left\" width=\"100\"><font face=\"Arial Unicode MS\" size=\"1\">FLIGHT DURATION</th> <th align=\"left\" width=\"130\"><font face=\"Arial Unicode MS\" size=\"1\">TYPE OF AIRCRAFT</th> </tr>";
while($row = mysql_fetch_array( $result )) {

    echo "<tr><td width=\"80\"><font face=\"Arial Unicode MS\" size=\"1\">";
    echo $row['intdom'];
    echo "</td><td width=\"60\"><font face=\"Arial Unicode MS\" size=\"1\">";
    echo $row['day'];
    echo "</td><td width=\"60\"><font face=\"Arial Unicode MS\" size=\"1\">";
    echo $row['flightno'];
    echo "</td><td width=\"140\"><font face=\"Arial Unicode MS\" size=\"1\">";
    echo $row['depairport'];
    echo "</td><td width=\"90\"><font face=\"Arial Unicode MS\" size=\"1\">";
    echo $row['deptime'];
    echo "</td><td width=\"140\"><font face=\"Arial Unicode MS\" size=\"1\">";
    echo $row['arrairport'];
    echo "</td><td width=\"90\"><font face=\"Arial Unicode MS\" size=\"1\">";
    echo $row['arrtime'];
    echo "</td><td width=\"100\"><font face=\"Arial Unicode MS\" size=\"1\">";
    echo $row['distance'];
    echo "</td><td width=\"100\"><font face=\"Arial Unicode MS\" size=\"1\">";
    echo $row['duration'];
    echo "</td><td width=\"130\"><font face=\"Arial Unicode MS\" size=\"1\">";
    echo $row['aircraft'];
    echo "</td></tr>";

}

echo "</table>";

?>[/CODE]
Link to comment
https://forums.phpfreaks.com/topic/14700-retaining-form-data-in-a-php-page/
Share on other sites

add an if() to each option to check if the posted value equals the option value, and echo "selected" if it is:

[code]<select name="cbointdom" size="1">
<option value="%e%"<?php if ($_POST['cbointdom'] == '%e%') echo ' selected="true"'; ?>>All</option>
<option value="International"<?php if ($_POST['cbointdom'] == 'International') echo ' selected="true"'; ?>>International</option>
<option value="Domestic"<?php if ($_POST['cbointdom'] == 'Domestic') echo ' selected="true"'; ?>>Domestic</option>
</select>[/code]

and so on.  if you don't want to have to rewrite that a bunch of times, you could look into using arrays and loops to output all your select options.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.