Ajax函数没有回复任何响应但是帖子工作正常:Wordpress

时间:2016-10-25 09:51:20

标签: ajax wordpress forms function email

我有这个ajax脚本,它对function.php中的一个函数采取行动,在提交表单后发送一封电子邮件。数据正在发布但功能未触发,因此电子邮件未发送,脚本也没​​有显示任何响应。

              ==================Jquery====================
jQuery('#quote-forms').submit(function(e) {

    e.preventDefault();

    var ocity = jQuery(".ocity").val();
    var ostate = jQuery(".ostate").val();
    var ozip = jQuery(".ozip").val();
    var dataString = 'custom ocity='+ ocity + '&custom ostate=' + ostate + '&custom ozip=' + ozip;

    jQuery.ajax({
        type: "POST",
        url: "/wp-admin/admin-ajax.php",
        action : 'quote_ajax_submission',
        data: dataString,
        success: function() {  
        alert("sent");
        } 
        });          
    });

这是我在function.php文件中的功能,请你查一下,让我知道是什么问题所以没有回复,电子邮件也没有发送。

        add_action('wp_ajax_quote_ajax_submission', 'quote_ajax_handler');
        add_action('wp_ajax_nopriv_quote_ajax_submission','quote_ajax_handler');
  function quote_ajax_handler(){

echo "helol";

$oCity          = $_POST["custom ocity"];
$iZip           = $_POST["custom ozip"];
$oState         = $_POST["custom ostate"];
$dState         = $_POST["dstate"];
$dZip           = $_POST["dzip"];
$dCity          = $_POST["dcity"];
$rNumbers       = $_POST["room-numbers"];
$mDate          = $_POST["mdate"];
$fName          = $_POST["fname"];
$lName          = $_POST["lname"];
$pEmail         = $_POST["pemail"];
$pPhone         = $_POST["pphone"];
$addtion_C      = $_POST["additionC"];

$to = 'shoaibswl123@gmail.com';

$subject = 'Quote Request From '. $fName ;

$message = '<html><body>'; 

$message = '<div style="background:#CEE4ED; padding:10px;>';
$message = '<div style="margin-left:30px;">';
$message .= "<br>";
$message .= "<br>";

$message .= "<b>Hi Admin,</b>";
$message .= "<br>";
$message .= "<br>";
$message .= 'You have received a new quote from the site with the following information';

$message .= "<br>";
$message .= "<br>";

$message .= '<b>Address Information:</b>';

$message .= "<br>";
$message .= "<br>";


$message .= "<b>Origin City: </b>" .$oCity;
$message .= "<br />";
$message .= "<b>Origin Zip: </b>" . $iZip; 
$message .= "<br />";
$message .= "<b>Origin State: </b>" . $oState;
$message .= "<br />";
$message .= "<b>Destination City: </b>" .$dCity;
$message .= "<br />";
$message .= "<b>Destination Zip: </b>" .$dZip;
$message .= "<br />";
$message .= "<b>Destination State: </b>" .$dState; 

$message .= "<hr>";
$message .= "<br>";
$message .= "<br>";

$message .= '<b>Client Information:</b>'; 
$message .= "<br />";
$message .= "<br />";
$message .= "<b>First Name: </b>" .$fName;
$message .= "<br />";
$message .= "<b>Last Name: </b>" .$lName;
$message .= "<br />";
$message .= "<b>Primary Emai: </b>" .$pEmail;
$message .= "<br />";
$message .= "<b>Primary Phone: </b>" .$pPhone;
$message .= "<hr>";
$message .= "<br>";
$message .= "<br>";

$message .= '<b>Moving Information:</b>'; 

$message .= "<br>";
$message .= "<br />";

$message .= "<b>Moving Date: </b>" .$mDate;
$message .= "<br />";
$message .= "<b>Number of Rooms: </b>" .$rNumbers;

$message .= "<hr>";
$message .= "<br>";
$message .= "<br>";

$message .= '<b>Addition Comments :</b>';
$message .= "<br />";
$message .= '<p>'.$addtion_C.'</p>' ;

$message .= "<br>";
$message .= "<br>";
$message .= "<br>";

$message .= "<p>Thank you.</p>";

$message .= "</div></div>";

$message .= "</body></html>";

$headers  = 'MIME-Version: 1.0' . "\r\n";

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$headers .= 'From: '.$pEmail.'' . "\r\n";


   if(wp_mail($to, $subject, $message, $headers)){

    echo "<div class='success-message'>Message Has Been Sent Successfully..!</div>";

   }else{

     echo "<div class='success-message'>There is an error while sending message..!</div>";
   }

   die();
 }

1 个答案:

答案 0 :(得分:0)

ajax调用中的action元素存在问题。

所以我们更新了这样的代码,并在数据数组中添加了action元素,其工作方式如下:

console.log(computedStyle.getPropertyValue("width");
console.log(computedStyle.getPropertyValue("width");

因此,在更新的代码之前,action元素是这样的:

jQuery.ajax({
        type: "POST",
        url: "/wp-admin/admin-ajax.php",
        data: 'action=quote_ajax_submission&'+dataString,
        success: function(data) {  
        alert("sent");
        },
        error: function(request, status, error){
        alert(request.responseText); 

        }
        });          
    });

更新代码操作后......

 action : 'quote_ajax_submission',