How to assign value to variable based on option value using "IF" condition?

I am using the dropdown in form, based on the option value,how to assign value to a variable using “IF” condition,

one.php:

            <div class="form-group">
                    <label class="control-label col-sm-2" for="i_have">I have :</label>
                    <div class="col-sm-10">  
                        <select id="old" class="form-control" placeholder="Select I have" name="i_have" onchange="kolu_padi_selection()" required>

                            <option value = "select_option">Select Option</option>
                            <option value = "three_compact" data-id="3">3 Compact</option>
                            <option value = "three_regular" data-id="3">3 Regular</option>
                            <option value = "three_triple"  data-id="3">3 Triple</option>
                            <option value = "five_compact" data-id="5">5 Compact</option>
                            <option value = "five_regular" data-id="5">5 Regular</option>
                            <option value = "five_triple" data-id="5">5 Triple</option>
                            <option value = "seven_compact" data-id="7">7 Compact</option>
                            <option value = "seven_regular" data-id="7">7 Regular</option>
                            <option value = "seven_triple" data-id="7">7 Triple</option>
                            <option value = "nine_compact" data-id="9">9 Compact</option>
                            <option value = "nine_regular">9 Regular</option>
                            <option value = "nine_triple">9 Triple</option>   
                        </select>
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label col-sm-2" for="i_want">I want :</label>
                    <div class="col-sm-10">  
                        <select id="new" class="form-control" placeholder="Select I want" name="i_want" required>
                            <option value = "select_option">Select Option</option>    
                            <option value = "three_compact" data-id="3">3 Compact</option>
                            <option value = "three_regular" data-id="3">3 Regular</option>
                            <option value = "three_triple"  data-id="3">3 Triple</option>
                            <option value = "five_compact" data-id="5">5 Compact</option>
                            <option value = "five_regular" data-id="5">5 Regular</option>
                            <option value = "five_triple" data-id="5">5 Triple</option>
                            <option value = "seven_compact" data-id="7">7 Compact</option>
                            <option value = "seven_regular" data-id="7">7 Regular</option>
                            <option value = "seven_triple" data-id="7">7 Triple</option>
                            <option value = "nine_compact" data-id="9">9 Compact</option>
                            <option value = "nine_regular">9 Regular</option>
                            <option value = "nine_triple">9 Triple</option>  
                        </select>
                    </div>
                </div>

Two.php:

if(isset($_POST['i_have']) && isset($_POST['i_want']) && isset($_POST['delivery_zip']) && isset($_POST['delivery_country']))
    {
    $i_have = $_POST['i_have'];
    $i_want = $_POST['i_want'];
	 $delivery_zip = $_POST['delivery_zip'];
	 $delivery_country = $_POST['delivery_country'];
 
    }

if($i_have == "three_compact" && $i_want="five_compact")
{
    $productId =  '14419';
    $results = getShippingEstimate($productId,'1',$delivery_country,$delivery_zip);
}

elseif($i_have == "three_compact" && $i_want="five_compact")
{
    $productId =  '14420';
    $results = getShippingEstimate($productId,'1',$delivery_country,$delivery_zip);
}

FYI => $results using products_id, 1, delivery_country, delivery_zip, how can i get product id using if statement.

If my workout wrong, make me a pardon, I just learning stage in PHP.

If all you are doing is mapping input values to output values, you don’t use conditional logic. You store the mapping in a data structure (array or database table) and simply ‘look up’ the value.

Your posted example, in addition to showing the same inputs values for different output values, doesn’t have two == in the $i_want comparison, so won’t work.

How exactly do you know what product id corresponds with which input choice, your text value names like three_triple don’t have any context for us and should actually be id numbers, not text names.

1 Like

Let me explain my workout, i have custom input form, the form have drop-down values,how can i use my first.php [Drop-down values] values into second.php page, i am using post method. In my second.php require product id, based on my first.php i need to give my product_id, here my product_id depends on i_have & i_want drop-down values.

Manual giving code is working,

if(isset($_POST['i_have']) && isset($_POST['i_want']) && isset($_POST['delivery_zip']) && isset($_POST['delivery_country']))
    {
    $i_have = $_POST['i_have'];
    $i_want = $_POST['i_want'];
    $delivery_zip = $_POST['delivery_zip'];
    $delivery_country = $_POST['delivery_country'];
    }
$results = getShippingEstimate('14419','1',$country,$zip_postal_code); //14419 is my product_id

I/we know what you are doing. I asked how do you know what product id corresponds with which input choice. You need to show some valid examples (the example you did show has the same two input values for different product ids) for anyone here to understand the dependency.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service