如果选中则单选按钮“是”,如果未选中则单选“否”

时间:2014-09-18 23:54:47

标签: php forms

下午好,我在创建的表单上遇到单选按钮有困难。我在这里发现了类似的问题,但由于PHP编码经验有限,我无法正确编码或我的表单。任何帮助将不胜感激。

    <?php 
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$procedure = $_POST['procedure'];
$date = $_POST['date'];
$message = $_POST['message'];
$policybox = null;
foreach($_POST['policy'] as $policy){
if(isset($policy)){
    $policybox .= "Yes\r\n";
} else{
    $policybox .= "No\r\n";
}
} 

$formcontent="From: $firstname $lastname \nEmail: $email \nPhone: $phone \nType of Procedure: $procedure \nDate Requested: $date \nI have read and understood the policies: $policybox \nMessage: $message";
$recipient = "sales@domainname.com";
$subject = "Appointment Form from DomainName.com";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: appointments.php');
?>

我试图出现的领域是:

<div class="form_info cmsms_radio">
                                        <div class="check_parent">
                                            <input type="radio" name="policy" value="Yes" />                
                                            <label for="policy">I have read and understand the <a href="refund-policy.php">Refund Policy</a> and <a href="cancellation-policy.php">Cancellation Policy</a></label>
                                        </div>

我确定它的东西很小我不见了,但就像我说我对php还不是很有经验。

1 个答案:

答案 0 :(得分:1)

您的$_POST['policy']变量不是一个数组(基于您的代码),因此您无需迭代它。您只需测试表单值本身即可。

foreach替换为以下内容:

if(isset($_POST['policy']) && $_POST['policy'] == "Yes") {
    $policybox .= "Yes\r\n";
} else{
    $policybox .= "No\r\n";
}