Home > Enterprise >  PHP - Form question population based on user answer
PHP - Form question population based on user answer

Time:10-06

So I'm trying to create a fuel calculator form using PHP and HTML. For the most part, I have the form pretty fleshed out, however, I'm stuck on one thing.

So I would like to create a fuel perks option that gets input from the user. The question that I wanted to pose to the user is whether or not they purchased groceries. If the user types yes, then I wanted a different input box to appear asking the user for the amount in groceries that they spent and then calculate the fuel points based on that input. However, I can't figure out how to get the initial question of how much did they spend on groceries to appear based on their answer.

Here is what I have so far:

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        How many miles did you drive? <input type="text" name="milesDriven" value="<?php echo $milesDriven;?>">
        <span class="error">* <?php echo $milesDrivenErr;?></span>
        <br><br>
        What is your cars miles per gallon?: 
        <input type="text" name="milesPerGallon" value="<?php echo $milesPerGallon;?>">
        <span class="error">* <?php echo $milesPerGallonErr;?></span>
        <br><br>
        Type of gas used: 
        <br><br>
        <input type="radio" name="gasType" <?php if (isset($gasType) && $gasType=="87 octane - 1.89$/gal") echo "checked";?> value="1.89">87 octane - 1.89$/global
        <br><br>
        <input type="radio" name="gasType" <?php if (isset($gasType) && $gasType=="89 octane - 1.99$/gal") echo "checked";?> value="1.99">89 octane - 1.99$/global
        <br><br>
        <input type="radio" name="gasType" <?php if (isset($gasType) && $gasType=="92 octane - 2.09$/gal") echo "checked";?> value="2.09">92 octane - 2.09$/global
        <br><br>
        <input type="text" name="groceries" 
            <?php
                if ($groceries = "yes") {
                    //code here that causes the input field to appear
                    
                }else if ($groceries = "no") {
                    //do nothing and proceed with the calculation
                }
            ?>
        <input type="submit" name="submit" value="Submit">
    </form>

Can you create form inputs inside of a PHP block? Please forgive me if this is a dumb question. I'm still a beginner who's trying to learn more about backend coding.

CodePudding user response:

Create a select tag inside the if block of yes and then calculate the amount the user has spend to purchase the groceries.

CodePudding user response:

Can you create form inputs inside of a PHP block ? yes of course, you can just simply doing it like this

<?php
  if ($groceries == "yes") {
   echo "<input type='text' name='purchasedGroceries' value='".$purchasedGroceries."'">   
  } 
?>

and calculate the fuel points when $groceries == "yes"

CodePudding user response:

Hijacking as answer as I cannot create a comment yet due to low rep. Answer by @adam should work. Just echo the html code or use javascript. U can also refer here.

  • Related