dongshi3605 2019-03-02 13:09
浏览 53

如何将无线电类型输入绑定到PHP以进行测验

I want to make a quiz test in PHP so that it will instantly show in every page if the answer chosen by the user is correct or not. So my quiz test will be shown as a question per page, please explain and show me how to modify the following code so that I will know what to do in the other pages:

        <main>

            <div class="big-div">

                <p>1. Which is the biggest planet in our Solar System?</p>

                <form method="POST">
                    <label class="container"> A) Uranus
                         <input type="radio" name="biggestplanet" value="uranus">
                         <span class="checkmark"></span>
                    </label>

                    <label class="container"> B) Saturn
                         <input type="radio" name="biggestplanet" value="saturn">
                         <span class="checkmark"></span>
                    </label>

                    <label class="container"> C) Jupiter
                         <input type="radio" name="biggestplanet" value="jupiter">
                         <span class="checkmark"></span>
                    </label>

                    <label class="container"> D) Neptune
                         <input type="radio" name="biggestplanet" value="neptune">
                         <span class="checkmark"></span>
                    </label>

                    <input type="submit" value="Send" class="submit">


                </form>


            <?php
                $answer1 = $_POST["getAttribute('biggestplanet')"];

                if ($answer1 == "C) Jupiter") {
                    echo "<p style='color:red;font-size:20px;'>Correct answer!</p>";
                } else {
                    echo "<p style='color:red;font-size:20px;'>Wrong answer! The biggest planet in out Solar System is Jupiter!</p>";
                }
            ?>


So, briefly, this is my PHP code that I can't figure out how to write it correctly:

<?php
        $answer1 = $_POST["getAttribute('biggestplanet')"];

        if ($answer1 == "C) Jupiter") {
            echo "<p style='color:red;font-size:20px;'>Correct answer!</p>";
        } else {
            echo "<p style='color:red;font-size:20px;'>Wrong answer! The biggest planet in out Solar System is Jupiter!</p>";
        }
    ?>

I'm not for the moment interested in how to send the answers to a database, I just want to know how to show to the user if his answers are correct or not.

  • 写回答

3条回答 默认 最新

  • douqian2524 2019-03-02 13:43
    关注

    The <label> tag is typically bound to an element using the 'for' attribute, which would match the id of the element to which it should be bound. Example:

    <label for="uranus">Uranus</label>
    <input type="radio" name="biggestplanet" id="uranus" value="Uranus">
    
    <label for="saturn">Saturn</label>
    <input type="radio" name="biggestplanet" id="saturn" value="Saturn">
    
    <input type="submit" value="Submit">
    

    And then to grab the value from the input, you'll need to use the 'name' for the input tag:

    <form method="POST">
        <label for="uranus">Uranus</label>
        <input type="radio" name="biggestplanet" id="uranus" value="Uranus">
    
        <label for="saturn">Saturn</label>
        <input type="radio" name="biggestplanet" id="saturn" value="Saturn">
    
        <input type="submit" value="Submit">
    </form>
    
    <?php
    if(isset($_POST['biggestplanet'])){
        if($_POST['biggestplanet'] === 'Saturn'){
            $answer1 = $_POST['biggestplanet'];
        }elseif($_POST['biggestplanet'] === 'Uranus'){
            $answer1 = $_POST['biggestplanet'];
        }
        echo $answer1;
    }
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题