【问题标题】:PHP - Form question population based on user answerPHP - 根据用户回答形成问题群体
【发布时间】:2021-11-26 05:49:32
【问题描述】:

所以我正在尝试使用 PHP 和 HTML 创建一个燃料计算器表单。在大多数情况下,我的表格非常充实,但是,我被困在一件事上。

所以我想创建一个获取用户输入的燃料津贴选项。我想向用户提出的问题是他们是否购买了杂货。如果用户输入是,那么我希望出现一个不同的输入框,询问用户他们花费的杂货数量,然后根据该输入计算燃料点。但是,我不知道如何根据他们的回答来获得最初的问题,即他们在杂货上花了多少钱。

这是我目前所拥有的:

<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>

您可以在 PHP 块内创建表单输入吗?如果这是一个愚蠢的问题,请原谅我。我仍然是一名初学者,正在尝试更多地了解后端编码。

【问题讨论】:

    标签: php html forms user-input


    【解决方案1】:

    在“是”的 if 块内创建一个选择标签,然后计算用户购买杂货所花费的金额。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:

    你能在 PHP 块中创建表单输入吗? 当然可以,你可以像这样简单地做

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

    并计算$groceries == "yes"时的加油点

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多