2017-01-09 45 views
0

驗證我有這個腳本PHPMailer的git的問題:https://github.com/Krishneil1/Contact-Form-PHPPHPMailer的使用AJAX

這個腳本作品,未經我的一個問題,但我想添加AJAX和發送郵件時,驗證是正確的。通常當驗證不正確時,頁面被重新加載,但是當重新加載不正確時我不想重新加載網頁,但是在同一頁面上顯示錯誤。我希望你明白我的意思。我怎麼能這樣做?

我contact.php文件:

<?php 
session_start(); 
require_once 'libs/phpmailer/PHPMailerAutoload.php'; 

$errors =[]; 

if(isset($_POST['name'],$_POST['email'],$_POST['message'])){ 
    $fields=[ 
     'name'=>$_POST['name'], 
     'email'=>$_POST['email'], 
     'message'=>$_POST['message'] 
    ]; 
    foreach($fields as $field=>$data){ 
     if(empty($data)){ 
      $errors[]='The '.$field . ' field is required.'; 
     } 
    } 
    if(empty($errors)){ 
     $m=new PHPMailer; 
     $m->isSMTP(); 
     $m->SMTPAuth=true; 
     $m->Host='smtp.gmail.com'; 
     $m->Username='[email protected]';//replace by your email address 
     $m->Password='password';//replace with your password 
     $m->SMTPSecure='ssl'; 
     $m->Port=465; 

     $m->isHTML(); 
     $m->Subject ='Contact form Submitted'; 
     $m->Body='From:'.$fields['name'].'('.$fields['email'].')<p>'.$fields['message'].'</p>'; 

     $m->FromName='Contact'; 
     $m->AddAddress('[email protected]','Some one'); 
     if ($m->send()) { 
      header('Location:thanks.php'); 
      die(); 
     }else{ 
      $errors[]="Sorry ,Could not send email.Try again later."; 
     } 
    } 
}else{ 
    $errors[]= 'Something went wrong'; 
} 
$_SESSION['errors']=$errors; 
$_SESSION['fields']=$fields; 
header ('Location:index.php'); 

而且index.php文件是git的鏈接主網站上。

我的AJAX文件:

$(function() { 
$('#contact-form').on('submit', function (e) { 
    if (!e.isDefaultPrevented()) { 
     const url = "contact.php"; 

     $.ajax({ 
      type: "POST", 
      url: url, 
      data: $(this).serialize(), 
      dataType: 'json', 
      success: function(){ 
       alert('success'); 
      }, 

      error: function(){ 
       alert('error'); 
      } 
     }); 
     return false; 
    } 
}) 
}); 
+1

也請更新你至今嘗試過的代碼的問題。 – codePG

+0

@codePG更新。 – kristoff090

回答

0

當使用AJAX調用上面的腳本,請執行下列操作在阿賈克斯success回調改變

if(isset($_POST['name'],$_POST['email'],$_POST['message'])){ 
    //All your current code 
    // .... 
    if(empty($errors)){ 

     // .... 

     if ($m->send()) { 
      //Instead of doing a location redirect, send a response back to the calling script 
      //header('Location:thanks.php'); 
      //die(); 

      //Example response here 
      echo json_encode(array("status" => "success")); 
     }else{ 
      //And send a failure response here 
      echo json_encode(array("status" => "failed", "message" => "Sorry ,Could not send email.Try again later.")); 
     } 

    } else { 
     echo json_encode(array("status" => "failed", "message" => "Validation error.", "errors" => $errors)); 
    } 

    //Rest of your code 
    // .... 

else { 
    echo json_encode(array("status" => "failed", "message" => "Something went wrong.")); 
} 

然後,檢查返回的數據。如果success重定向到正確的位置,否則顯示錯誤消息。

請記住在您的ajax調用中將type設置爲json

如果您有任何疑問,請在下面評論。

修訂ANSWER

$.ajax({ 
    type: "POST", 
    url: url, 
    dataType: 'json', //Added the data type 
    data: $(this).serialize(), 
    dataType: 'json', 
    success: function (data){ 
     alert(JSON.stringify(data)); //alerted the return object 
    }, 

    error: function(){ 
     alert('error'); 
    } 
}); 

還要去掉下面的PHP文件,

$_SESSION['errors']=$errors; 
$_SESSION['fields']=$fields; 
+0

感謝您的回答!我按照你的寫法做了,並且我從ajax獲得了ajax成功或錯誤的響應,但是腳本沒有顯示錯誤,而不是它顯示我ajax resposne ...我想告訴我驗證錯誤,並且當驗證正確時,顯示我阿賈克斯成功...我更新我的主題與我的代碼 – kristoff090

+0

@ kristoff090你可以更新你的問題與js文件的Ajax是。這會幫助我更好地說明。沒有必要在會話中存儲錯誤 – codePG

+0

添加我的AJAX文件 – kristoff090