PHP反馈表单复选框错误

时间:2011-12-09 09:19:48

标签: php forms

好的,这是我的联系表单的缩短版本的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>

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

2 个答案:

答案 0 :(得分:2)

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],{{1等等)。

因此,例如在您的修剪线中,您需要以下内容:

$hear_about[1]

这将保留处理数组的好处。

答案 1 :(得分:0)

$_POST['hear_about']是一组值。您将其作为一个简单的字符串处理!

我认为你可以解决简单的替换线:

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

使用:

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

implode function (doc) 数组转换为字符串,方法是将数组值与给定的胶水连接起来。所以你可以连接选中的“你怎么听说我们的?”使用逗号的选项,然后使用结果字符串作为其他数据。