无法获取输入复选框列表

时间:2017-06-08 20:09:48

标签: php

我有一个表单,其中有多个复选框选项,但我无法检索所有选中的列表。 这是我的部分表格:

<form action="error" id="contact" method="post" name="contact">
<div class="form-step animated" id="form-step-1" style="display: block;">
    <h1>What are you or your loved one struggling with?</h1>
    <div class="info">
        <span class="txt">Check all that apply</span>
    </div>
    <ul class="services-list clearfix">
        <li>
            <div class="service">
                <label class="checkbox"><input name="problem" type="checkbox" value="Alcoholism"> <span class="icon"><i class="check"></i></span> <span class="label">Alcoholism</span></label>
            </div>
        </li>
        <li>
            <div class="service">
                <label class="checkbox"><input name="problem" type="checkbox" value="Drug Abuse"> <span class="icon"><i class="check"></i></span> <span class="label">Drug Abuse</span></label>
            </div>
        </li>
        <li>
            <div class="service">
                <label class="checkbox"><input name="problem" type="checkbox" value="Opiates"> <span class="icon"><i class="check"></i></span> <span class="label">Opiates</span></label>
            </div>
        </li>
        <li>
            <div class="service">
                <label class="checkbox"><input name="problem" type="checkbox" value="Eating Disorder"> <span class="icon"><i class="check"></i></span> <span class="label">Eating Disorder</span></label>
            </div>
        </li>
        <li>
            <div class="service">
                <label class="checkbox"><input name="problem" type="checkbox" value="Gambling"> <span class="icon"><i class="check"></i></span> <span class="label">Gambling</span></label>
            </div>
        </li>
        <li>
            <div class="service">
                <label class="checkbox"><input name="problem" type="checkbox" value="Mental Health"> <span class="icon"><i class="check"></i></span> <span class="label">Mental Health</span></label>
            </div>
        </li>
    </ul>
</div>
<input id="realSubmit" type="submit">

以下是表单处理程序的PHP代码:

    <?php
$errors = '';
$myemail = 'example@example.com';//<-----Put Your email address here.
if( empty($_POST['problem'])  ||
    empty($_POST['patient_name'])  ||
    empty($_POST['patient_phone'])  ||
    empty($_POST['patient_birth'])  ||
    empty($_POST['patient_address'])  ||
    empty($_POST['insurance'])  ||
    empty($_POST['ins_digits'])  ||
    empty($_POST['ins_provider'])  ||
    empty($_POST['ins_provider_phone'])  ||
    empty($_POST['group_id'])  ||
    empty($_POST['member_id'])) {
    $errors .= "\n Error: All fields are required";
}
// $problem = $_POST['problem'];
$problems = implode("," , $_POST['problem']);
$patient_name = $_POST['patient_name'];
$patient_phone = $_POST['patient_phone'];
$patient_birth = $_POST['patient_birth'];
$patient_address = $_POST['patient_address'];
$insurance = $_POST['insurance'];
$policy_holder_name = $_POST['policy_holder_name'];
$policy_holder_phone = $_POST['policy_holder_phone'];
$policy_holder_birth = $_POST['policy_holder_birth'];
$policy_holder_address = $_POST['policy_holder_address'];
$ins_digits = $_POST['ins_digits'];
$ins_provider = $_POST['ins_provider'];
$ins_provider_phone = $_POST['ins_provider_phone'];
$group_id = $_POST['group_id'];
$member_id = $_POST['member_id'];

if (!preg_match( "/^[1-9][0-9]{0,15}$/", $patient_phone)) {
    $errors_patient_phone .= "\n Error: Invalid Phone Number of Patient";
}

if (!preg_match( "/^[1-9][0-9]{0,15}$/", $policy_holder_phone)) {
    $errors_policy_holder_phone .= "\n Error: Invalid Phone Number of Policy Holder";
}

if (!preg_match( "/^[1-9][0-9]{0,4}$/", $ins_digits)) {
    $errors_ins_digit .= "\n Error: Invalid Social Security Number";
}
if (!preg_match( "/^[1-9][0-9]{0,15}$/", $ins_provider_phone)) {
    $errors_ins_provider_phone .= "\n Error: Invalid Phone Number of Insurance Provider";
}

if( empty($errors_patient_phone) && empty($errors_patient_phone) && empty($errors_ins_digit) && empty($errors_ins_provider_phone))  {
    $to = $myemail;
    $email_subject = "Contact form submission from: $patient_name";
    $email_body = "You have received a new submission. ".
    "Here are the details:\n Patient's Name: $patient_name \n Patient's Phone: $patient_phone \n Patient's Address: $patient_address \n Patient's Problems: $problems \n Insurance: $insurance \n Policy Holder's Name: $policy_holder_name \n Policy Holder's Phone: $policy_holder_phone \n Policy Holder's Address: $policy_holder_address \n    Policy Holder's Social Security Number: $ins_digits \n Insurance Provider: $ins_provider \n Insurance Provider's Phone: $ins_provider_phone \n Group ID# $group_id \n Membar ID# $member_id";

    $headers[] = 'Content-Type: text/plain; charset=UTF-8';
    // $headers .= "Reply-To: $email_address";

    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    header('Location: thank-you');
}
?>

我无法获取所选复选框的列表。

我可能没有从所选输入中获取数组。 有什么建议吗?

1 个答案:

答案 0 :(得分:3)

将复选框的名称从problem更改为problem[],然后您的$_POST['problem']将成为复选框值的数组:

<input name="problem[]" type="checkbox" value="Gambling">