最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - HTML form not sending expected value of radio button, sending "on" instead - Stack Overflow

programmeradmin2浏览0评论
    <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".

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

Share Improve this question edited 2 days ago ADyson 61.9k15 gold badges73 silver badges88 bronze badges Recognized by PHP Collective asked 2 days ago ScaryMindsScaryMinds 3454 silver badges11 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 3

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">
发布评论

评论列表(0)

  1. 暂无评论