没有进入wordpress的php功能

时间:2016-01-22 05:59:44

标签: php ajax wordpress

我的问题是,当我点击发送这个没有进入sendmail()函数时,应该显示警告,ajax是admin- ajax.php是好的请求URL:http://localhost:8888/password/wp-admin/admin-ajax.php 请求方法:POST 状态代码:200 OK

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>';
    }




 ?>

2 个答案:

答案 0 :(得分:0)

你不能在ajax文件中运行javascript函数..而是在ajaxfile中运行所需的php代码

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


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

?>

然后是您可以执行警报功能的响应

    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');
}
                    }
                });
            }

答案 1 :(得分: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
}