Home > other >  can you put a value in a dropdown for a php calculator
can you put a value in a dropdown for a php calculator

Time:04-24

im trying to make a php calculator for an F1 tyre wear calculator for a project and im inexperienced with coding can you put a value in a dropdown and cover it with a label.

for example: drop down has 3 options fast, medium and slow to pick from but in the code they are assigned a value. fast = 3 medium = 2 slow = 1

<form>
<input type="text" name="num1" placeholder="E.G. 1">
<input type="text" name="num2" placeholder="E.G. 1">
<select name="operator">
    <option value="3">fast</option>
    <option value="2">medium</option>
    <option value="1">slow</option>
</select>
<br>
<button type="submit" name="submit" value="submit">Calculate</button>
</form>
<p>the answer is %</p>
<?php
if (isset($_GET['submit'])) {
    $result1 =$_GET['num1'];
    $result2 =$_GET['num2'];
    $operator =$_GET['operator'];
    switch ($operator){
        case "fast":
            echo $result1   $result2   3;
            break;

        case "medium":
            echo $result1   $result2   2;
            break;

        case "slow":
            echo $result1   $result2   1;
            break;
            
    }
}

any help is welcome

CodePudding user response:

This is more of a basic HTML funtion than anything.

First you need to make your Select dropdown list using the <select> and <option> tags like this

<form action="./submit" method="post">
    <select name="speed" required>
      <option selected disabled>Select A Speed</option>
      <option value="1">Slow</option>
      <option value="2">Medium</option>
      <option value="3">Fast</option>
    </select>
</form>

Then in your PHP code that handles the form submission you can get the value selected from the $_POST variable. (Assuming the form method is POST)

Eg.

$speed = $_POST['speed']; // This will be 1,2 or 3 depending on the selected option

CodePudding user response:

<form action="<?php echo $_SERVER['REQUEST_URI'] // Current page url?>" method="GET">
    <!--Make sure to say which method you are using!-->
    <input type="text" name="num1" placeholder="E.G. 1">
    <input type="text" name="num2" placeholder="E.G. 1">
    <select name="operator">
        <option value="3">fast</option>
        <!--The value will be what is sent not the text inside the option!--
        <option value="2">medium</option>
        <option value="1">slow</option>
    </select>
    <br>
    <button type="submit" name="submit" value="submit">Calculate</button>
    </form>
    <p>the answer is %</p>
    <?php
    if (isset($_GET['submit'])) {
        $result1 =$_GET['num1'];
        $result2 =$_GET['num2'];
        $operator =$_GET['operator'];
        switch ($operator){
            case "3":
                echo $result1   $result2   3;
                break;
    
            case "2":
                echo $result1   $result2   2;
                break;
    
            case "1":
                echo $result1   $result2   1;
                break;
                
        }
    }
    ?>
  • Related