Page 1 of 1

Keeping dropdown selections after submission

Posted: Wed Apr 27, 2011 8:12 pm
by bemaitea
Hello all,

I currently have a php from which populates the selections based on information found in a SQL database.

As it stands, the user selects the various options in the drop down lists and then submits these selctions.

Using $_POST commands, I then populate a list of the current configurations to summarize the selections. Only problem is that once the first form is submitted for review, all the selections get reset. So if any changes need to be made the user has to reselect all the options.

This is a simplified version of the php form:

Code: Select all

// Make a MySQL Connection 
<?php mysql_connect("localhost", "kp_dbl", "mastermaster") or die(mysql_error());
mysql_select_db("kp_db") or die(mysql_error());
?> 
<br />
<form action="build22.php" method="post">
<input type="hidden" name="data" value="1" />
<br />
<br />
<?php
// GRAB DATA
$result = mysql_query("SELECT * FROM special2 WHERE cat = 'color' ORDER BY cat") 
or die(mysql_error());  
echo "<div id='color'><select id='color' name='product_color'>";

while($row = mysql_fetch_array( $result )) {
$name= $row["name"];
$cat= $row["cat"];
$price= $row["price"];
echo "<option value='";echo $name;echo"'>";echo $name;echo" ($$price)</option>";} 
echo "</select>";
echo "<input type='hidden' name='amount_color' value='";echo $price;echo"'></div>";
?>

<input type="submit" value="Update Configuration">
</form>
Once submitted, the summary prints using this code:

Code: Select all

<div id="config" style="background-color:#FFF; font-size:12px; line-height:22px;">
  <h1>Current Configuration:</h1>
  <?php echo "<strong>Color:</strong>&nbsp&nbsp&nbsp&nbsp";echo $_POST['product_color']; ?>
</div>
Any suggestions on how to keep these selections after hitting submit?

Thanks for any help! :)

Re: Keeping dropdown selections after submission

Posted: Thu Apr 28, 2011 9:49 am
by Celauran

Code: Select all

$result = mysql_query("SELECT * FROM special2 WHERE cat = 'color' ORDER BY cat") or die(mysql_error());  
echo "<div id='color'><select id='color' name='product_color'>"; // id needs to be unique

while($row = mysql_fetch_array( $result ))
{
    echo "<option value=\"{$name}\"";
    echo ($_POST['product_color'] == $name) ? " selected=\"selected\"" : "";
    echo ">{$name} ({$$price})</option>";
}

Re: Keeping dropdown selections after submission

Posted: Thu Apr 28, 2011 11:19 am
by bemaitea
That's awesome! Thanks! :D

Code works well in keeping the selection the same after submssion.

Only problem is now it's not printing the prices associated with the options.

This bit of code doesn't seem to function right, but I can't see the problem??

Code: Select all

echo ">{$name} ({$$price})</option>";
When run, the page ouputs selections that look like: Red ()

It's strange, even the nonoperational '$' sign within the parentheses is not printing...

Any ideas?

Re: Keeping dropdown selections after submission

Posted: Thu Apr 28, 2011 11:29 am
by Celauran
My bad. Couple of typos on my part.

Code: Select all

$result = mysql_query("SELECT * FROM special2 WHERE cat = 'color' ORDER BY cat") or die(mysql_error());  
echo "<div id='color'><select id='color' name='product_color'>"; // id needs to be unique

while($row = mysql_fetch_array( $result ))
{
    echo "<option value=\"{$row['name']}\"";
    echo ($_POST['product_color'] == $row['name']) ? " selected=\"selected\"" : "";
    echo ">{$row['name']} (\${$row['price']})</option>";
}

Re: Keeping dropdown selections after submission

Posted: Thu Apr 28, 2011 12:22 pm
by bemaitea
You, sir, are awesome. :D

Final code, for posterity:

Code: Select all

 <form action="build22.php" method="post">
 <input type="hidden" name="data" value="1" />

 <?php
 // GRAB DATA
 $result = mysql_query("SELECT * FROM special2 WHERE cat = 'color' ORDER BY cat") 
or die(mysql_error());  
 echo "<div id='color'><select id='color' name='product_color'>";
 
while($row = mysql_fetch_array( $result )) {
 $name= $row["name"];
 $cat= $row["cat"];
 $price= $row["price"];
 echo "<option value=\"{$row['name']}\"";
 echo ($_POST['product_color'] == $row['name']) ? " selected=\"selected\"" : "";
 echo ">{$row['name']} (\${$row['price']})</option>";
 }
echo "</select></div>";
 ?>
 
<input type="submit" value="Update Configuration">
</form>
That was real ace, Celauran! Thanks again!

Re: Keeping dropdown selections after submission

Posted: Thu Apr 28, 2011 2:03 pm
by Celauran

Code: Select all

$name = $row['name'];
And similar are redundant since you can just as easily refer to $row['name'] directly. Just $0.02

Re: Keeping dropdown selections after submission

Posted: Fri Apr 29, 2011 11:33 am
by bemaitea
I did try the code without the reference, but after I commented out that bit, the code stopped working properly. Removed it altogether and same issue occured.

Have no idea why, but had to keep that top bit in there for it to function properly.