2015-10-29 164 views
3

我需要一些帮助。我设计了我的自定义ajax函数。这是一个简单的形式,输入5个值验证它们,然后通过ajax将数据发送到php函数,然后函数通过电子邮件发送这些详细信息。一旦成功,弹出窗口显示给用户确认。ajax成功返回0

我已经应用验证,我也能够运行ajax。但由于某种原因,我的成功函数不断返回0.请帮助。 http://imgur.com/m5Ah0md

网络输出上提交关于铬: http://imgur.com/R7q9WnH

我的JavaScript

function contact_validations(){ 
event.preventDefault(); 
var name= jQuery('#contactname').val(); 
var cemail= jQuery('#contactemail').val(); 
var tel= jQuery('#contactphone').val(); 
var address= jQuery('#contactaddress').val(); 
var message= jQuery('#contactsubject').val(); 
var s=true; 

if(name==''|| name==null){ 
    jQuery('#name_err').html('Please enter your name'); 
    s=false; 
    }else{ 
     jQuery('#name_err').html(''); 
     } 

if(cemail==''){ 
    jQuery('#email_err').html('Please enter email'); 
    s=false; 
    }else{ 
     var x=cemail; 
     var atpos=x.indexOf("@"); 
     var dotpos=x.lastIndexOf("."); 
     if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) 
      { jQuery('#email_err').html("Please Enter Valid E-mail Address"); s= false;} 
      else{ 
       jQuery('#email_err').html(''); 
       } 
     } 


if(tel==''|| tel==null){ 
    jQuery('#tel_err').html('Please enter your telephone number'); 
    s=false; 
    }else{ 
     jQuery('#tel_err').html(''); 
     } 



if(address=='' || address==null){ 
    jQuery('#address_err').html('Please enter your address'); 
    s=false; 
    }else{ 
     jQuery('#address_err').html(''); 
     } 


if(message=='' || message==null){ 
    jQuery('#message_err').html('Please enter your message'); 
    s=false; 
    }else{ 
     jQuery('#message_err').html(''); 
     } 
     console.log(url); 


if(s) 
     { 
      var url = "http://localhost:9080/c/wp-admin/admin-ajax.php"; 
      var data = { 

       "action":"contact_email" , 
       "name": name , 
       "email": cemail , 
       "tel": tel , 
       "address": address , 
       "message": message 


      } 
      jQuery.ajax({ 
      type: "POST", 
      url: url, 
      contentType: 'JSON', 
      data: JSON.stringify(data), 
      beforeSend: function(){ 
       console.log('happened'); 
       jQuery('#ajax-loader').show(); 
       jQuery('#ajax-loader').html('<img src="http://localhost:9080/c/wp-content/themes/aussie/images/ajax-loader.gif">'); 
       }, 
      success: function(result) { 
       jQuery('#ajax-loader').hide(); 
       console.log(result); 


       if(result=='0') 
       { 
        console.log("No Result") 
       } 
       if(result=='1') 
       { 
       jQuery.fancybox({ 
        href: '#contact-popup' 
       }); 
       jQuery('#contactname').val(''); 
       jQuery('#contactemail').val(''); 
       jQuery('#contactphone').val(''); 
       jQuery('#contactaddress').val(''); 
       jQuery('#contactsubject').val(''); 

       console.log (s); 
       } 
      } 
     }); 


     } 
} 

我的javascript:上提交铬

开发工具输出

function contact_email() { 



      $url = get_bloginfo('url'); 
      $storeemail = get_option('contactemailstored'); 
      $to = $storeemail['contactemail']; 
      $subject = $url . ' - Contact'; 
      $name = $_POST['contactname']; 
      $email = $_POST['contactemail']; 
      $phone = $_POST['contactphone']; 
      $address= $_POST['contactaddress']; 
      $content= $_POST['contactsubject'];           
      $message = '<br>'.'Name:- ' . $name . '<br>'; 
      $message .= 'Email- ' . $email . '<br>'; 
      $message .= 'Phone:- ' . $phone . '<br>'; 
      $message .= 'Address :' . $address . '<br>'; 
      $message .= 'Subject :' . $content . '<br>'; 


      $sent_message = wp_mail($to , $subject, $message); 

     if ($sent_message) { 
      // the message was sent... 
      echo '1'; 
     } else { 
      // the message was not sent... 
      echo '1'; 
     } 

     exit; 


    } 
add_action('wp_ajax_contact_email', 'contact_email'); 

add_action('wp_ajax_nopriv_contact_email', 'contact_email'); 
+1

究竟返回0的是什么? –

+0

当你在浏览器中加载php页面时会发生什么? – Fluinc

+0

但是你的'成功'方法并没有返回任何东西......你的意思是'result =='0''? – rnevius

回答

0

谢谢非常感谢大家的帮助。我得到了很多帮助。虽然我发现了这个问题。问题是,对于WordPress我不需要在JSON中解析数据。所以我用正常的类型,它的工作。

此外,我对我的功能CONTACT_EMAIL语法错误。我用

$name = $_POST['contactname']; 
$email = $_POST['contactemail']; 
$phone = $_POST['contactphone']; 
$address= $_POST['contactaddress']; 
$content= $_POST['contactsubject']; 

我捕捉表单数据与它的原始而是应该由已在AJAX被宣布为VAL被捕获。相反,我用这个和它的工作:

$name = $_POST['name']; 
$email = $_POST['email']; 
$phone = $_POST['tel']; 
$address= $_POST['address']; 
$content= $_POST['message']; 

下面是更新后的代码:

的Jquery:

function contact_validations(){ 
var name= jQuery('#contactname').val(); 
var cemail= jQuery('#contactemail').val(); 
var tel= jQuery('#contactphone').val(); 
var address= jQuery('#contactaddress').val(); 
var message= jQuery('#contactsubject').val(); 
var s=true; 

if(name==''|| name==null){ 
    jQuery('#name_err').html('Please enter your name'); 
    s=false; 
    }else{ 
     jQuery('#name_err').html(''); 
     } 

if(cemail==''){ 
    jQuery('#email_err').html('Please enter email'); 
    s=false; 
    }else{ 
     var x=cemail; 
     var atpos=x.indexOf("@"); 
     var dotpos=x.lastIndexOf("."); 
     if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) 
      { jQuery('#email_err').html("Please Enter Valid E-mail Address"); s= false;} 
      else{ 
       jQuery('#email_err').html(''); 
       } 
     } 


if(tel==''|| tel==null){ 
    jQuery('#tel_err').html('Please enter your telephone number'); 
    s=false; 
    }else{ 
     jQuery('#tel_err').html(''); 
     } 



if(address=='' || address==null){ 
    jQuery('#address_err').html('Please enter your address'); 
    s=false; 
    }else{ 
     jQuery('#address_err').html(''); 
     } 


if(message=='' || message==null){ 
    jQuery('#message_err').html('Please enter your message'); 
    s=false; 
    }else{ 
     jQuery('#message_err').html(''); 
     } 


if(s) 
     { 
      var url = "http://localhost:9080/c/wp-admin/admin-ajax.php"; 

      jQuery.ajax({ 
      type: "POST", 
      url: url, 


      data:"action=contact_email&name="+name+"&email="+cemail+"&tel="+tel+"&address="+address+"&message="+message, 
      beforeSend: function(){ 
       console.log('happened'); 
       jQuery('#ajax-loader').show(); 
       jQuery('#ajax-loader').html('<img src="http://localhost:9080/c/wp-content/themes/aussie/images/ajax-loader.gif">'); 
       }, 
      success: function(result) { 
       jQuery('#ajax-loader').hide(); 
       console.log(result); 


       if(result=='0') 
       { 
        console.log("No Result") 
       } 
       if(result=='1') 
       { 
       jQuery.fancybox({ 
        href: '#contact-popup' 
       }); 
       jQuery('#contactname').val(''); 
       jQuery('#contactemail').val(''); 
       jQuery('#contactphone').val(''); 
       jQuery('#contactaddress').val(''); 
       jQuery('#contactsubject').val(''); 

       console.log (s); 
       } 
      } 
     }); 


     } 

} 

PHP

function contact_email() { 



      $url = get_bloginfo('url'); 
      $storeemail = get_option('contactemailstored'); 
      $to = $storeemail['contactemail']; 
      $subject = $url . ' - Contact'; 
      $name = $_POST['name']; 
      $email = $_POST['email']; 
      $phone = $_POST['tel']; 
      $address= $_POST['address']; 
      $content= $_POST['message'];            
      $message = '<br>'.'Name:- ' . $name . '<br>'; 
      $message .= 'Email- ' . $email . '<br>'; 
      $message .= 'Phone:- ' . $phone . '<br>'; 
      $message .= 'Address :' . $address . '<br>'; 
      $message .= 'Subject :' . $content . '<br>'; 


      $sent_message = wp_mail($to , $subject, $message); 

     if ($sent_message) { 
      // the message was sent... 
      echo '1'; 
     } else { 
      // the message was not sent... 
      echo '0'; 
     } 

     exit; 


    } 
add_action('wp_ajax_contact_email', 'contact_email'); 

add_action('wp_ajax_nopriv_contact_email', 'contact_email'); 
1

问题是您正在将data转换为json,但wp期望找到一个名为action的字段来查找正确的函数,因此您的返回值为0(函数未找到,未遇到出口,因此返回0)。

在这种情况下,你实际上并不需要json值发送表单,但它会期望一个json响应,所以在你的php函数中你应该得到json_encode你的返回值。

jQuery(document).ready(function(){ 

     var url = "<?php echo admin_url('admin-ajax.php'); ?>"; 
     var data = { 

      "action": contact_email , 
      "name": name , 
      "email": cemail , 
      "tel": tel , 
      "address": address , 
      "message": message 
     } 

     jQuery.ajax({ 
      type: "POST", 
      url: url, 
      contentType: 'JSON', 
      //JSON.stringify(data), -->will cause issue 
      data: data, 
      beforeSend: function(){ 
       console.log('happened'); 
      }, 
      success: function(result) { 
       console.log(result); 
      } 
     });   
}); 

PHP

function contact_email() { 

      // the message was sent... 
     echo json_encode(1); // if just sending 1, you can echo without json encode... 
     exit; 

} 
add_action('wp_ajax_contact_email', 'contact_email'); 

add_action('wp_ajax_nopriv_contact_email', 'contact_email'); 

发送的Json ...

如果你愿意,你可以转换其他形式的价值观JSON和包括在它自己的对象键。例如

var data={}; 
data.action= 'hook'; 
data= JSON.stringify(formData); 

依此类推。

+0

谢谢你的解答队友。我试过你的方式确切的代码没有工作,但我明白了.. –

0
function contact_email() { 

     //your code 
     exit(); 
     die(); 
} 

您需要在函数的末尾包含这些PHP函数exit();die();