Page 1 of 1

Form question involving dropdown boxes

Posted: Fri Nov 06, 2009 6:15 am
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>";
?>
 

Re: Form question involving dropdown boxes

Posted: Fri Nov 06, 2009 6:24 am
by VladSun
Change the corresponding OPTION line, so it includes a SELECTED attribute:
http://www.w3schools.com/TAGS/att_option_selected.asp

Re: Form question involving dropdown boxes

Posted: Fri Nov 06, 2009 6:30 am
by jefffan24
But that will just reset the page to the option with selected="selected" not which one the user picks.

Re: Form question involving dropdown boxes

Posted: Fri Nov 06, 2009 6:33 am
by VladSun
You should use *PHP* to include it to the corresponding line according to the data in your $_GET variable.

Re: Form question involving dropdown boxes

Posted: Fri Nov 06, 2009 6:42 am
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.

Re: Form question involving dropdown boxes

Posted: Fri Nov 06, 2009 6:48 am
by VladSun
You need conditional logic ... Like this:

Code: Select all

if ($color == "green.jpg")
    echo "User selected green.jpg";

Re: Form question involving dropdown boxes

Posted: Fri Nov 06, 2009 6:59 am
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.

Re: Form question involving dropdown boxes

Posted: Fri Nov 06, 2009 7:07 am
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.