Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

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

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
dmalovich
Asked:
dmalovich
  • 9
  • 9
  • 3
1 Solution
 
carsRSTCommented:
0
 
dmalovichAuthor Commented:
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
 
jaxbrianCommented:
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
carsRSTCommented:
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
 
dmalovichAuthor Commented:
When I submit the page I want the users input to show up in the select box.
0
 
carsRSTCommented:
>>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
 
carsRSTCommented:
"views" can be what ever value you want to name your session variable.

$_SESSION['views']
0
 
dmalovichAuthor Commented:
How do I get 'SELECTED'  to show up on the one the person selected from the dropdown box?
0
 
carsRSTCommented:
>>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
 
dmalovichAuthor Commented:
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
 
carsRSTCommented:
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
 
dmalovichAuthor Commented:
How do I build it dynamically?  Can you give an example of how this is done in the select statements example I provided?
0
 
dmalovichAuthor Commented:
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
 
jaxbrianCommented:
Where are you trying to dip lay the information after you submit the form?
0
 
jaxbrianCommented:
display
0
 
dmalovichAuthor Commented:
to jaxbrian.  I want it to display in the drop down box.
0
 
carsRSTCommented:
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
 
dmalovichAuthor Commented:
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
 
carsRSTCommented:
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
 
dmalovichAuthor Commented:
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
 
carsRSTCommented:
My apologies, dmalovich.

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

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 9
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now