Jump to content

Recommended Posts

Need some help to define the php logic to echo back a selection if/when chosen from the drop down menu.

Note: I'm using just a small subset of the USA states to show setup and what I'm trying to accomplish - it will be the same for all the other drop down menus as well....

 

Here's the example code:

 


<form name="form1" method="post" action="<?php $_SERVER[php_SELF]; ?>">
<select name="address_state" id="address_state" tabindex="25">
<option value="" selected> - Select State -</option>
<option value="AK">Alaska</option>
<option value="AL">Alabama</option>
<option value="AR">Arkansas</option>
<option value="AZ">Arizona</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DC">District of Columbia</option>
</select>

<input name="submit" type="submit" value="submit" tabindex="900" id="submit" />
</form>

 

Issue: I have validation (logic) define to check all the (other) text, radio, and check boxes of the form and that all works great! However, any time one of those particular fields fail validation, any selected "drop down" item is lost - it defaults back to no selection and the user has to make all drop down menu selections again.

 

Can someone provide method to echo back - in php being the page reloads to itself - the drop down selection as in the example above?

Thanks!

 

rough (not proof-read)...

 

<option value="" <?PHP if($_POST[address_state] == "") { echo "SELECTED"; ?>> - Select State -</option>
<option value="AK" <?PHP if($_POST[address_state] == "AK") { echo "SELECTED"; ?>>Alaska</option>
<option value="AL"  <?PHP if($_POST[address_state] == "AL") { echo "SELECTED"; ?>>Alabama</option>

Stop, stop, stop. Hold the presses (or in this case the http response.)

 

You would never hardcode more than about two lines of repetitive code that only differ in the data value they operate on.

 

You need to use an array that defines the abbreviation/value and the display name. Then simply loop over that array to produce the <option></option> list. Inside that loop you would test the submitted value and output the html needed to cause the current option to be selected.

 

Also, the correct syntax to specify which option is selected is - selected="selected"

Thanks for both your answers - The logic works exact! (appreciate, litebearer).

And, "yes, I actually created an array for all and looped thru - defining each line of code with the various elements (in this case the USA states).

 

Thanks again - I just had a brain freeze on coding it for he dd menu.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.