• 
    <?php
    function example_ajax_enqueue() {
      // Enqueue javascript on the frontend.
    
      // The wp_localize_script allows us to output the ajax_url path for our script to use.
      wp_localize_script(
        'example-ajax-script',
        'example_ajax_obj',
        array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) )
      );
    }
    add_action( 'wp_enqueue_scripts', 'example_ajax_enqueue' ); ?>
         <script type="text/javascript">
      var ajaxurl = '<?php echo admin_url("admin-ajax.php"); ?>';
    
    jQuery(document).ready(function($) {
       $('body').on('change', '#assign', function() {
        // We'll pass this variable to the PHP function example_ajax_request
        var fruit = 'Banana';
         
        // This does the ajax request
        $.ajax({
            url: ajaxurl, // or example_ajax_obj.ajaxurl if using on frontend
            data: {
                'action': 'example_ajax_request',
                'fruit' : fruit
            },
            success:function(data) {
                // This outputs the result of the ajax request
                console.log(data);
            },
            error: function(errorThrown){
                console.log(errorThrown);
            }
        });    }); 
                  
    });
    </script>
    <?php 
    function example_ajax_request() {
     
        // The $_REQUEST contains all the data sent via ajax
       echo "DSfds";
         
        // Always die in functions echoing ajax content
       die();
    }
     
    add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' );
     
    // If you wanted to also use the function for non-logged in users (in a theme for example)
    add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' );
    

    please help me

    • This topic was modified 6 years, 7 months ago by Jan Dembowski.
    • This topic was modified 6 years, 7 months ago by Jan Dembowski. Reason: Code formatted
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Ajax is not working my plugin’ is closed to new replies.