Sibghat
Sibghat

Reputation: 21

Declare two variables from the selected option in php

I have this project where I have to declare sub variables of lets say "width" and "height" for the respective selected option i.e. "small" or "large". For example if the user select "small" from the option then the system should save two variables "width" and "height" for "small". Your assistance for this question is highly appreciated. If you have further questions, Please do ask. Thanks

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script type="text/javascript" src="js.js"></script>
    <script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<!--name.php to be called on form submission--> 
<form method = 'post'>  
            <h4>CALCULATOR</h4> 
              
            <label>Select Plate Size</label>
            <select name = 'plateSelector'>   
                <option value = 'Small Plate' name="small">Small Plate</option> 
                <option value = 'Large Plate' name="large">Large Plate</option> 
            </select> 
            <input type = 'submit' name = 'submit' value = Submit> 
        </form> 
    
</body>
<script type="text/javascript">

</script>
</html>
<?php 
      
    // Check if form is submitted successfully 
    if(isset($_POST["submit"]))  
    { 
        // Check if any option is selected 
        if(isset($_POST["plateSelector"]))  
        { 
            $plate = $_GET["small"];
            if($plate)
            {
                $height=5;
                $width=5;
                print "$height and $width<br/>"; 
            }
            else{
                print"this"; 
            }
        } 
    else
        echo "Select an option first !!"; 
    } 
?> 

Upvotes: 0

Views: 39

Answers (1)

Ken Lee
Ken Lee

Reputation: 8043

There are many ways to do the job (for example you may store the two pieces of data in a db), but one of the simpler methods is to put the two variables (width and height) into a string and then separate them by a delimiter (e.g. ##).

For example, a string 12.2##22.2 can stand for width=12.2 and height=22.2

You may refer to the following working code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script type="text/javascript" src="js.js"></script>
    <script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<!--name.php to be called on form submission--> 
<form method ='post' action="">  
            <h4>CALCULATOR</h4> 
              
<label>Select Plate Size</label>
<select name = 'plateSelector'>   
<option value = '' >Please choose</option> 
<option value = '12.2##22.2'  >Small Plate</option> 
<option value = '100.2##99.1' >Large Plate</option> 
</select> 
<input type = 'submit' name = 'submit' value = Submit> 
        </form> 
    
</body>
<script type="text/javascript">

</script>
</html>

<?php 
      
if(isset($_POST["submit"]))  { 
   $plate = $_POST["plateSelector"];
   if($plate) {
   $pieces = explode("##", $plate);
   echo "width is: ". $pieces[0] . ",  height is: " . $pieces[1]; 
   } else {
   echo  "Please select an option"; 
   }
} 

?> 

Upvotes: 1

Related Questions