duanfan1869
duanfan1869
2016-01-22 05:59

没有进入wordpress的php功能

已采纳

my problem is that when I click send this not entering the sendmail () function , should show alert, the ajax is the admin- ajax.php is good Request URL:http://localhost:8888/password/wp-admin/admin-ajax.php Request Method:POST Status Code:200 OK

HTML FORM:

<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条回答

  • dongyou4411 dongyou4411 5年前

    You cannot run javascript function in the ajax file .. instead run the desired php code in ajaxfile

    <?php 
    add_action('wp_ajax_sendmail', 'sendmail');
    add_action('wp_ajax_nopriv_sendmail', 'sendmail');
    
    
        function sendmail() {
        echo 'alert_me';
        die;
        }
    
    ?>
    

    Then is the response you can perform the alerting functionality

        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');
    }
                        }
                    });
                }
    
    点赞 评论 复制链接分享
  • duanjie5570 duanjie5570 5年前

    I'm not sure what you're trying to accomplish with creating a JS snippet inside PHP.

    Hit the admin-ajax route directly in the url. Put your payload in data and the endpoint you want to hit in 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          
            }
        });
    

    Your PHP would look like this

    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
    }
    
    点赞 评论 复制链接分享