18910140161

HTML-PHP-基于用户回答的表单问题填充-堆栈溢出

顺晟科技

2022-10-19 14:13:46

14

所以我尝试使用PHP和HTML创建一个燃料计算器表单。在很大程度上,我的表单已经非常充实了,但是,有一件事让我卡住了。

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

以下是我目前掌握的信息:

<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块中创建表单输入吗?当然可以,您可以像这样简单地做

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

并计算当

时的燃料点

在if(是)块中创建一个select标记,然后计算用户购买杂货的花费。

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航