-1

我正在尝试使用 php 获取所选单选按钮的值。但是,当我尝试回显单选的值时,它会返回“开”,无论我在提交表单时选择了哪个单选按钮。

HTML:

<article>
    <form id="quiz" method="post" action="question3.php">
        <fieldset id="question">
            <legend>Question 2</legend>
            <p>Where was CoffeeScripts first stable build announced? <br />
            <label for="github">GitHub</label>
            <input type="radio" name="ans2" id="github" value="GitHub"/> <br />
            <label for="hackernews">Hacker News</label>
            <input type="radio" name="ans2" id="hackernews" value="Hacker News"/> <br />
            <label for="coffeescript">CoffeeScript.com</label>
            <input type="radio" name="ans2" id="coffeescript" value="CoffeeScript"/> <br />
            <label for="dropbox">DropBox</label>
            <input type="radio" name="ans2" id="dropbox" value="Dropbox"/> <br />
        </fieldset>
        <p><input type="submit" value="Submit Answer" id="subans" /></p>
    </form>
</article>

然后在流程页面上:

<?php

if (isset($_POST['ans2'])) {
    $ans = $_POST['ans2'];
    echo "<p>$ans</p>";
} 
else {
    echo "Nothing was selected.";
}

?>

就像我说的,这只是在页面上输出“on”。

帮助表示赞赏!

4

1 回答 1

0

Can you try this, Added isset($_POST['subans']) in PHP block

        <article>
            <form id="quiz" method="post" action="question3.php">
                <fieldset id="question">
                    <legend>Question 2</legend>
                    <p>Where was CoffeeScripts first stable build announced? <br />
                    <label for="github">GitHub</label>
                    <input type="radio" name="ans2" id="github" value="GitHub"/> <br />
                    <label for="hackernews">Hacker News</label>
                    <input type="radio" name="ans2" id="hackernews" value="Hacker News"/> <br />
                    <label for="coffeescript">CoffeeScript.com</label>
                    <input type="radio" name="ans2" id="coffeescript" value="CoffeeScript"/> <br />
                    <label for="dropbox">DropBox</label>
                    <input type="radio" name="ans2" id="dropbox" value="Dropbox"/> <br />
                </fieldset>
                <p><input type="submit" value="Submit Answer" id="subans" name="subans" /></p>
            </form>
        </article>

In php page/section, Added the isset($_POST['subans']).

        <?php 

            if(isset($_POST['subans'])){ // added the submit button action check

                    if (isset($_POST['ans2'])) {
                        $ans = $_POST['ans2'];
                        echo "<p>$ans</p>";
                    } 
                    else {
                        echo "Nothing was selected.";
                    }
            }

        ?>
于 2013-11-05T04:48:10.697 回答