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
save drop down list seelcted value in page load
Moderator: General Moderators
-
greedyisg00d
- Forum Commoner
- Posts: 42
- Joined: Thu Feb 12, 2009 2:48 am
Re: save drop down list seelcted value in page load
$_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
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
-
greedyisg00d
- Forum Commoner
- Posts: 42
- Joined: Thu Feb 12, 2009 2:48 am
Re: save drop down list seelcted value in page load
Sorry I am a newbie to php, this is my drop down list code can you help me on how to code it?
Thanks
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>Re: save drop down list seelcted value in page load
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...
Hope this helps.
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>
-
greedyisg00d
- Forum Commoner
- Posts: 42
- Joined: Thu Feb 12, 2009 2:48 am
Re: save drop down list seelcted value in page load
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
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...
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
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...
Hope this helps.
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>