dqba94394 2019-02-22 00:57
浏览 26

引导单选按钮组选择的空/空返回值

Would appreciate someone explaining to me why I only get an empty string back from PHP when trying to get radio-button group selection.

$currentlyemployed = null;
if (isset($currentlyemployedyes)) {
  $currentlyemployed = "Yes";
}
else {
  $currentlyemployed = "No";
}

NOTE: text inputs within the same form post and I can get their values.

Example snippets:

START FORM MARKUP

<div class="row">
<div class="col-sm-6 text-left">
   <h5>Are you currently employed?</h5>
      <div class="control-group">
         <div class="controls">
            <label>
               <input type="radio" id="currentlyemployedyes" name="currentlyemployed" value="yes" />Yes</label>
            <label>
               <input type="radio" id="currentlyemployedno" name="currentlyemployed" value="no" />No</label>
             <p class="help-block"></p>
          </div>
       </div>
    </div>
</div>

END FORM MARKUP

START PHP CODE SNIPPET

$currentlyemployed = $_POST['currentlyemployed']; RETURNS empty string

/* FYI
$currentlyemployedyes = $_POST['currentlyemployedyes']; RETURNS "Yes"
$currentlyemployedno = $_POST['currentlyemployedno']; RETURNS "No"
*/

END PHP CODE SNIPPET

  • 写回答

2条回答 默认 最新

  • doujing5150 2019-02-23 03:50
    关注

    The code you have written works as expected. You can check out the snippet below. Bootstrap or not it does not matter. The name="currentlyemployed" sent from the form to php server and then php binds it into $_POST associative global array.

     <?php 
        echo "<pre>";
          var_dump($_POST);
        echo "</pre>";
    
          echo "Currently Employed: ".$_POST['currentlyemployed'];
    
        ?>
        <hr>
    
        <div class="row">
        <div class="col-sm-6 text-left">
          <form method="POST">
             <h5>Are you currently employed?</h5>
                <div class="control-group">
                   <div class="controls">
                      <label>
                         <input type="radio" id="currentlyemployedyes" name="currentlyemployed" value="yes" />Yes</label>
                      <label>
                         <input type="radio" id="currentlyemployedno" name="currentlyemployed" value="no" />No</label>
                       <p class="help-block"></p>
                    </div>
                 </div>
    
                 <input type="submit">
            </form>
            </div>
        </div>
    
    评论

报告相同问题?