Wordpress AJAX not working - response 0

I want to add AJAX support for my plugin and I have a huge problem with this simple task. WordPress doesn't allow me to use regular AJAX and I need to use the WordPress version.

At all times the WordPress function (which should generate output) returns 0. And I think the reason is that WP doesn't call the "function". I'm trying to get the function to work many times, but I don't know what I can improve.

<?php
public function widget( $args, $instance ) {

$options = get_option('Free_Quotation_options');
?>
<script type="text/javascript" >

    jQuery(document).ready(function($) {        
        var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";

        jQuery.ajax({
            url: ajaxurl,
            type: 'POST',
            action: 'fqtag',
            data: {
                'whatever': 'text'
            },
            success: function (output) {
                $('#secondary').append(output);
            }       
        });
    });

</script> 
<?php
add_action( 'wp_ajax_fqtag', 'fqtag' );
add_action( 'wp_ajax_nopriv_fqtag', 'fqtag' );

function fqtag() {
    global $wpdb; 

    echo 'echo';

    die(); 
}
}

      

I am trying to add a warning ('echo'); to the test function, but it has no effect. I think the AJAX is not launching the correct function: fq_tag_support_callback ().

At the beginning, I had a problem with the ajaxurl variable. It hasn't been defined. This is not a normal situation. I am trying to solve this problem using:

var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";

      

Do you have any idea how I can try to solve this problem?

--- EDIT --- After discussion with David, I have a file like this (doesn't work all the time)

<?php
/*
    Plugin Name: TEST PLUGIN
    Description: TEST
    Author: Krzysztof Kubiak
    Version: 1.0
*/
function Test_01_settings_init(){
    register_setting( 'Test_01_settings_filed', 'Test_01_options', 'Test_01_validate' );
}
add_action('admin_init', 'Test_01_settings_init' );

function T01_init_method() {         
    wp_enqueue_script('jquery');      
}   
add_action('init', 'T01_init_method');

function Test_01_menu_page(){
    add_menu_page( 'Test_01', 'Test_01', 'manage_options', 'T01_menu_page', 'T01_add_page' );
    echo my_test();
}
add_action('admin_menu', 'Test_01_menu_page');

function my_test(){
    echo 'Function test';
}
function T01_add_page() {
    echo 'TEST_01_plugin';
}

function Test_01_validate($input) {
}   

//AJAX FROM HIRE

function test_callback() {
    $whatever = 8;
    echo $whatever;
    die();
}       
add_action( 'wp_ajax_nopriv_fqtag', 'test_callback', 1 );
add_action( 'wp_ajax_fqtag', 'test_callback', 1 );

function print_js() { ?>
    <script type="text/javascript">
    jQuery.ajax({
        url: 'wp-admin/admin-ajax.php',
        type: 'POST',
        action: 'fqtag',
        data: {
            'whatever': 'text'
        },
        success: function (output) {
          alert(output);
        }       
    });
    </script>
<?php
}
add_action('wp_print_footer_scripts', 'print_js', 1000);
?>

      

+3


source to share


3 answers


delete

<script>alert('echo');</script>

      

your answer should echo if you check the console. I suspect all of the above code is in your plugin functions file. Basically, the php function should be placed in the functions file.

jQuery should be placed in the template you want the response from.



Put this in your functions file ... remove jquery from class ...

add_action('wp_print_footer_scripts', 'print_js', 1000);

    function print_js() { ?>
    <script type="text/javascript">
    jQuery(document).ready(function(){

        jQuery.ajax({
            url: 'wp-admin/admin-ajax.php',
            type: 'POST',
            data: {
                'action': 'test_callback',
                'whatever': 'text'
            },
            success: function (output) {
              alert(output);
            }       
        }); 

    });
    </script>
<?php
}

      

Move this outside of your class ...

 function test_callback() {
                    $whatever = 8;
                    echo $whatever;
                    die();
 }

 add_action( 'wp_ajax_nopriv_testaction', 'test_callback' );
 add_action( 'wp_ajax_testaction', 'test_callback' );

      

+4


source


Just make sure you put the "fq_tag_support_callback ()" function in the main plugin file.



+1


source


I see several problems here. The action must be inside a data object , not as a jQuery Ajax parameter. Also the $ _POST variable is stored in the callback function.

function test_callback() {

    $whatever = $_POST['whatever'];
    echo $whatever;

    die();
}
add_action('wp_ajax_nopriv_fqtag', 'test_callback');
add_action('wp_ajax_fqtag', 'test_callback');

function print_js() {
    ?>
    <script type="text/javascript">
        jQuery.ajax({
            url: <?php echo admin_url('admin-ajax.php'); ?>,
            type: 'POST',
            data: {
                action: 'fqtag',
                whatever: 'text'
            },
            success: function (output) {
                alert(output);
           }       
        });
    </script>
    <?php
}
add_action('wp_print_footer_scripts', 'print_js', 1000);
?>

      

0


source







All Articles