单页脚本错误

时间:2015-09-25 23:14:07

标签: php forms

我有这个表单页面:http://webauthorsgroup.com/raa/contacform/index3.html产生“空字段”错误,但我无法确定这个错误发生的位置。

我使用以下php表单:

<?php
if($_POST)
{
    $to_Email       = "email@email.com"; //Replace with recipient email address
    $subject        = 'Web Form'; //Subject line for emails

    //check if its an ajax request, exit if not
    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {

        //exit script outputting json data
        $output = json_encode(
        array(
            'type'=>'error', 
            'text' => 'Request must come from Ajax'
        ));

        die($output);
    } 

    //check $_POST vars are set, exit if any missing
    if(empty($_POST["userFirst"]) || empty($_POST["userLast"]) || empty($_POST["userPhone"]) || empty($_POST["userEmail"]))
    {
        $output = json_encode(array('type'=>'error', 'text' => 'Input fields are empty!'));
        die($output);
    }

    //Sanitize input data using PHP filter_var().
    $user_First       = filter_var($_POST["userFirst"], FILTER_SANITIZE_STRING);
    $user_Last       = filter_var($_POST["userLast"], FILTER_SANITIZE_STRING);
    $user_Company       = filter_var($_POST["userCompany"], FILTER_SANITIZE_EMAIL);
    $user_Address       = filter_var($_POST["user_Address"], FILTER_SANITIZE_STRING);
    $user_City       = filter_var($_POST["user_City"], FILTER_SANITIZE_STRING);
    $user_State       = filter_var($_POST["user_State"], FILTER_SANITIZE_STRING);
    $user_Zip       = filter_var($_POST["user_Zip"], FILTER_SANITIZE_STRING);
    $user_Phone       = filter_var($_POST["user_Phone"], FILTER_SANITIZE_STRING);
    $user_Email       = filter_var($_POST["user_Email"], FILTER_SANITIZE_STRING);
    $user_Cell       = filter_var($_POST["user_Cell"], FILTER_SANITIZE_STRING);
    $user_Type       = filter_var($_POST["user_Type"], FILTER_SANITIZE_STRING);
    $user_Carrier       = filter_var($_POST["user_Carrier"], FILTER_SANITIZE_STRING);
    $user_cs_experience       = filter_var($_POST["user_cs_experience"], FILTER_SANITIZE_STRING);
    $user_contractor_experience       = filter_var($_POST["user_contractor_experience"], FILTER_SANITIZE_STRING);
    $user_delivery_areas       = filter_var($_POST["user_delivery_areas"], FILTER_SANITIZE_STRING);
    $user_yr_make_model       = filter_var($_POST["user_yr_make_model"], FILTER_SANITIZE_STRING);
    $user_ref1_company       = filter_var($_POST["user_ref1_company"], FILTER_SANITIZE_STRING);
    $user_ref1_contact       = filter_var($_POST["user_ref1_contact"], FILTER_SANITIZE_STRING);
    $user_ref1_address       = filter_var($_POST["user_ref1_address"], FILTER_SANITIZE_STRING);
    $user_ref1_city       = filter_var($_POST["user_ref1_city"], FILTER_SANITIZE_STRING);
    $user_ref1_state       = filter_var($_POST["user_ref1_state"], FILTER_SANITIZE_STRING);
    $user_ref1_zip       = filter_var($_POST["user_ref1_zip"], FILTER_SANITIZE_STRING);
    $user_ref2_company       = filter_var($_POST["user_ref2_company"], FILTER_SANITIZE_STRING);
    $user_ref2_contact       = filter_var($_POST["user_ref2_contact"], FILTER_SANITIZE_STRING);
    $user_ref2_address       = filter_var($_POST["user_ref2_address"], FILTER_SANITIZE_STRING);
    $user_ref2_city       = filter_var($_POST["user_ref2_city"], FILTER_SANITIZE_STRING);
    $user_ref2_state       = filter_var($_POST["user_ref2_state"], FILTER_SANITIZE_STRING);
    $user_ref2_zip       = filter_var($_POST["user_ref2_zip"], FILTER_SANITIZE_STRING);
    $user_Message       = filter_var($_POST["user_Message"], FILTER_SANITIZE_STRING);

    //additional php validation
    if(strlen($user_First)<3) // If length is less than 4 it will throw an HTTP error.
    {
        $output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
        die($output);
    }
    if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
    {
        $output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
        die($output);
    }
    if(!is_numeric($user_Phone)) //check entered data is numbers
    {
        $output = json_encode(array('type'=>'error', 'text' => 'Only numbers allowed in phone field'));
        die($output);
    }
    /*if(strlen($user_Message)<5) //check emtpy message
    {
        $output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
        die($output);
    }*/

    //proceed with PHP email.
    $headers = 'From: '.$user_Email.'' . "\r\n" .
    'Reply-To: '.$user_Email.'' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

    $content = $user_Message."\n".$user_First."\n".$user_Last."\n".$user_Company."\n".$user_Address."\n".$user_City."\n".$user_State."\n".$user_Zip."\n".$user_Phone."\n".$user_Email."\n".$user_Cell."\n".$user_Type."\n".$user_Carrier."\n".$user_cs_experience."\n".$user_contractor_experience."\n".$user_delivery_areas."\n".$user_yr_make_model."\n".$user_ref1_company."\n"."\n".$user_ref1_contact."\n".$user_ref1_address."\n".$user_ref1_city."\n".$user_ref1_state."\n".$user_ref1_zip."\n".$user_ref2_company."\n".$user_ref2_contact."\n".$user_ref2_addressl."\n".$user_ref2_city."\n".$user_ref2_state."\n".$user_ref2_zip;

    $sentMail = @mail($to_Email, $subject, $content, $headers);

    if(!$sentMail)
    {
        $output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
        die($output);
    }else{
        $output = json_encode(array('type'=>'message', 'text' => 'Thank you '.$user_First .'. We will be in contact with you.'));
        die($output);
    }
}
?>

1 个答案:

答案 0 :(得分:0)

您与某些变量命名不一致。查看HTML源代码,您有以下变量:

    var user_First      = $('input[name=First]').val();
    var user_Last      = $('input[name=Last]').val();
    var user_Company      = $('input[name=Company]').val();
    var user_Address      = $('input[name=Address]').val();
    var user_City      = $('input[name=City]').val();
    var user_State      = $('input[name=State]').val();
    var user_Zip      = $('input[name=Zip]').val();
    var user_Phone 

查看代码,您正在寻找没有下划线的变量:

这里:

$user_Company       = filter_var($_POST["userCompany"], FILTER_SANITIZE_EMAIL);

另外:让user_FirstuserFirst都很混乱,您可能希望将其清理干净以备将来调试。