单选按钮“;是”;如果被检查;否”;如果未选中


Radio button "yes" if checked, "no" if unchecked

下午好,我在创建的表单上使用单选按钮时遇到困难。我在这里发现了一个类似的问题,但由于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还不是很有经验。

您的$_POST['policy']变量不会是一个数组(基于您的代码),因此您不需要对其进行迭代。您可以简单地测试表单值本身。

foreach替换为以下内容:

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