在PHP中处理单选按钮的表单

时间:2014-02-25 09:31:11

标签: php email radio-button return-value processing

我有一个rsvp表单,我试图发送一个单选按钮的值,但不确定它的PHP,我的代码如下:

 <div class="form-group">
     <input type="text" name="name" id="name" size="30" value="" placeholder="Name(s)" class="text-input form-control" />
     <label class="error" for="name" id="name_error">Name is required.</label>
</div>

<div class="form-group">
     <input type="radio" name="response" id="response_accepted" value="accepted" checked="checked" > Will be there
     <input type="radio" name="response" id="response_declined" value="declined"> Won't make it
     <label class="error" for="response" id="name_error">Please select an option</label>
 </div>

<div class="form-group">
     <input type="text" name="guests" id="guests" size="30" value="" placeholder="No. of guests attending" class="text-input form-control" />
     <label class="error" for="guests" id="guests_error">No. of guests is required.</label>
</div>

但我不确定收集单选按钮值的php,但是使用其他字段来传递数据:

    <?php
if ((isset($_POST['name'])) && (strlen(trim($_POST['name'])) > 0)) {
    $name = stripslashes(strip_tags($_POST['name']));
} else {$name = 'No name entered';}

if ((isset($_POST['guests'])) && (strlen(trim($_POST['guests'])) > 0)) {
    $guests = stripslashes(strip_tags($_POST['guests']));
} else {$guests = 'No # of guests entered';}
?>

2 个答案:

答案 0 :(得分:0)

它将以与另一个相同的方式处理,唯一的区别是你已经知道它的价值,所以你需要

if ((isset($_POST['response'])) && ($_POST['response'] == 'accepted' ) {
    $response = 'Accepted';
} else {$response= 'Not Accepted';}

答案 1 :(得分:0)

此代码可以帮助您

$response = $_POST['response'] ; 

if(isset($response) && $resonse == "accepted" )){ 
echo "Accepted" ; 
}
else{ echo "Declined" ;  }