Search code examples
phphtmlradio-button

HTML form not sending expected value of radio button, sending "on" instead


    <form action="admin_review.php?opt=newReview" method="post" >
        <fieldset>
            <legend>Review Type</legend>
            <div class="input">
                <label for="movie">Movie Review</label>
                <input type="radio" name="reviewType" id="movie">
            </div>
            <div class="input">
                <label for="television">Television Review</label>
                <input type="radio" name="reviewType" id="television">
            </div>
            <div class="input">
                <label for="book">Book Review</label>
                <input type="radio" name="reviewType" id="book">
            </div>
            <div class="input">
                <label for="comic">Comic Review</label>
                <input type="radio" name="reviewType" id="comic">
            </div>
            <div class="input">
                <label for="game">Game Review</label>
                <input type="radio" name="reviewType" id="game">
            </div>
        </fieldset>
        <input type="submit" value="New Review Defintion"> 
    </form>

    if(isset($_POST['reviewType'])) {
        $value = $_POST['reviewType'];
        die("<h1>Review Type " . $value . "</h1>");

$value is set to "on", not quite sure where this is coming from and why it isn't the selected value of "movie".


Solution

  • You haven't given your radio buttons any values. So, as per the radio button documentation:

    If you omit the value attribute in the HTML, the submitted form data assigns the value on to the group.

    You seem to be under the impression that the id attribute assigns the value of the radio button, but it doesn't - that just sets a unique identifier for that particular HTML element within the document / page, just like any other HTML element. It has nothing to do with a form field's value.

    To correct this, change the id to value on all your buttons, e.g.

     <input type="radio" name="reviewType" value="movie">