PHP反馈表单复选框错误


PHP Feedback form Checkbox error

好了,这是我的联系人表单的php缩短版本,(复选框没有正确发送)

<?php
 //please fill this in at least!
$myemail = "";
$title = "Feedback Form"; 
if(isset($_POST['submit'])) { //form has been submitted
//set variables with filters
$cont_name = filter_var($_POST['cont_name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['cont_email'], FILTER_SANITIZE_STRING);
$phone = filter_var($_POST['cont_phone'], FILTER_SANITIZE_STRING);
$first_time = filter_var($_POST['first_time'], FILTER_SANITIZE_STRING);
$hear_about = filter_var($_POST['hear_about'], FILTER_SANITIZE_STRING);
function valid_email($str){
    return ( ! preg_match("/^([a-z0-9'+_'-]+)('.[a-z0-9'+_'-]+)*@([a-z0-9'-]+'.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;} 
    $errors = 0; //by default there are no errors
    $trimcont_name = trim($cont_name);
    if(empty($trimcont_name)){
        //the name field is empty
        $errors = 1; //tips off the error messages below
        $errorcont_name = "The name field is empty"; //this error is displayed next to the label
    }
    if(!valid_email($email)) {
        //email is invalid or empty
        $errors = 1;
        $erroremail = "The email address was not valid";
    }
    $trimphone = trim($phone);
    if(empty($trimphone)){
        //the phone field is empty
        $errors = 1;
        $errorphone = "The phone field is empty";
    }
    $trimfirst_time = trim($first_time);
    if(empty($trimfirst_time)){
        //the first_time field is empty
        $errors = 1;
        $errorfirst_time = "This field is empty";
    }
    $trimhear_about = trim($hear_about);
    if(empty($trimhear_about)){
        //the hear_about field is empty
        $errors = 1;
        $errorhear_about = "This field is empty";
    }
    if($spam != "") {
        //spam was filled in
        $errors = 1;
        $errorspam = "The Spam box was filled in";
    }
    if($errors == 0) {
        $sendto = $myemail;
        $message = <<<DATA
DETAILS
Name: $cont_name 
Email: $email
Phone: $phone
Was this the first time you have been to us?
$first_time
How did you hear about us?
$hear_about
DATA;
        $headers = 'From: ' . $name . '<' . $email . '>';
           if(mail($sendto, $title, $message, $headers)) {
                //this is where it sends, using the php mail function
                $success = true;
                //set all the variables to blank to prevent re-submitting.
                $cont_name = "";
                $email = "";
                $phone = "";
                $hear_about = "";
                $first_time = "";
 } else {
                $success = false;
            }
    } else {
        $success = false;
    }
}
?>

功能不正常的区域是

<fieldset>
    <legend>How did you hear about us? <span class="phpformerror"><?php echo $errorhear_about; ?></span></legend>
    <div><input type="checkbox" name="hear_about[]" value="Web" /> Web</div>
    <div><input type="checkbox" name="hear_about[]" value="Newspaper" /> Newspaper</div>
    <div><input type="checkbox" name="hear_about[]" value="Radio" /> Radio</div>
    <div><input type="checkbox" name="hear_about[]" value="Driving" /> Driving Past</div>
    <div><input type="checkbox" name="hear_about[]" value="Referal" /> Referal</div>
    <div><input type="checkbox" name="hear_about[]" value="Other" /> Other</div>
</fieldset>

目前,如果选择多个变量,它只会显示其中一个变量。

hear_about是一个数组,filter_var()不能正确处理数组。而是使用filter_var_array():

$hear_about = filter_var_array($_POST['hear_about'], FILTER_SANITIZE_STRING);

请记住,$hear_about是一个数组,在整个代码中必须像一个数组一样对待(例如,只使用$hear_about将不起作用,它需要是$hear_about[0], $hear_about[1]等)。

例如,在你的trim line中你需要这样写:

foreach($hear_about as $key => $value) {
$trimhear_about[$key] = trim($value);
    if(empty($trimhear_about[$key])){
        //the hear_about field is empty
        $errors = 1;
        $errorhear_about[$key] = "This field is empty";
    }
}

$_POST['hear_about']是一个值数组。你把它当作一个简单的字符串来处理!

我认为你可以简单地替换这行:

$hear_about = filter_var($_POST['hear_about'], FILTER_SANITIZE_STRING);

:

$hear_about = filter_var(implode(', ', $_POST['hear_about']), FILTER_SANITIZE_STRING);
内爆函数(doc)通过将数组值与给定的glue连接起来,将数组"转换"为字符串。因此,您可以使用逗号将选定的"How did you hear about us?"选项连接起来,然后使用结果字符串作为其他数据。