Self submit and dynamic dropdown

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rashidf2
Forum Newbie
Posts: 1
Joined: Sat Dec 05, 2009 8:33 pm

Self submit and dynamic dropdown

Post by rashidf2 »

I have a php page that recevies a value "$cat" from another page. It uses that value to populate a dropdown menu dynamically with selections. When the user selects one value (form the drop down) and clicks on submit, the form is supposed to self submit and run execute another query and display the results as a table.

My problem is: every time I click on submit the selection values form the first drop-down clear. But I want the selections to stay, so the user can re-use the form to submit a differnet selection.

Here is the code: The file name is "dd3ck.php"

Code: Select all

 
<!doctype html public "-//w3c//dtd html 3.2//en">
 
<html>
 
<head>
<title>Multiple drop down box</title>
</head>
 
<body>
<?php
 
if (!empty($_POST)) {
 
$username="root";
$password="";
$database="cs631";
 
// Connect to the server
$con = mysql_connect(localhost,$username,$password);
mysql_select_db($database, $con);
 
$cat = $_REQUEST['cat'];
 
$q1 = "SELECT * FROM `Station` WHERE RouteSriNumber = " . $cat;
$result = mysql_query($q1);
?>
 
<table>
<tr>
<td>
Select a Station ID from the List:
</td>
 
<form method=post name=f2 action='dd3ck.php'>
<tr><td>
<?php
echo "";
echo "<select size = 5 name='Station'>" ;
while($noticia = mysql_fetch_array($result)) { 
echo  "<option value='$noticia[StationID]'> $noticia[StationID] </option>";
}
echo "</select>"; 
 
}
//mysql_close($con);
 
?>
</td></tr>
<tr><td> <input type=submit value='Submit'></td></tr> 
 
</form>
</table>
 
<?php
 
if ($_SERVER['REQUEST_METHOD'] == "POST"){ 
 
$St = $_REQUEST['Station'];
If ($St <>"")
{
$username="root";
$password="";
$database="cs631";
 
// Connect to the server
$con = mysql_connect(localhost,$username,$password);
mysql_select_db($database, $con);
 
$q2 = "SELECT * FROM `Station` WHERE RouteSriNumber =" .  $St;
echo $q2;
$result2 = mysql_query($q2);
 
 
echo "<table border='1'>
<tr>
<th>StationID</th>
<th>Latitude</th>
<th>Longitude</th>
</tr>";
 
while($row = mysql_fetch_array($result2))
  {
  echo "<tr>";
  echo "<td>" . $row['StationID'] . "</td>";
  echo "<td>" . $row['Latitude'] . "</td>";
  echo "<td>" . $row['Longitude'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
 
mysql_close($con);
 
}
}
 
?>
 
</body>
 
</html>[/color]
 
 
 
[color=#80FFBF
Marinusjvv
Forum Commoner
Posts: 29
Joined: Wed Dec 02, 2009 5:59 am

Re: Self submit and dynamic dropdown

Post by Marinusjvv »

echo "<select size = 5 name='Station'>" ;
while($noticia = mysql_fetch_array($result)) {
echo "<option value='$noticia[StationID]'> $noticia[StationID] </option>";
Here you need to add something like:

Code: Select all

if($_REQUEST["Station"] <> NULL)
{
 
}
And make that option -> selected = "selected"
hemakumarrr
Forum Newbie
Posts: 7
Joined: Mon Nov 09, 2009 12:42 am

Re: Self submit and dynamic dropdown

Post by hemakumarrr »

Hi,

Its Simple. You store the value in hidden field.

<?PHP
//some code
$cat=$_REQUEST['cat'];
//some code
?>
<form method=post name=f2 action='dd3ck.php'>
<input type="hidden" name="cat" value="<?PHP echo $cat; ?>" />
<?PHP
//some code
?>
<?php
echo "";
echo "<select size = 5 name='Station'>" ;
while($noticia = mysql_fetch_array($result))
{
if($noticia[StationID]==$_POST['Station'])
{
echo "<option value='$noticia[StationID]' selected> $noticia[StationID] </option>";
}
else
{
echo "<option value='$noticia[StationID]'> $noticia[StationID] </option>";
}
}
echo "</select>";

//some code
?>
</form>
<?PHP
//some code
?>
Post Reply