dongzha3058 2015-10-25 19:38
浏览 29
已采纳

为什么我的表单会出错?

I've been using this php code as part of my form's validation. Its been working fine for the past 6 months. All of a sudden every time I submit the form I keep getting the following custom error message I have setup here:

header('HTTP/1.1 500 Our server is under maintenance!<br> If this error persists please contact us directly: <strong>something@test.com</strong>');

I've tested the php validation on the form's fields and that seems to work OK. I haven't changed anything in my php or ajax logic or the html form itself. Can't figure out whats wrong with it.

Here is the php that I use:

<?php
if($_POST)
{
    //check if its an ajax request, exit if not
    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
        die();
    } 


    //check $_POST vars are set, exit if any missing
    if(!isset($_POST["userName"]) || !isset($_POST["userLastName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userArrival"]) || !isset($_POST["userNumberPeople"]) 
    || !isset($_POST["userPickupLocation"]) || !isset($_POST["userRequest"]) || !isset($_POST["userTourSelection"]))
    {
        die();
    }

    //Sanitize input data using PHP filter_var().
    $user_Name              = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
    $user_LastName          = filter_var($_POST["userLastName"], FILTER_SANITIZE_STRING);
    $user_Email             = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
    $user_TourSelection     = filter_var($_POST["userTourSelection"], FILTER_SANITIZE_STRING);
    $user_Arrival           = filter_var($_POST["userArrival"], FILTER_SANITIZE_STRING);
    $user_NumberPeople      = filter_var($_POST["userNumberPeople"], FILTER_SANITIZE_STRING);
    $user_PickupLocation    = filter_var($_POST["userPickupLocation"], FILTER_SANITIZE_STRING);
    $user_Request           = filter_var($_POST["userRequest"], FILTER_SANITIZE_STRING);



     $to_Email       = "something@test.com"; //Replace with recipient email address
     $subject        = 'Tour request: '.$user_TourSelection.' '; //Subject line for emails


    //additional php validation
    if(strlen($user_Name)<2) // If length is less than 4 it will throw an HTTP error.
    {
        header('HTTP/1.1 500 Name is too short or empty!');
        exit();
    }
    if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
    {
        header('HTTP/1.1 500 Please enter a valid email address!');
        exit();
    }
    if(strlen($user_Request)<5) //check emtpy message
    {
        header('HTTP/1.1 500 Please explain in a few words how you would like us to assist you.');
        exit();
    }


    // message
$message = '<strong>Name:</strong> '.$user_Name.' '.$user_LastName.' <br><br>
            <strong>Date of Arrival:</strong> '.$user_Arrival.' <br><br>
            <strong>Tour:</strong> '.$user_TourSelection.' <br><br>
            <strong>Number of people:</strong> '.$user_NumberPeople.' <br><br>
            <strong>Pick-Up Location:</strong> '.$user_PickupLocation.' <br><br>
            <strong>Request:</strong> '.$user_Request.' 
            ';


//proceed with PHP email.
    $headers = 'From: '.$user_Email.'' . "
" .
    $headers .='Reply-To: '.$user_Email.'' . "
" ;
    $headers .= 'MIME-Version: 1.0' . "
";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";
    $headers .='X-Mailer: PHP/' . phpversion();

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

    if(!$sentMail)
    {
        header('HTTP/1.1 500 Our server is under maintenance!<br> If this error persists please contact us directly: <strong>something@test.com</strong>');
        exit();
    }else{
        echo 'Congratulations '.$user_Name .'! ';
        echo 'We have received your request and we will respond as soon as possible.';
    }
}
?>

Here is the JS

$(document).ready(function() {

    $("#submit_btn").click(function() { 
        //collect input field values
        var user_name       = $('input[name=firstName]').val(); 
        var user_lastname   = $('input[name=lastName]').val();
        var user_email      = $('input[name=email]').val();
        var user_numberpeople = $('select#numberofPeople').val();
        var user_pickuplocation = $('select#pickupLocation').val();
        var user_arrivaldate = $('input[name=arrivalDate]').val();
        var user_request    = $('textarea[name=request]').val();
        var user_tourselection = $('#tourSelection option:selected').val();

        //simple validation at client's end
        //we simply change border color to red if empty field using .css()
        var proceed = true;

       if(user_name==""){ 
            $('input[name=firstName]').addClass("required");
            proceed = false;
        }
        if(user_lastname==""){ 
            $('input[name=lastName]').addClass("required"); 
            proceed = false;
        }
        if(user_email=="") {    
            $('input[name=email]').addClass("required"); 
            proceed = false;
        }
        if(user_numberpeople=="") {    
            $('select#numberofPeople').addClass("required");
            proceed = false;
        }

        if(user_pickuplocation=="") {    
            $('select#pickupLocation').addClass("required"); 
            proceed = false;
        }


        if(user_arrivaldate=="") {    
            $('input[name=arrivalDate]').addClass("required"); 
            proceed = false;
        }

        if(user_request=="") {  
            $('textarea[name=request]').addClass("required"); 
            proceed = false;
        }

        if(user_tourselection=="") {  
            $('select#tourSelection').addClass("required"); 
            proceed = false;
        }



        //everything looks good! proceed...
        if(proceed){

            //deactivate button
            $("#reservation-form button#submit_btn").attr("disabled", "disabled").text('Processing...').addClass("processing-state");

            //data to be sent to server
            var post_data = {

                'userName':user_name, 
                'userLastName':user_lastname, 
                'userEmail':user_email, 
                'userNumberPeople':user_numberpeople, 
                'userPickupLocation':user_pickuplocation, 
                'userArrival':user_arrivaldate, 
                'userRequest':user_request, 
                'userTourSelection':user_tourselection
            };

            //Ajax post data to server
            $.post('http://www.myurl.com/reservation.php', post_data, function(data){  

                //load success message in #result div element, with slide effect.
                $("#result").hide().html('<span class="success">'+data+'</span>').slideDown();

                $('label').hide();
                $('select').hide();
                $('#reservation-form h2').hide();
                ga('send', 'event', 'tours', 'request-tour', 'Tour-Request-Complete');


            }).fail(function(err) {  //load any error data

                $("#reservation-form button#submit_btn").text('REQUEST FAILED...').removeClass("processing-state").addClass("fail-state");
                $("#result").hide().html('<div class="error">'+err.statusText+'</div>').slideDown();

                ga('send', 'event', 'tours', 'request-tour', 'Tour-Request-Error');

                $( "#reservation-form input, #reservation-form textarea" ).focus(function() {

                    $("#reservation-form button#submit_btn").removeAttr("disabled").text('SEND YOUR REQUEST').removeClass("fail-state");
                    $("#result").slideUp();

                }); 

            });
        }
    });


    //reset previously set border colors and hide all message on .keyup()
    $("input, textarea").keyup(function() { 
        $(this).removeClass("required"); 
    });


    $("select#tourSelection").change(function() {

        if( $("select#tourSelection").val() != '')

            {$("select#tourSelection").removeClass("required");
            }

    });


        $("select#numberofPeople").change(function() {

        if( $("select#numberofPeople").val() != '')

            {$("select#numberofPeople").removeClass("required");
            }

    });

        $("select#pickupLocation").change(function() {

        if( $("select#pickupLocation").val() != '')

            {$("select#pickupLocation").removeClass("required");
            }

    });




});

and the HTML

<fieldset id="reservation-form">

<div id="result"></div>
    <h2>Please fill out the form</h2>

    <label for="firstName">
    <input type="text" name="firstName" id="firstName" placeholder="FIRST NAME" />
    </label>

    <label for="lastName">
    <input type="text" name="lastName" id="lastName" placeholder="LAST NAME" />
    </label>

    <label for="email">
    <input type="text" name="email" id="email" placeholder="EMAIL" />
    </label>

    <select id="tourSelection">
    <option value="">PLEASE SELECT YOUR TOUR</option>
    <option value="ATHENS TOUR">ATHENS TOUR</option>
    <option value="ATHENS and SOUNION TOUR">ATHENS &amp; SOUNION TOUR</option>
    <option value="ATHENS and CORINTH TOUR">ATHENS &amp; CORINTH TOUR</option>
    <option value="PELOPONNESE TOUR">PELOPONNESE TOUR</option>
    <option value="DELPHI TOUR">DELPHI TOUR</option>
    <option value="DELPHI and THERMOPYLAE TOUR">DELPHI &amp; THERMOPYLAE TOUR</option>
    <option value="VRAVRONA MARATHON and SOUNION TOUR">VRAVRONA MARATHON &amp; SOUNION</option>
    <option value="ATHENS HALF DAY TOUR">ATHENS HALF DAY TOUR</option>
    <option value="CORINTH TOUR">CORINTH TOUR</option>
    <option value="SOUNION TOUR">SOUNION TOUR</option>
    <option value="SOUNION SUNSET TOUR">SOUNION SUNSET TOUR</option>
    <option value="NAFPLIO TOUR">NAFPLIO TOUR</option>
    <option value="DELPHI and METEORA TOUR">DELPHI &amp; METEORA TOUR</option>
    <option value="ARGOLIDA and ANCIENT OLYMPIA TOUR">ARGOLIDA &amp; ANCIENT OLYMPIA TOUR</option>
    <option value="THREE DAY PELOPONNESE TOUR">THREE DAY PELOPONNESE TOUR</option>
    <option value="FOUR DAY PELOPONNESE TOUR">FOUR DAY PELOPONNESE TOUR</option>
    <option value="CUSTOM TOUR">CUSTOM TOUR</option>
    </select>


    <select id="numberofPeople">
        <option value="">TOTAL NUMBER OF PEOPLE IN YOUR PARTY</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="Over 8">Over 8</option>
    </select>


        <select id="pickupLocation">
         <option value="">SPECIFY PICK-UP LOCATION</option>
        <option value="Port">PORT</option>
        <option value="Airport">AIRPORT</option>
        <option value="Hotel">HOTEL</option>
        <option value="Other">OTHER</option>
        </select>




    <label for="arrivalDate">
    <input type="text" name="arrivalDate" id="arrivalDate" placeholder="DATE YOU WISH TO DO THE TOUR" />
    </label>

    <label for="request">
    <textarea name="request" id="request" placeholder="YOUR PERSONAL REQUEST"></textarea>
    </label>

    <label>
    <button class="submit_btn" id="submit_btn">SEND YOUR REQUEST</button>
    </label>
</fieldset>

The problem is that the form does not send the data to the designated email. It always shows the custom error message.

Any help is much appreciated.

Thank you!


php error reporting shows the following:

Array ( [type] => 8192 [message] => Directive 'register_globals' is deprecated in PHP 5.3 and greater [file] => Unknown [line] => 0 )

Update After adding php_flag register_globals off to my htaccess I'm no longer getting php error messages but its still not submitting. Keeps loading custom error message from line 72 (php code).

  • 写回答

2条回答 默认 最新

  • duandi8838 2015-10-25 19:53
    关注

    The problem are caused by email sending. Check if mail function is working. The problem can be in the server configuration.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊