Genesis+AJAX+jQuery|调用操作失败

时间:2016-03-23 作者:Jacques Mivi

我想执行一个操作,而不需要重新加载页面。我跟着这首短裙https://codex.wordpress.org/AJAX_in_Plugins

但是每次我的页面重新加载时,我都无法获得结果值。这是我的代码:

In custom-login.php

add_action( \'get_header\', \'ck_do_login_form\' );
function ck_do_login_form() {

     // Enqueue sticky menu script - Crunchify Tips
    add_action( \'wp_enqueue_scripts\', \'crunchify_enqueue_script\' );

    // Test love
    add_action( \'wp_ajax_post_love_add_love\', \'post_love_add_love\' );
    add_action( \'wp_ajax_nopriv_post_love_add_love\', \'post_love_add_love\' );
    add_action( \'wp_ajax_post_love_add_love\', \'post_love_add_love\' );

    // Content
    add_action( \'genesis_entry_content\', \'vivi_logged\' );
}

function post_love_add_love() {
    $whatever = 1;
    $whatever++;
    echo $love;
    wp_die();
}

function crunchify_enqueue_script() {  

    wp_enqueue_script( \'jquery-2.1.0\', get_stylesheet_directory_uri() . \'/js/jquery-2.1.0.min.js\', array(\'jquery\'), \'1.0\', true );  
    wp_enqueue_script( \'user-login\', get_stylesheet_directory_uri() . \'/js/user-login.js\', array(\'jquery\'), \'1.0\', true );

    //Here we create a javascript object variable called "youruniquejs_vars". We can access any variable in the array using youruniquejs_vars.name_of_sub_variable
    wp_localize_script( \'user-login\', \'userlogin_object\', 
        array(\'ajaxurl\' => admin_url( \'admin-ajax.php\' ), \'the_issue_key\' => $the_issue_key) 
    );      
}

function vivi_logged() {

$love_text = \'<form id="ticket" name="ticket" >  
                          <fieldset>  
                            <input type="hidden" name="tkt_id" value="\' . $TKT_ID . \'" required/>
                            <input type="number" name="TKT_qty_new" value="\' . $TKT_qty . \'" required/>
                            <input id="submit" type="submit" name="submit" value="Send" />  
                          </fieldset>  
                          </form><div id="form-messages"></div></form><div id="form-messages2"></div><div id="form-messages3"></div>\';

            echo $love_text;

}

In user-login.js

jQuery(document).ready(function($){

    // jQuery( document ).on( \'click\', \'.love-button\', function(event) { Triger on click href
    jQuery(\'#ticket\').change( function(event){  

        jQuery("#form-messages").html("TEST 1");
        event.preventDefault();

        var postData = {
            \'action\': \'post_love_add_love\',
            \'whatever\': userlogin_object.the_issue_key
        };

        jQuery.ajax({
            type: "POST",
            url : userlogin_object.ajaxurl,
            data : postData,
            success: function(response) {
                alert(\'Got this from the server: \' + response);
                jQuery("#form-messages2").html("TEST REQUEST");
            }
        });

        jQuery("#form-messages3").html("TEST 3");
    }); 
});

Result

显示“TEST1”和“TEST3”,然后重新加载我的页面

你有没有办法阻止这种重新加载并让我的结果成为“测试请求”?

非常感谢您的帮助,

----------------EDIT V2 - 24/03/2016 ----------------

感谢Milo和Userabuser的评论,我改变了我的php。我的回答中仍然有0

In user-login.js

jQuery(document).ready(function($){

    jQuery(\'#ticket\').on(\'keyup click\', \'input[name="submit"], input[name="TKT_qty_new"]\', function(event){  

        //prevent the default submit action of the form...
        event.preventDefault();

        if ( event.type === \'click\' && event.target.name !== \'submit\' ) {
            return false;
        }        

        doAjaxRequest();

    }); 

    function doAjaxRequest() {

        //issue ajax request
        jQuery.ajax({
            type: "POST",
            url : userlogin_object.ajaxurl,
            dataType: "json",
            data : {
                \'action\': \'post_love_add_love\'
            },
            success: function(response) {
                //process success response
                alert(\'Got this from the server: \' + response);
                jQuery("#form-messages2").html("TEST REQUEST : " + response);
            }
        });

    }      

});

In custom-login.php

 add_action( \'get_header\', \'ck_do_login_form\' );
function ck_do_login_form() {

    // Start session if doesn\'t already exists
    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }

    // Remove the comment form
    add_filter( \'comments_open\', \'__return_false\' ); 

    // Remove the list of comments
    add_filter( \'comments_array\', \'__return_empty_array\' );
}

/**
* Enqueue sticky menu script - Crunchify Tips
*/
add_action( \'wp_enqueue_scripts\', \'crunchify_enqueue_script\' );
function crunchify_enqueue_script() {  

    wp_enqueue_script( \'jquery-2.1.0\', get_stylesheet_directory_uri() . \'/js/jquery-2.1.0.min.js\', array(\'jquery\'), \'1.0\', true );  
    wp_enqueue_script( \'user-login\', get_stylesheet_directory_uri() . \'/js/user-login.js\', array(\'jquery\'), \'1.0\', true );

    //Here we create a javascript object variable called "youruniquejs_vars". We can access any variable in the array using youruniquejs_vars.name_of_sub_variable
    wp_localize_script( \'user-login\', \'userlogin_object\', 
        array(\'ajaxurl\' => admin_url( \'admin-ajax.php\' ), \'the_issue_key\' => $the_issue_key) 
    );      
}


    // Test love Ajax
add_action( \'wp_ajax_post_love_add_love\', \'post_love_add_love\' );
add_action( \'wp_ajax_nopriv_post_love_add_love\', \'post_love_add_love\' );
function post_love_add_love() {
    $love = 10;
    wp_send_json($love);
    exit;
}

/**
* Fonction quand l\'internaute est connecté : Informations + Stages postés
*/
remove_action( \'genesis_entry_content\', \'genesis_do_post_content\' );
add_action( \'genesis_entry_content\', \'vivi_logged\' );
function vivi_logged() {

$love_text = \'<form id="ticket" name="ticket" >  
                          <fieldset>  
                            <input type="hidden" name="tkt_id" value="\' . $TKT_ID . \'" required/>
                            <input type="number" name="TKT_qty_new" value="\' . $TKT_qty . \'" required/>
                            <input id="submit" type="submit" name="submit" value="Send" />  
                          </fieldset>  
                          </form><div id="form-messages"></div></form><div id="form-messages2"></div><div id="form-messages3"></div>\';

            echo $love_text;
}

1 个回复
最合适的回答,由SO网友:Adam 整理而成

如果您正在单击表单中的“提交”按钮(我假设是这样);它将提交执行默认方法的表单GET 其中未指定方法。

您需要确保阻止默认提交操作。

在JavaScript中;您正在收听change 事件,而应该绑定到提交按钮。

jQuery(document).ready(function($){

    jQuery(\'#ticket\').on(\'click\', \'#submit\', function(event){  

        //prevent the default submit action of the form...
        event.preventDefault();

        //issue ajax request
        jQuery.ajax({
            type: "POST",
            url : userlogin_object.ajaxurl,
            data : {
                \'action\': \'post_love_add_love\',
                \'whatever\': userlogin_object.the_issue_key
            },
            success: function(response) {
                //process success response
                alert(\'Got this from the server: \' + response);
                jQuery("#form-messages2").html("TEST REQUEST");
            }
        });

    }); 

});
如果你真的必须倾听change 事件;以接收输入的字段为目标,在您的情况下,该字段仅为一个字段name="TKT_qty_new":

jQuery(document).ready(function($){

    jQuery(\'#ticket\').on(\'keyup click\', \'input[name="submit"], input[name="TKT_qty_new"]\', function(event){  

        //prevent the default submit action of the form...
        event.preventDefault();

        if ( event.type === \'click\' && event.target.name !== \'submit\' ) {
            return false;
        }        

        doAjaxRequest();

    }); 

    function doAjaxRequest() {

        //issue ajax request
        jQuery.ajax({
            type: "POST",
            url : userlogin_object.ajaxurl,
            data : {
                \'action\': \'post_love_add_love\',
                \'whatever\': userlogin_object.the_issue_key
            },
            success: function(response) {
                //process success response
                alert(\'Got this from the server: \' + response);
                jQuery("#form-messages2").html("TEST REQUEST");
            }
        });

    }      

});