Forum Moderators: coopster

Message Too Old, No Replies

Keeping dropdown list selection during form submission.

dropdown list and forms with php and html

         

Madmaxx68

9:23 pm on Aug 24, 2005 (gmt 0)

10+ Year Member



Sorry if I missed anything about this. I have been searching high and low!
I have a very nice form and it works the way I want it to. My problem is that when a selection is made from one of the dropdown lists and if the form is incomplete when it is submitted all the data except what was in the dropdown lists is still there so the use does not have to re-type, but if they enter the missing data but do not notice the dropdown lists and they re-submit they will get an error.
Long description for a short question.
How can I make my dropdown lists keep the selections that are made until the form has been properly submitted.

Thanks for any help.

<?
$form_block = "
<FORM METHOD=\"POST\" ACTION=\"$_SERVER[PHP_SELF]\">

<p><strong><font color=\"red\">*</font>Credit Card:</strong>
<SELECT NAME=\"credit_card\" VALUE=\"$_POST[credit_card]\">
<option selected></option>
<OPTION VALUE=\"Visa\">Visa</OPTION>
<OPTION VALUE=\"MasterCard\">MasterCard</OPTION>
</SELECT></p>

<p><strong><font color=\"red\">*</font>Card Number:</strong>
<INPUT type=\"text\" NAME=\"card_num\"
VALUE=\"$_POST[card_num]\" SIZE=16></p>

<p><strong>Expiration Date:</strong>&nbsp;&nbsp;&nbsp;<strong>Month</strong>
<SELECT NAME=\"exp_month\" OPTION VALUE=\"$_POST[exp_month]\">
<option selected></option>
<OPTION VALUE=\"01\">01</OPTION>
<OPTION VALUE=\"02\">02</OPTION>
<OPTION VALUE=\"03\">03</OPTION>
<OPTION VALUE=\"04\">04</OPTION>
<OPTION VALUE=\"05\">05</OPTION>
<OPTION VALUE=\"06\">06</OPTION>
<OPTION VALUE=\"07\">07</OPTION>
<OPTION VALUE=\"08\">08</OPTION>
<OPTION VALUE=\"09\">09</OPTION>
<OPTION VALUE=\"10\">10</OPTION>
<OPTION VALUE=\"11\">11</OPTION>
<OPTION VALUE=\"12\">12</OPTION>
</SELECT>&nbsp;&nbsp;<strong>Year</strong>

<SELECT NAME=\"exp_year\" OPTION VALUE=\"$_POST[exp_year]\">
<option selected></option>
<OPTION VALUE=\"2005\">2005</OPTION>
<OPTION VALUE=\"2006\">2006</OPTION>
<OPTION VALUE=\"2007\">2007</OPTION>
<OPTION VALUE=\"2008\">2008</OPTION>
<OPTION VALUE=\"2009\">2009</OPTION>
<OPTION VALUE=\"2010\">2010</OPTION>
<OPTION VALUE=\"2011\">2011</OPTION>
<OPTION VALUE=\"2012\">2012</OPTION>
<OPTION VALUE=\"2013\">2013</OPTION>
<OPTION VALUE=\"2014\">2014</OPTION>
<OPTION VALUE=\"2015\">2015</OPTION>
<OPTION VALUE=\"2016\">2016</OPTION>
<OPTION VALUE=\"2017\">2017</OPTION>
<OPTION VALUE=\"2018\">2018</OPTION>
<OPTION VALUE=\"2019\">2019</OPTION>
<OPTION VALUE=\"2020\">2020</OPTION>
</SELECT></p>

[edited by: jatar_k at 2:33 pm (utc) on Aug. 25, 2005]
[edit reason] removed all extra code [/edit]

dreamcatcher

6:52 am on Aug 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Madmaxx68, welcome to Webmaster World. :)

What you need here is called a Ternary Operator. Also, you have added the option value to the select tag, which isn`t correct. So, your drop downs should be like this:


<SELECT NAME=\"exp_month\">
<option selected></option>
<OPTION".(isset($_POST[exp_month]) && $_POST[exp_month] == '01'? ' selected ' : ' ')."VALUE=\"01\">01</OPTION>
<OPTION".(isset($_POST[exp_month]) && $_POST[exp_month] == '02'? ' selected ' : ' ')."VALUE=\"02\">02</OPTION>
<OPTION".(isset($_POST[exp_month]) && $_POST[exp_month] == '03'? ' selected ' : ' ')."VALUE=\"03\">03</OPTION>
<OPTION".(isset($_POST[exp_month]) && $_POST[exp_month] == '04'? ' selected ' : ' ')."VALUE=\"04\">04</OPTION>
<OPTION".(isset($_POST[exp_month]) && $_POST[exp_month] == '05'? ' selected ' : ' ')."VALUE=\"05\">05</OPTION>
<OPTION".(isset($_POST[exp_month]) && $_POST[exp_month] == '06'? ' selected ' : ' ')."VALUE=\"06\">06</OPTION>
<OPTION".(isset($_POST[exp_month]) && $_POST[exp_month] == '07'? ' selected ' : ' ')."VALUE=\"07\">07</OPTION>
<OPTION".(isset($_POST[exp_month]) && $_POST[exp_month] == '08'? ' selected ' : ' ')."VALUE=\"08\">08</OPTION>
<OPTION".(isset($_POST[exp_month]) && $_POST[exp_month] == '09'? ' selected ' : ' ')."VALUE=\"09\">09</OPTION>
<OPTION".(isset($_POST[exp_month]) && $_POST[exp_month] == '10'? ' selected ' : ' ')."VALUE=\"10\">10</OPTION>
<OPTION".(isset($_POST[exp_month]) && $_POST[exp_month] == '11'? ' selected ' : ' ')."VALUE=\"11\">11</OPTION>
<OPTION".(isset($_POST[exp_month]) && $_POST[exp_month] == '12'? ' selected ' : ' ')."VALUE=\"12\">12</OPTION>
</SELECT>&nbsp;&nbsp;<strong>Year</strong>
<SELECT NAME=\"exp_year\">
<option selected></option>
<OPTION".(isset($_POST[exp_year]) && $_POST[exp_year] == '2005'? ' selected ' : ' ')."VALUE=\"2005\">2005</OPTION>
<OPTION".(isset($_POST[exp_year]) && $_POST[exp_year] == '2006'? ' selected ' : ' ')."VALUE=\"2006\">2006</OPTION>
<OPTION".(isset($_POST[exp_year]) && $_POST[exp_year] == '2007'? ' selected ' : ' ')."VALUE=\"2007\">2007</OPTION>
<OPTION".(isset($_POST[exp_year]) && $_POST[exp_year] == '2008'? ' selected ' : ' ')."VALUE=\"2008\">2008</OPTION>
<OPTION".(isset($_POST[exp_year]) && $_POST[exp_year] == '2009'? ' selected ' : ' ')."VALUE=\"2009\">2009</OPTION>
<OPTION".(isset($_POST[exp_year]) && $_POST[exp_year] == '2010'? ' selected ' : ' ')."VALUE=\"2010\">2010</OPTION>
<OPTION".(isset($_POST[exp_year]) && $_POST[exp_year] == '2011'? ' selected ' : ' ')."VALUE=\"2011\">2011</OPTION>
<OPTION".(isset($_POST[exp_year]) && $_POST[exp_year] == '2012'? ' selected ' : ' ')."VALUE=\"2012\">2012</OPTION>
<OPTION".(isset($_POST[exp_year]) && $_POST[exp_year] == '2013'? ' selected ' : ' ')."VALUE=\"2013\">2013</OPTION>
<OPTION".(isset($_POST[exp_year]) && $_POST[exp_year] == '2014'? ' selected ' : ' ')."VALUE=\"2014\">2014</OPTION>
<OPTION".(isset($_POST[exp_year]) && $_POST[exp_year] == '2015'? ' selected ' : ' ')."VALUE=\"2015\">2015</OPTION>
<OPTION".(isset($_POST[exp_year]) && $_POST[exp_year] == '2016'? ' selected ' : ' ')."VALUE=\"2016\">2016</OPTION>
<OPTION".(isset($_POST[exp_year]) && $_POST[exp_year] == '2017'? ' selected ' : ' ')."VALUE=\"2017\">2017</OPTION>
<OPTION".(isset($_POST[exp_year]) && $_POST[exp_year] == '2018'? ' selected ' : ' ')."VALUE=\"2018\">2018</OPTION>
<OPTION".(isset($_POST[exp_year]) && $_POST[exp_year] == '2019'? ' selected ' : ' ')."VALUE=\"2019\">2019</OPTION>
<OPTION".(isset($_POST[exp_year]) && $_POST[exp_year] == '2020'? ' selected ' : ' ')."VALUE=\"2020\">2020</OPTION>
</SELECT></p>

I would use a loop for the years personally, but its personal choice.

Try that.

dc

Madmaxx68

3:21 pm on Aug 25, 2005 (gmt 0)

10+ Year Member



Thanks dreamcatcher!
That was exactly what I needed!

I am not to clear about the loop though. Could you provide an example or elaborate on the concept.

I am sure you can tell by what you have seen that I am very new to this. No "REAL" programming background is putting me on a enormous learning curve!

Thanks again!

dreamcatcher

3:40 pm on Aug 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No problem.

As for the loops, oh nothing major, its just that instead of showing this:


<OPTION VALUE=\"01\">01</OPTION>
<OPTION VALUE=\"02\">02</OPTION>
<OPTION VALUE=\"03\">03</OPTION>
<OPTION VALUE=\"04\">04</OPTION>
<OPTION VALUE=\"05\">05</OPTION>
<OPTION VALUE=\"06\">06</OPTION>
<OPTION VALUE=\"07\">07</OPTION>
<OPTION VALUE=\"08\">08</OPTION>
<OPTION VALUE=\"09\">09</OPTION>
<OPTION VALUE=\"10\">10</OPTION>
<OPTION VALUE=\"11\">11</OPTION>
<OPTION VALUE=\"12\">12</OPTION>

I would do this:


for ($i=1; $i<13; $i++)
{
echo '<OPTION VALUE="'.$i.'">'.$i.'</OPTION>';
}

Its just me being lazy I suppose. Nothing to worry about really.

:)