2016-01-22 42 views
0

我的問題是,當我點擊發送這個不進入sendmail()函數時,應該顯示alert,ajax是admin- ajax.php是好請求網址:http://localhost:8888/password/wp-admin/admin-ajax.php 請求方法: POST 狀態代碼:200 OK未進入wordpress的php功能

HTML表單:

<div class="form"> 
          <form action="" method="POST" id="ContactForm" enctype="application/x-www-form-urlencoded" novalidate="novalidate" > 
           <div class="form-group">  
            <input type="text" class="form-control" name="name" placeholder="Full Name *"> 
           </div> 
           <div class="form-group">  
            <input type="text" class="form-control" name="email" placeholder="Email *"> 
           </div> 
           <div class="form-group">  
            <input type="text" class="form-control" name="celular" placeholder="Celular o Teléfono"> 
           </div> 
           <div class="form-group"> 
            <textarea rows="5" class="form-control" name="message" placeholder="Your Message *" style="height:175px;"></textarea> 
           </div> 
           <div id="loading_icon"><img src='<?php echo get_template_directory_uri(). '/images/loading_spinner.gif'; ?>' alt="loading" style="display: none;"></div> 
           <input class="btn btn-default" type="submit" name="submit" value="Enviar"> 
           <div id="#response"></div> 
          </form> 
         </div> 

AJAX:

jQuery(document).ready(function($){ 
    $("#ContactForm").validate({ 
     rules: { 
      name: "required", 
      email: { 
       required: true, 
       email: true 
      }, 
      celular: "required" 
     }, 
     messages: { 
      name: "Por favor digite su nombre", 
      email: { 
       required: "Por favor digite su correo", 
       email: "Porfavor ingrese un email valido" 
      }, 
      message: "Ingrese el asunto en el que le podemos ayudar", 
      celular: "Digite su numero de celular o telefono" 
     }, 
     submitHandler: function(form) { 
      $('#loading_icon').show(); 
      $('#click').hide(); 
      var params = $(form).serialize(); 
      $.ajax ({ 
       type: 'POST', 
       url: ajaxurl, 
       data: params + '&action=sendmail', 
       success: function(response) { 
        $('#response').hide(); 
        $('#response').html(response); 
        $('#response').fadeIn('slow'); 
        $('#loading_icon').hide();      
       } 
      }); 
     } 

    }); 
}); 

PHP:

<?php 

/* 
Plugin Name: Formulario de contacto 
Plugin URI: http://www.e-world.co 
Description: Formulario de contacto con ajax 
Version: 1.0 
Author: Jorge Moreno 
Author URI: http://www.e-world.co 
license: GLP2 
*/ 

function frontend_custom_ajaxurl() { ?> 
    <script type="text/javascript"> 
     var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>'; 
    </script> 
    <?php 
    } 
add_action('wp_head','frontend_custom_ajaxurl'); 



add_action('wp_ajax_sendmail', 'sendmail'); 
add_action('wp_ajax_nopriv_sendmail', 'sendmail'); 


    function sendmail() { 
    echo '<script language="javascript">alert("se envió!!");</script>'; 
    } 




?> 
+0

您發送的形式,是否正在發送?因爲你可以使用ajax方法('success','beforeSend','error','complete')來輸出任何警報等等。 –

回答

0

您不能運行在AJAX文件中的JavaScript功能..而不是運行在ajaxfile

<?php 
add_action('wp_ajax_sendmail', 'sendmail'); 
add_action('wp_ajax_nopriv_sendmail', 'sendmail'); 


    function sendmail() { 
    echo 'alert_me'; 
    die; 
    } 

?> 

然後將所需的PHP代碼是可以執行的警報功能

submitHandler: function(form) { 
       $('#loading_icon').show(); 
       $('#click').hide(); 
       var params = $(form).serialize(); 
       $.ajax ({ 
        type: 'POST', 
        url: ajaxurl, 
        data: params + '&action=sendmail', 
        success: function(response) { 
         $('#response').hide(); 
         $('#response').html(response); 
         $('#response').fadeIn('slow'); 
         $('#loading_icon').hide(); 
         if(reponse == 'alert_me'){ 
alert('Alert your required string'); 
} 
        } 
       }); 
      } 
+0

jajajjajjaaj 好的,起初的疑惑是,無論是發送還是執行Ajax和發出警告告訴我任何事情,我非常感謝你。 –

0

響應我不確定你想在PHP中創建一個JS片段來完成什麼。

直接在url中輸入admin-ajax路由。把你的有效載荷在data,你想在params: {'action': 'your_endpoint_here'}

$.ajax({ 
    method: 'POST', 
    url: '/wp-admin/admin-ajax.php', 
    data: data, 
    params: {'action': 'my_endpoint'} 
    }).success(function (data) { 
     // do stuff with response data 
     } 
    }).error(function (data) { 
     // do stuff with error response data   
     } 
    }); 

你的PHP打終點應該是這樣

add_action('wp_ajax_nopriv_my_endpoint', array($this, 'myMailFunction')); 
add_action('wp_ajax_my_endpoint', array($this, 'myMailFunction')); 

public function myMailFunction() 
{ 
    //send mail here 
} 
+0

好的我明白,但可以通過URL'/wp-admin/admin-ajax.php'訪問wordpress中的文件?我會嘗試,謝謝 –