nicole101
nicole101

Reputation: 187

How to save and use a user picked option in a dropdown list as a variable?

What I need to do is let the user pick in the dropdown list. Then when the user clicked submit, I will have an if..else statement that executes different codes depending on what the user picked in the dropdown list.

Shipping Provider: <select id='ship' name='ship'>
<option value="val1">Value1</option>
<option value="val2">Value2</option>
<option value="val3">Value3</option>
<option value="val4">Value4</option>
</select>

Then after that, I don't know on how to save it. What I'm thinking is like this:

if(isset($_POST['extract'])){

$dropdownvalue = $_POST['ship'];

if($dropdownvalue == 'Value1'){
       EXECUTE CODE HERE
}
else if($dropdownvalue == 'Value2'){
       EXECUTE CODE HERE
}
else if($dropdownvalue == 'Value3'){
       EXECUTE CODE HERE
}
else{
       EXECUTE CODE HERE
}
}

but somehow it's not working. Can anyone please tell me what's wrong?

Upvotes: 0

Views: 102

Answers (4)

Awlad Liton
Awlad Liton

Reputation: 9351

You will have value of the selected option not the text.

if(isset($_POST['extract'])){

    $dropdownvalue = $_POST['ship'];

    if($dropdownvalue == 'val1'){
           EXECUTE CODE HERE
    }
    else if($dropdownvalue == 'val2'){
           EXECUTE CODE HERE
    }
    else if($dropdownvalue == 'val3'){
           EXECUTE CODE HERE
    }
    else{
           EXECUTE CODE HERE
     }
   }

Upvotes: 1

rsakhale
rsakhale

Reputation: 1036

You may also consider making use of switch case in this case

if(isset($_POST['extract'])){
    if(isset($_POST['ship'] and !empty($_POST['ship'])){
        $dropdownvalue = $_POST['ship'];
        switch($dropdownvalue){
            case 'val1' : 
                // Code here
                break;
            case 'val2' :
                // Code here
                break;                
            case 'val3' :
                // Code here
                break;            
            case 'val4' :
                // Code here
                break;            
            default : 
                // Code here            
        }        
    }
}

Upvotes: 1

Perry
Perry

Reputation: 11710

Since you have set a value in the dropdown option it will be that value

change your code to this:

if(isset($_POST['extract'])){

 $dropdownvalue = $_POST['ship'];

 if($dropdownvalue === 'val1'){
   EXECUTE CODE HERE
 }
 else if($dropdownvalue === 'val2'){
   EXECUTE CODE HERE
 }
 else if($dropdownvalue === 'val3'){
   EXECUTE CODE HERE
 }
 else{
   EXECUTE CODE HERE
 }
}

A little explantion

When you use the value attribute on a option element the value attribtute will be used, this is also happing in your code. When you don't use a value attribute the string between the option element will be used.

so your html will look like this:

<option>string</option>

In php the value you will get is string. But when you used the value attribute you will be able to show a string that is not the same as the value you will be sending

your html looks like this:

<option value="1">String</option>

now the value you will get in php is 1, this is very useful, and in my opion you should always use the value attribute.

Upvotes: 1

Stefan
Stefan

Reputation: 528

im not sure but doesnt you need to check for the option value like

if($dropdownvalue == 'val1'){
    ....
}

you can also, for debugging, echo the value of $dropdownvalue to see what value it actually has

Upvotes: 1

Related Questions