Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I keep user input in a drop down select field in an html form?

Posted on 2011-09-08
21
Medium Priority
?
206 Views
Last Modified: 2012-05-12
I have this form.  How can I keep the users data after the form is submitted?
<form name="duty" action="index.php" method="POST">              
        <select name="province"  onchange=""> 
		     <option value="">Select Province</option>
			<option value="Alberta">Alberta</option>
            <option value="British Columbia">British Columbia</option>
            <option value="Manitoba">Manitoba</option>
            <option value="New Brunswick">New Brunswick</option>
            <option value="Nfld.">Nfld.</option>
            <option value="Nova Scotia">Nova Scotia</option>
            <option value="Ontario">Ontario</option>    
            <option value="P.E.I.">P.E.I.</option>
            <option value="Quebec">Quebec</option>
            <option value="Saskatchewan">Saskatchewan</option>
            <option value="N.W.T.">N.W.T.</option>
            <option value="Nunavut">Nunavut</option>
            <option value="Yukon">Yukon</option>
        </select><br /><br />
                                          
        <select name="alcohol_type" size="1"
        onchange="setOptions(document.duty.alcohol_type.options[document.duty.alcohol_type.selectedIndex].value);">
            <option value="0">Type Of Alcohol</option>
            <option value="1">Spirits</option>
            <option value="2">Spirit Coolers</option>
            <option value="3">Wine</option>
            <option value="4">Wine Coolers</option>
            <option value="5">Sparkling Wine</option>
            <option value="6">Cider</option>
            <option value="7">Beer</option>    
        </select><br /><br />
        
        Cost $CDN:<br /><input size="10" type="text" name="cost" value="<?php echo isset($_POST['cost']) ? $_POST['cost'] : '' ?>" /><br /><br />
                
        <select id="typeSpirits" name="quantity" size="1">
            <option value=" " selected="selected">Select Quantity</option>
        </select><br /><br />
        
        Alcohol %:<br /> <input size="10" type="text" name="percentage" value="<?php echo isset($_POST['percentage']) ? $_POST['percentage'] : '' ?>" /><br /><br />
        
        <select name="spirits_type">
            <option value="">Type of Spirits</option>
            <option value="Whisky">Whisky</option>
            <option value="Rum">Rum</option>
            <option value="Vodka">Vodka</option>
            <option value="Tequila">Tequila</option>
            <option value="Gin">Gin</option>
            <option value="Liqueurs">Liqueurs</option>
            <option value="Brandy/Cognac">Brandy/Cognac</option>
            <option value="Other">Other</option>
        </select><br /><br />
                
        <select name="outside_country">
            <option value="">Made Outside Canada, USA, Mexico?</option>
            <option value="No">No</option>
            <option value="Yes">Yes</option>
        </select><br /><br />
 
<select id="bottled_in" name="bottled_in">
                  <option value="">Bottled In?</option>
                  <option value="Canada">Canada</option>
                  <option value="United States">United States</option>
                  <option value="Mexico">Mexico</option>
                  <option value="Australia">Australia</option>
                  <option value="New Zealand">New Zealand</option>
                  <option value="South Africa">South Africa</option>
                  <option value="France">France</option>
                  <option value="Other">Other</option>
            </select><br /><br />
    
        <input id="submitbutton" type="submit" name="submit1" value="Calculate Duty" tabindex="3" />     
                            
    </form>

Open in new window

0
Comment
Question by:dmalovich
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 9
  • 3
21 Comments
 
LVL 16

Expert Comment

by:carsRST
ID: 36503689
0
 

Author Comment

by:dmalovich
ID: 36503717
How would I use it in the select statement?

Example:

        <select name="province"  onchange="">
                  <option value="Alberta">Alberta</option>
            <option value="British Columbia">British Columbia</option>
            <option value="Manitoba">Manitoba</option>
            <option value="New Brunswick">New Brunswick</option>
            <option value="Nfld.">Nfld.</option>
            <option value="Nova Scotia">Nova Scotia</option>
            <option value="Ontario">Ontario</option>    
            <option value="P.E.I.">P.E.I.</option>
            <option value="Quebec">Quebec</option>
            <option value="Saskatchewan">Saskatchewan</option>
            <option value="N.W.T.">N.W.T.</option>
            <option value="Nunavut">Nunavut</option>
            <option value="Yukon">Yukon</option>
        </select>

0
 
LVL 2

Expert Comment

by:jaxbrian
ID: 36503899
What is it that you are trying to do?
$_SESSION['province'];

if you needed that at a different page you could just echo that where you need it.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 16

Expert Comment

by:carsRST
ID: 36503980
Test if session is set.  

if(isset($_SESSION['views']))

If it is then use the "selected" parameter of your options.

http://www.htmlcodetutorial.com/forms/_OPTION_SELECTED.html
0
 

Author Comment

by:dmalovich
ID: 36503983
When I submit the page I want the users input to show up in the select box.
0
 
LVL 16

Expert Comment

by:carsRST
ID: 36504017
>>When I submit the page I want the users input to show up in the select box.

God it...use a session variable, set the selected value to that session variable.  On the page with the select options, determine which option matches the session variable, then use the "selected" parameter in that option.

IF ($_SESSION['views']=="New Brunswick") {
   echo ("<option value=\"New Brunswick\" SELECTED>New Brunswick</option>");
} else {
    echo ("<option value=\"New Brunswick\" >New Brunswick</option>");
}
0
 
LVL 16

Expert Comment

by:carsRST
ID: 36504028
"views" can be what ever value you want to name your session variable.

$_SESSION['views']
0
 

Author Comment

by:dmalovich
ID: 36504040
How do I get 'SELECTED'  to show up on the one the person selected from the dropdown box?
0
 
LVL 16

Expert Comment

by:carsRST
ID: 36504069
>>How do I get 'SELECTED'  to show up on the one the person selected from the dropdown box?

Use an if or switch statement to determine what value was selected.   Build your output dynamically.

Example if New Brunswick was selected:
echo ("<option value=\"New Brunswick\" SELECTED>New Brunswick</option>");
0
 

Author Comment

by:dmalovich
ID: 36504077
IF ($_SESSION['views']=="New Brunswick") {
   echo ("<option value=\"New Brunswick\" SELECTED>New Brunswick</option>");
} else {
    echo ("<option value=\"New Brunswick\" >New Brunswick</option>");
}

Does this go inside every single 'option' in the select code?  There must be a better way?
0
 
LVL 16

Expert Comment

by:carsRST
ID: 36504089
The answer to your question is to use a session variable and build your SELECT outputs dynamically.  If you're asking what the syntax in PHP is then you should break this question in to two questions.

0
 

Author Comment

by:dmalovich
ID: 36504103
How do I build it dynamically?  Can you give an example of how this is done in the select statements example I provided?
0
 

Author Comment

by:dmalovich
ID: 36504315
I tried what you said and it doesn't work. When I select something from the drop down box and then submit the form, it doesn't show what I selected.     Here is the code....

<form name="duty" action="index.php" method="POST" onSubmit="hideOnSubmit(document.duty.alcohol_type.options[document.duty.alcohol_type.selectedIndex].value);">
           
        <select name="province"  onchange="">

<?php

IF ($_SESSION['views']=="Alberta") {
   echo ("<option value=\"Alberta\" SELECTED>Alberta</option>");
} else {
    echo ("<option value=\"Alberta\" >Alberta</option>");
}
IF ($_SESSION['views']=="British Columbia") {
   echo ("<option value=\"British Columbia\" SELECTED>British Columbia</option>");
} else {
    echo ("<option value=\"British Columbia\" >British Columbia</option>");
}
IF ($_SESSION['views']=="Manitoba") {
   echo ("<option value=\"Manitoba\" SELECTED>Manitoba</option>");
} else {
    echo ("<option value=\"Manitoba\" >Manitoba</option>");
}
IF ($_SESSION['views']=="New Brunswick") {
   echo ("<option value=\"New Brunswick\" SELECTED>New Brunswick</option>");
} else {
    echo ("<option value=\"New Brunswick\" >New Brunswick</option>");
}
IF ($_SESSION['views']=="Nfld.") {
   echo ("<option value=\"Nfld.\" SELECTED>Nfld.</option>");
} else {
    echo ("<option value=\"Nfld.\" >Nfld.</option>");
}
IF ($_SESSION['views']=="Nova Scotia") {
   echo ("<option value=\"Nova Scotia\" SELECTED>Nova Scotia</option>");
} else {
    echo ("<option value=\"Nova Scotia\" >Nova Scotia</option>");
}
IF ($_SESSION['views']=="Ontario") {
   echo ("<option value=\"Ontario\" SELECTED>Ontario</option>");
} else {
    echo ("<option value=\"Ontario\" >Ontario</option>");
}
IF ($_SESSION['views']=="P.E.I.") {
   echo ("<option value=\"P.E.I.\" SELECTED>P.E.I.</option>");
} else {
    echo ("<option value=\"P.E.I.\" >P.E.I.</option>");
}
IF ($_SESSION['views']=="Quebec") {
   echo ("<option value=\"Quebec\" SELECTED>Quebec</option>");
} else {
    echo ("<option value=\"Quebec\" >Quebec</option>");
}
IF ($_SESSION['views']=="Saskatchewan") {
   echo ("<option value=\"Saskatchewan\" SELECTED>Saskatchewan</option>");
} else {
    echo ("<option value=\"Saskatchewan\" >Saskatchewan</option>");
}

IF ($_SESSION['views']=="N.W.T.") {
   echo ("<option value=\"N.W.T.\" SELECTED>N.W.T.</option>");
} else {
    echo ("<option value=\"N.W.T.\" >N.W.T.</option>");
}

IF ($_SESSION['views']=="Nunavut") {
   echo ("<option value=\"Nunavut\" SELECTED>Nunavut</option>");
} else {
    echo ("<option value=\"Nunavut\" >Nunavut</option>");
}

IF ($_SESSION['views']=="Yukon") {
   echo ("<option value=\"Yukon\" SELECTED>Yukon</option>");
} else {
    echo ("<option value=\"Yukon\" >Yukon</option>");
}

?></select><br /><br />
0
 
LVL 2

Expert Comment

by:jaxbrian
ID: 36504325
Where are you trying to dip lay the information after you submit the form?
0
 
LVL 2

Expert Comment

by:jaxbrian
ID: 36504326
display
0
 

Author Comment

by:dmalovich
ID: 36504330
to jaxbrian.  I want it to display in the drop down box.
0
 
LVL 16

Expert Comment

by:carsRST
ID: 36504374
dmalovich,

Question has been answered - what you're now asking is for a tutorial in PHP which deviates greatly from your original question.  My suggestion would be to read up on setting session variables.  There are a number of steps.  See the link I posted.
0
 

Author Comment

by:dmalovich
ID: 36504461
I'm sorry carsRST, I'm just looking for the solution to my problem.  Giving me the link to something hasn't solved it.  I've tried the code you suggested and it doesn't work, so I don't see how my question has been answered.  I'm looking for some help.....
0
 
LVL 16

Accepted Solution

by:
carsRST earned 1500 total points
ID: 36504525
On your index.php page you have to set the session variable to what ever the user selected.


<<index.php>>
<?PHP

session_start();
$_SESSION['province'] = $_POST['province'];


//...rest of code on index.php page...
?>


Then try using something along the logic you've already posted to test which value is in the session variable.
0
 

Author Closing Comment

by:dmalovich
ID: 36504587
Thanks carsRST.  I changed the code for each option to:

IF ($_POST['province']=="Alberta") {
   echo ("<option value=\"Alberta\" SELECTED>Alberta</option>");
} else {
    echo ("<option value=\"Alberta\" >Alberta</option>");
}

and it worked.  Thanks........
0
 
LVL 16

Expert Comment

by:carsRST
ID: 36504705
My apologies, dmalovich.

I didn't realize you were calling the same page from where submitted.  Something I should have asked.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question