Form question involving dropdown boxes

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
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Form question involving dropdown boxes

Post by jefffan24 »

Ok so I have a dropdown form with a little php after it. The php is rendering a picture based on what the user picks. This is working fine, but the page refreshes after the user selects their options. When it refreshes it resets their selections. Is there anyway I can stop it from resetting their selections?

So for example a person picks Medium, Orange, and Redbox they click render picture, that works fine. But when it renders their picture it resets their selection back the default: Small, Choose Color, None.

Code: Select all

 
<center><form name="blah" id="blah" method="get">
<select name="sizes" tabindex="1">
<option value="small" id="small">Small</option>
<option value="blueshirt.png" id="medium">Medium</option>
<option value="large" id="large">Large</option>
<option value="xlarge" id="xlarge">X-Large</option>
<option value="xxlarge" id="xxlarge">XX-Large</option>
</select>
&nbsp;&nbsp;
<select name="colors" tabindex="2">
<option value="" id="choose">Choose Color</option>
<option value="orange.jpg" id="orange">Orange</option>
<option value="blueshirt.png" id="blue">Blue</option>
<option value="lightblue.jpg" id="lightblue">Light Blue</option>
<option value="green.jpg" id="green">Green</option>
<option value="lightgreen.jpg" id="lightgreen">Light Green</option>
</select>
&nbsp;&nbsp;
<select name="decal" tabindex="3">
<option value="" id="none">None</option>
<option value="redbox.png" id="redBox">Red Box</option>
<option value="blueBox" id="blueBox">Blue Box</option>
</select><br /><br />
<input type="submit" value="Render Picture" tabindex="4" />
</form></center>
 
<?php
$color = $_GET['colors'];
$decal = $_GET['decal'];
print "
<div class=\"shirt\" style=\"background-image:url(shirts/$color); background-repeat:no-repeat; height:500px; margin-left:400px; margin-top:100px;\"><br />\n
<img class=\"decal\" src=\"decals/$decal\" style=\"margin-left:100px; margin-top:50px;\" />
</div>";
?>
 
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Form question involving dropdown boxes

Post by VladSun »

Change the corresponding OPTION line, so it includes a SELECTED attribute:
http://www.w3schools.com/TAGS/att_option_selected.asp
There are 10 types of people in this world, those who understand binary and those who don't
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Re: Form question involving dropdown boxes

Post by jefffan24 »

But that will just reset the page to the option with selected="selected" not which one the user picks.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Form question involving dropdown boxes

Post by VladSun »

You should use *PHP* to include it to the corresponding line according to the data in your $_GET variable.
There are 10 types of people in this world, those who understand binary and those who don't
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Re: Form question involving dropdown boxes

Post by jefffan24 »

I tried doing

Code: Select all

 
selected="<?php $_GET['colors']; ?>"
 
But whichever option I put it in it didn't work.

The for kicks and giggles i put it in the select tag and it did absolutely nothing which was expected.

Can you please give me an example of what you are talking about if I did it wrong.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Form question involving dropdown boxes

Post by VladSun »

You need conditional logic ... Like this:

Code: Select all

if ($color == "green.jpg")
    echo "User selected green.jpg";
There are 10 types of people in this world, those who understand binary and those who don't
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Re: Form question involving dropdown boxes

Post by jefffan24 »

Code: Select all

<select name="colors" tabindex="2" id="colors">
<option value="" id="colors">Choose Color</option>
<option value="orange.jpg"  id="colors" selected="<?php if($color == 'orange.jpg') echo 'selected'; ?>">Orange</option>
<option value="blueshirt.png" id="colors" selected="<?php if($color == 'blueshirt.png') echo 'selected'; ?>">Blue</option>
<option value="lightblue.jpg" id="colors" selected="<?php if($color == 'lightblue.jpg') echo 'selected'; ?>">Light Blue</option>
<option value="green.jpg" id="colors" selected="<?php if($color == 'green.jpg') echo 'selected'; ?>">Green</option>
<option value="lightgreen.jpg" id="colors" selected="<?php if($color == 'lightgreen.jpg') echo 'selected'; ?>">Light Green</option>
</select>
I tried that and it didn't work, I'm sorry this just isn't clicking for me :( I know it should be rather simple but it just isn't clicking.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Form question involving dropdown boxes

Post by VladSun »

Try

Code: Select all

<option value="orange.jpg"  id="colors" <?php if($color == 'orange.jpg') echo 'selected="selected"'; ?>">Orange</option>
SELECTED attribute may be used without assigning value (not HTML strict) - its presence is important.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply