Page 1 of 1
save drop down list seelcted value in page load
Posted: Thu Feb 19, 2009 9:59 pm
by greedyisg00d
I have a form which have a 1 text field and 3 drop down list in it. My problem is whenever I hit the submit button and redirect it to the same page the value of the selected drop down list is set again to default (Select one). Thus the user will need to select one by one again. Any idea or sample code so that I can analze it?
Thanks
Re: save drop down list seelcted value in page load
Posted: Thu Feb 19, 2009 11:16 pm
by wpsd2006
$_SESSION is your answer
put the $_POST or $_GET to your $_SESSION after submit ( well if you submit to the same page you can use $_POST or $_GET directly without using $_SESSION
after reload set the select element value with your $_SESSION or $_POST or $_GET
Re: save drop down list seelcted value in page load
Posted: Fri Feb 20, 2009 12:14 am
by greedyisg00d
Sorry I am a newbie to php, this is my drop down list code can you help me on how to code it?
Code: Select all
<select name="type" id="Type">
<option value="[Select one]">[Select one]</option>
<option value="Meeting">Meeting</option>
<option value="Conference">Conference</option>
<option value="Workshop">Workshop</option>
</select>
Thanks
Re: save drop down list seelcted value in page load
Posted: Fri Feb 20, 2009 1:20 am
by Dinosoles
Hi,
On the page try <?php print_r($_POST); ?> and see what the value for 'type' is, if it matches one of the options then it should be selected.
If you are using $_SESSION to store the value you will need to set this to $_POST above the form...
<?php if(isset($_SESSION['type'])) { $_POST['type'] = $_SESSION['type']; } ?>
alternatively you could do something like...
Code: Select all
<select name="type" id="Type">
<option value="Meeting" <?php if($_SESSION['type'] == 'Meeting') { echo('selected="selected"'); ?>>Meeting</option>
</select>
Hope this helps.
Re: save drop down list seelcted value in page load
Posted: Fri Feb 20, 2009 3:01 am
by greedyisg00d
Thanks your code works fine but in order to view the selected 'type' I must reload the page to get the session value. I just notice a while ago that the page does not load when the user selects an invalid selection but the selection is set again to default selected value[Select one]. Any idea on how to fix this?
Re: save drop down list seelcted value in page load
Posted: Fri Feb 20, 2009 4:39 am
by nmreddy
might be this will help, in select tag use this <select name="type" id="Type" onChange='this.form.submit();'>
in your file extract the posted values like extract($_POST)
then the variable $type have the selected value, then you can
check with your dropdown values...
Re: save drop down list seelcted value in page load
Posted: Tue Mar 03, 2009 11:58 pm
by Dinosoles
Hi,
Sessions do require an additional page refresh and there is no way around it (that I am aware of anyway).
If you really need the session to keep track of the selection but also want the the option selected immediately after the form is submitted then try this...
Code: Select all
<select name="type" id="Type">
<option value="Meeting" <?php if($_SESSION['type'] == 'Meeting' || $_POST['type'] == 'Meeting') { echo('selected="selected"'); ?>>Meeting</option>
</select>
Hope this helps.