Home ajax response is working without wp_ajax action
Reply: 0

ajax response is working without wp_ajax action

user4105
1#
user4105 Published in June 19, 2018, 11:52 pm

HTML:

<form enctype="multipart/form-data" action="" method="POST" style="margin-top: 20px; ">
        <div class="row btts-form-group clearfix">
    <div style="width: 20%; float: left; margin-right: 1%;">
        Image : <input type="file" name="image[]">
    </div>  
    <div style="width: 27%; float: left; margin-right: 1%;">
        Title : <input type="text" name="title[]">
    </div>

    <div style="width: 22%; float: left; margin-right: 1%;">
        More Info : <input type="text" name="more_info[]">
    </div>

    <div style="width: 22%; float: left; margin-right: 1%;">
        Button : <input placeholder="Put the url.." type="text" name="button_link[]">
    </div>
    <div style="width: 5%; float: left; ">
        <input type="button" class="button button-primary add_more" value="+">
    </div>
</div>


        <input type="submit" class="button button-primary" value="Save Change" style="margin-top: 20px;">
    </form>

Here is my simple jquery code to append a row of a form:

 $(document).ready(function(){

        $('body').on('click', '.add_more', function(e){


            $.ajax({
                url : absbBtToS.ajax_url,
                type : 'get',
                data : {
                    action : 'new_slider_html',
                    security : absbBtToS.check_nonce

                },
                success : function( response ) {

                    $('.btts-form-group:last').after(response);
                    //console.log(response);
                    //jQuery('.rml_contents').html(response);
                },
                error : function(error){
                   console.log(error);
                }
            });

          e.stopImmediatePropagation();

        });
     });

Ajax Action :

add_action( 'wp_ajax_new_slider_html',  'new_slider_html');

function new_slider_html(){
    include( plugin_dir_path( __FILE__ ) . 'admin/partials/absb_bt_to_s-admin-display.php');

        if( !check_ajax_referer( 'absbBtToS-nonce', 'security' ) ){
            wp_send_json_error('error!');
        }

      show_slider_form_input();
}

And show_slider_form_input(); definition is as follows which is inside absb_bt_to_s-admin-display.php:

function show_slider_form_input(){?>
<div class="row btts-form-group clearfix" >
    <div style="width: 20%; float: left; margin-right: 1%;">
        Image : <input type="file" name="image[]"  />
    </div>  
    <div style="width: 27%; float: left; margin-right: 1%;">
        Title : <input type="text" name="title[]" />
    </div>

    <div style="width: 22%; float: left; margin-right: 1%;">
        More Info : <input type="text" name="more_info[]" />
    </div>

    <div style="width: 22%; float: left; margin-right: 1%;">
        Button : <input placeholder="Put the url.." type="text" name="button_link[]" />
    </div>
    <div style="width: 5%; float: left; ">
        <input type="button" class="button button-primary add_more"  value="+" />
    </div>
</div>

<?php }

Everything is cool and working as expected. But if I remove following code:

add_action( 'wp_ajax_new_slider_html',  'new_slider_html');

function new_slider_html(){
include( plugin_dir_path( __FILE__ ) . 'admin/partials/absb_bt_to_s-admin-display.php');

    if( !check_ajax_referer( 'absbBtToS-nonce', 'security' ) ){
        wp_send_json_error('error!');
    }

  show_slider_form_input();
} 

It works as usual. As far I know the above code is only responsible to push response. I searched in my entire plugin no duplicate code is there. My question is how the ajax response come from? I am apologizing if I am asking something like nonsense. I just started to learn Ajax with wordpress.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.31966 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO