n1concepts Posted December 26, 2011 Share Posted December 26, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/253847-need-to-capture-echo-back-drop-down-menu-selection/ Share on other sites More sharing options...
litebearer Posted December 26, 2011 Share Posted December 26, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/253847-need-to-capture-echo-back-drop-down-menu-selection/#findComment-1301393 Share on other sites More sharing options...
PFMaBiSmAd Posted December 26, 2011 Share Posted December 26, 2011 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" Quote Link to comment https://forums.phpfreaks.com/topic/253847-need-to-capture-echo-back-drop-down-menu-selection/#findComment-1301395 Share on other sites More sharing options...
n1concepts Posted December 26, 2011 Author Share Posted December 26, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253847-need-to-capture-echo-back-drop-down-menu-selection/#findComment-1301396 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.