cozzy1984 Posted February 6, 2008 Share Posted February 6, 2008 Hi was, just wondering if anyone knew how to return the value a user selects from a drop down menu if the form doesn't validate. Need this as i have the form doing various checks after the form is submitted then if there is an error they all appear and i want all the values entered/selected the first time to reappear. I have the code for returning input values value="<?php echo $_POST['username']; ?>" but just dont know what the best way to tackle the drop down menu. My code is: <fieldset> <label for="">Location:</label> <select name="location" onfocus="infotitle('Enter Location'); info('Please select the location that you live in, this will be displayed in your ads.');"> <option value="Please Select">Please Select..</option> <option value="Co.Antrim">Co.Antrim</option> <option value="Co.Armagh">Co.Armagh</option> <option value="Co.Down">Co.Down</option> <option value="Co.Fermanagh">Co.Fermanagh</option> <option value="Co.Londonderry">Co.Londonderry</option> <option value="Co.Tyrone">Co.Tyrone</option> </select> <span class="<?php echo $locationerror; ?>"><?php echo $error['location']; ?></span> </fieldset> Quote Link to comment https://forums.phpfreaks.com/topic/89789-solved-remember-drop-down-value-selected/ Share on other sites More sharing options...
rhodesa Posted February 6, 2008 Share Posted February 6, 2008 <fieldset> <label for="">Location:</label> <select name="location" onfocus="infotitle('Enter Location'); info('Please select the location that you live in, this will be displayed in your ads.');"> <option value="Please Select">Please Select..</option> <option value="Co.Antrim"<?=$_POST['location'] == 'Co.Antrim'?'selected=true ':''?>>Co.Antrim</option> <option value="Co.Armagh"<?=$_POST['location'] == 'Co.Armagh'?'selected=true ':''?>>Co.Armagh</option> <option value="Co.Down"<?=$_POST['location'] == 'Co.Down'?'selected=true ':''?>>Co.Down</option> <option value="Co.Fermanagh"<?=$_POST['location'] == 'Co.Fermanagh'?'selected=true ':''?>>Co.Fermanagh</option> <option value="Co.Londonderry"<?=$_POST['location'] == 'Co.Londonderry'?'selected=true ':''?>>Co.Londonderry</option> <option value="Co.Tyrone"<?=$_POST['location'] == 'Co.Tyrone'?'selected=true ':''?>>Co.Tyrone</option> </select> <span class="<?php echo $locationerror; ?>"><?php echo $error['location']; ?></span> </fieldset> Quote Link to comment https://forums.phpfreaks.com/topic/89789-solved-remember-drop-down-value-selected/#findComment-460073 Share on other sites More sharing options...
rhodesa Posted February 6, 2008 Share Posted February 6, 2008 It's usually easier to maintain if you do it this way: <fieldset> <label for="">Location:</label> <select name="location" onfocus="infotitle('Enter Location'); info('Please select the location that you live in, this will be displayed in your ads.');"> <option value="Please Select">Please Select..</option> <?php $location_opts = array( "Co.Antrim", "Co.Armagh", "Co.Down", "Co.Fermanagh", "Co.Londonderry", "Co.Tyrone", ); foreach($location_opts as $opt){ $selected = $_POST['location'] == $opt ? " selected=true":""; print "<option value=\"{$opt}\"{$selected}>{$opt}</option>"; } ?> </select> <span class="<?php echo $locationerror; ?>"><?php echo $error['location']; ?></span> </fieldset> Quote Link to comment https://forums.phpfreaks.com/topic/89789-solved-remember-drop-down-value-selected/#findComment-460083 Share on other sites More sharing options...
cozzy1984 Posted February 6, 2008 Author Share Posted February 6, 2008 That didn't seem to work mate. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/89789-solved-remember-drop-down-value-selected/#findComment-460096 Share on other sites More sharing options...
rhodesa Posted February 6, 2008 Share Posted February 6, 2008 If you are using the first option I sent...i noticed an error: <option value="Co.Antrim"<?=$_POST['location'] == 'Co.Antrim'?'selected=true ':''?>>Co.Antrim</option> In the above, the space should be before selected=true instead of after it. like so: <option value="Co.Antrim"<?=$_POST['location'] == 'Co.Antrim'?' selected=true':''?>>Co.Antrim</option> ...change that for all of them Quote Link to comment https://forums.phpfreaks.com/topic/89789-solved-remember-drop-down-value-selected/#findComment-460106 Share on other sites More sharing options...
cozzy1984 Posted February 6, 2008 Author Share Posted February 6, 2008 Have tried that mate. Its putting a > before each option on the page. Does it need to start <?php at the beginning of each one? Quote Link to comment https://forums.phpfreaks.com/topic/89789-solved-remember-drop-down-value-selected/#findComment-460117 Share on other sites More sharing options...
cozzy1984 Posted February 6, 2008 Author Share Posted February 6, 2008 Don't worry mate, might just use your other way of doing it as it seems to be working. Cheers for your help. Quote Link to comment https://forums.phpfreaks.com/topic/89789-solved-remember-drop-down-value-selected/#findComment-460122 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.