save drop down list seelcted value in page load

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
greedyisg00d
Forum Commoner
Posts: 42
Joined: Thu Feb 12, 2009 2:48 am

save drop down list seelcted value in page load

Post 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
wpsd2006
Forum Commoner
Posts: 66
Joined: Wed Jan 07, 2009 12:43 am

Re: save drop down list seelcted value in page load

Post 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
greedyisg00d
Forum Commoner
Posts: 42
Joined: Thu Feb 12, 2009 2:48 am

Re: save drop down list seelcted value in page load

Post 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
Dinosoles
Forum Newbie
Posts: 8
Joined: Fri Feb 20, 2009 1:07 am

Re: save drop down list seelcted value in page load

Post 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.
greedyisg00d
Forum Commoner
Posts: 42
Joined: Thu Feb 12, 2009 2:48 am

Re: save drop down list seelcted value in page load

Post 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?
nmreddy
Forum Commoner
Posts: 25
Joined: Wed Feb 18, 2009 5:36 am

Re: save drop down list seelcted value in page load

Post 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...
Dinosoles
Forum Newbie
Posts: 8
Joined: Fri Feb 20, 2009 1:07 am

Re: save drop down list seelcted value in page load

Post 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.
Post Reply