Skip to content
Advertisement

Getting radio selection in PHP returns on not the value

I am trying to get the value of the selected radio button using php. But when I try to echo the value of the radio it returns ‘on’, no matter which radio button is selected when I submit the form.

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>

Then on the process page:

<?php

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

?>

Like I said, this just outputs “on” to the page.

Help appreciated!

Advertisement

Answer

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.";
                    }
            }

        ?>
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement