Keeping dropdown selections after submission

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
bemaitea
Forum Newbie
Posts: 9
Joined: Sun Jul 04, 2010 6:21 pm

Keeping dropdown selections after submission

Post 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! :)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Keeping dropdown selections after submission

Post 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>";
}
bemaitea
Forum Newbie
Posts: 9
Joined: Sun Jul 04, 2010 6:21 pm

Re: Keeping dropdown selections after submission

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Keeping dropdown selections after submission

Post 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>";
}
bemaitea
Forum Newbie
Posts: 9
Joined: Sun Jul 04, 2010 6:21 pm

Re: Keeping dropdown selections after submission

Post 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!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Keeping dropdown selections after submission

Post 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
bemaitea
Forum Newbie
Posts: 9
Joined: Sun Jul 04, 2010 6:21 pm

Re: Keeping dropdown selections after submission

Post 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.
Post Reply