Ajax php联系表单没有电子邮件

时间:2017-04-26 03:09:15

标签: javascript php ajax formmail

似乎无法让这个代码工作它说它发送电子邮件,但在某个地方它被卡住了。此时无法从主机访问日志,因此无法检查,此时应该从我的观点发送电子邮件

//check if its an ajax request, exit if not
if(!isset($_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(!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userMessage"]))
{
    $output = json_encode(array('type'=>'error', 'text' => 'The input fields are empty!'));
    die($output);
}

//Sanitize input data using PHP filter_var().
$user_Name        = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
$user_Email       = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
$user_Phone             = $_POST["userTelephone"];
$user_Message     = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);

//additional php validation
if(strlen($user_Name)<3) // If length is less than 3 it will throw an HTTP error.
{
    $output = json_encode(array('type'=>'error', 'text' => 'Name field can not be empty'));
    die($output);
}
if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
{
    $output = json_encode(array('type'=>'error', 'text' => 'Please use a valid email address'));
    die($output);
}

if(strlen($user_Message)<5) //check emtpy message
{
    $output = json_encode(array('type'=>'error', 'text' => 'Please enter a message'));
    die($output);
}


$message_Body = "<strong>Name: </strong>". $user_Name ."<br>";
$message_Body .= "<strong>Email: </strong>". $user_Email ."<br>";
$message_Body .= "<strong>Phone: </strong>". $user_Phone ."<br>";
$message_Body .= "<strong>Message: </strong>". $user_Message ."<br>";



$headers = "From: " . strip_tags($user_Email) . "\r\n";
$headers .= "Reply-To: ". strip_tags($user_Email) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";



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



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

if(!$sentMail)
{
    $output = json_encode(array('type'=>'error', 'text' => 'An error has occurred, please try again'));
    die($output);
}else{
    $output = json_encode(array('type'=>'message', 'text' => 'Hello '.$user_Name .'Thanks for your contact we will return soon.'));
    die($output);
}
}

这是JS

  $("#submit_btn").click(function() {
            //get input field values
            var user_name       = $('input[name=name]').val();
            var user_email      = $('input[name=email]').val();
            var user_telephone      = $('input[name=phone]').val();
            var user_message    = $('textarea[name=message]').val();

            //simple validation at client's end
            var post_data, output;
            var proceed = true;
            if(user_name==""){
                    proceed = false;
            }
            if(user_email==""){
                    proceed = false;
            }
            if(user_message=="") {
                    proceed = false;
            }

            //everything looks good! proceed...
            if(proceed)
            {
                    //data to be sent to server
                    post_data = {'userName':user_name, 'userEmail':user_email, 'userTelephone':user_telephone, 'userMessage':user_message};

                    //Ajax post data to server
                    $.post('contact.php', post_data, function(response){

                            //load json data from server and output message
            if(response.type == 'error')
            {
                output = '<div class="alert-danger" style="padding:10px; margin-bottom:25px;">'+response.text+'</div>';
            }else{
                output = '<div class="alert-success" style="padding:10px; margin-bottom:25px;">'+response.text+'</div>';

                //reset values in all input fields
                $('#form-elements input').val('');
                $('#form-elements textarea').val('');
            }

            $("#result").hide().html(output).slideDown();
                    }, 'json');

            }
    });

    //reset previously set border colors and hide all message on .keyup()
    $("#form-elements input, #form-elements textarea").keyup(function() {
            $("#result").slideUp();
    });

HTML

  <form id="form-elements" onSubmit="return false">
                     <div class="row">
                        <div class="col-md-12 center"><div id="result"> </div> </div>
                     </div>

                    <div class="row">
                        <div class="col-sm-4">
                            <div class="form-group">
                                <input type="text" class="form-control" placeholder="Namn" name="name" id="name" required></div>
                        </div>
                        <div class="col-sm-4">
                            <div class="form-group">
                                <input type="email" class="form-control" placeholder="Email Adress" name="email" id="email" required></div>
                        </div>
                        <div class="col-sm-4">
                            <div class="form-group">
                                <input type="text" class="form-control" placeholder="Telefon nummer" name="phone" id="phone" required>
                            </div>
                        </div>
                        <div class="col-xs-12">
                            <textarea id="input" class="form-control" rows="7" required="required" placeholder="Meddelande" name="message" id="message" ></textarea>
                        </div>
                        <button type="submit" class="btn btn-default buttons" id="submit_btn">Skicka</button>
                    </div>
                </form>

1 个答案:

答案 0 :(得分:0)

Dunno但有些人说我应该插入

$("#submit_btn").click(function() {
 e.preventDefault();

是我使用的问题吗?

onSubmit="return false"