2014-02-25 58 views
2

處理單選按鈕我有我試圖發送一個單選按鈕的值的報名表,但不確定的PHP吧,我的代碼如下:形式在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';} 
?> 

回答

0

它會以同樣的方式與其他過程中,唯一的區別是你已經知道它的價值,所以你需要

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

+1檢查isset()函數。請記住,如果您的單選按鈕被選中,您將獲得該值。如果未選中,則需要手動設置響應,因爲在這種情況下單選按鈕不會在請求中傳遞。 – nextgtech

0

此代碼將幫助您

$response = $_POST['response'] ; 

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