mjsalinger
mjsalinger

Reputation: 660

Cannot retrieve selected radio button value in PHP form post

I am unable to get the selected radio button value in a form post. No matter which option I select, I am getting the value "on". Note this is in a Wordpress Plugin Options page.

Code for form below:

?>
<div class="wrap">
    <?php screen_icon(); ?>
    <h2>Title</h2>
    <br />
    <form action="options-general.php?page=page" method="post">
    <table class="widefat">
    <thead>
        <tr>
            <th></th>
            <th>Site Name</th>
            <th>Site URL</th>
            <th>Username</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th></th>
            <th>Site Name</th>
            <th>Site URL</th>
            <th>Username</th>
        </tr>
    </tfoot>
    <tbody>
    <?php 

    $size = count($sites);

    for ( $i = 0; $i < $size; $i++) {

        echo '<tr>';

        for ($j = 0; $j < 4; $j++) {
            if ($j == 0) {


            echo '<td><input type="radio" id="cta_siteID_'.$i.'" name="cta_siteID",value="'.$i.'"/></td>';

            }
            else
            {

                echo '<td>'.$sites[$i][$j-1].'</td>';
            }
        }
        echo '</tr>';
    } 
    ?>
    </tbody>
   </table>
   <br />

   <input type="hidden" name="formType" value="main" />
   <select name="selectedItem">
        <option value="" selected="true">Select an Option</option>
        <option value="add">Add Site</option>
        <option value="edit">Edit Site</option>
        <option value="delete">Delete Site</option>
   </select>
   <input type="submit" name="Apply" value="Apply" class="button-primary" />
   </form>
</div>

When I do a var_dump($_POST) after selecting an item in the radio group, I get the following:

onarray(4) { ["cta_siteID"]=> string(2) "on" ["formType"]=> string(4) "main" ["selectedItem"]=> string(4) "edit" ["Apply"]=> string(5) "Apply" }

The values are being rendered correctly in the HTML before posting the form, not sure what is going on.

The "on" value shows up no matter which radio option I select, and does not show up at all if none is selected (as I would expect).

Upvotes: 0

Views: 856

Answers (1)

Fluffeh
Fluffeh

Reputation: 33542

You shouldn't have a comma inside your input tag:

name="cta_siteID",value=

This could well be causing your browser to bork the value being passed.

Upvotes: 3

Related Questions