JavaScript Wordpress passing ajax value to a specific page using Wordpress wordpress send ajax request,wordpress send ajax response,wordpress send

I would like to pass a variable to a specific page. I found a simple example explaining how to use ajax with wordpress.

JavaScript:

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

// We'll pass this variable to the PHP function example_ajax_request
var fruit = 'Banana';

// This does the ajax request
$.ajax({
    url: ajaxurl,
    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);
    }
});  

});

Piece of PHP to insert in functions.php

function example_ajax_request() {

// The $_REQUEST contains all the data sent via ajax
if ( isset($_REQUEST) ) {

    $fruit = $_REQUEST['fruit'];

    // Let's take the data that was sent and do something with it
    if ( $fruit == 'Banana' ) {
        $fruit = 'Apple';
    }

    // Now we'll return it to the javascript function
    // Anything outputted will be returned in the response
    echo $fruit;

    // If you're debugging, it might be useful to see what was sent in the $_REQUEST
    // print_r($_REQUEST);

}

// Always die in functions echoing ajax content
  die();
 }

add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' );


   wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajax_url' =>   admin_url( 'admin-ajax.php' ) ) );

Unfortunately I cannot pass the variable. I inspected the code and I get this error:

Error: ajax_object is not defined

Do you maybe know another way to obtain the same result?

Answer:1

You are very near, but there is some little things missing…

What I mean in my comment, is that you need to use it this way using 'ajax-script' in both:

add_action('wp_enqueue_scripts', 'add_js_scripts'); 
add_js_scripts(){
    wp_enqueue_script( 'ajax-script', get_template_directory_uri().'/js/script.js', array('jquery'), '1.0', true );
    wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajaxurl' =>   admin_url( 'admin-ajax.php' ) ) );
}

Changed $_REQUEST to $_POST:

function example_ajax_request() {

    // The $_REQUEST contains all the data sent via ajax
    if ( isset($_POST) ) {

        $fruit = $_POST['fruit'];

        // Let's take the data that was sent and do something with it
        if ( $fruit == 'Banana' ) {
            $fruit = 'Apple';
        }

        // Now we'll return it to the javascript function
        // Anything outputted will be returned in the response
        echo $fruit;

        // If you're debugging, it might be useful to see what was sent in the $_POST
        // print_r($_POST);

    }

    // Always die in functions echoing ajax content
      die();

 }

Added add_action( 'wp_ajax_nopriv_ … ):

add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' ); // <= this one
add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' );

For your jQuery script script.js file, there is 2 important missing little things:

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

    /* We'll pass this variable to the PHP function example_ajax_request */
    var fruit = 'Banana';

    /* This does the ajax request */
    $.ajax({
        url: ajax_object.ajaxurl, /* <====== missing here */
        type : 'post', /*    <========== and missing here */
        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);
        }
    });  

});

This should work now…

References:

Answer:2

You are using wp_localize_script wrong. In the PHP code, remove the wp_localize_script line.

Optionally you can (and should) add the following

// this line is for users who are not logged in
add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' );
Answer:3

I was messing around with the first question here: Reduce duplicate characters to a desired minimum and am looking for more elegant answers than what I came up with. It passes the test but curious to ...

I was messing around with the first question here: Reduce duplicate characters to a desired minimum and am looking for more elegant answers than what I came up with. It passes the test but curious to ...

  1. reduce duplicate characters to a desired minimum

In my app (generated with yeoman) i have such structure: view: index.html with ng-view for rendering controller view controller: every controller try to get some data with api urlm like: $scope....

In my app (generated with yeoman) i have such structure: view: index.html with ng-view for rendering controller view controller: every controller try to get some data with api urlm like: $scope....

  1. angularjs load view dynamically
  2. angularjs load view
  3. angularjs load view from controller
  4. angularjs view load before controller
  5. angularjs view loaded event
  6. angularjs load partial view
  7. angularjs load new view
  8. angularjs load different view
  9. angularjs load data before view
  10. angularjs after view load
  11. angularjs load mvc partial view
  12. angularjs ng-view load event
  13. angularjs directive on view load

Is it possible to have multiple clients to the same UDP server ? I'd like to broadcast the same data to all connected clients. Here would be a starting sample, if it helps somehow ... // Server var ...

Is it possible to have multiple clients to the same UDP server ? I'd like to broadcast the same data to all connected clients. Here would be a starting sample, if it helps somehow ... // Server var ...

My Node script has this in it: var connection = mysql.createConnection(...); connection.connect(); connection.query(/*sql query*/, function(err, rows, fields){ app.get('/', function(req, res){ ...

My Node script has this in it: var connection = mysql.createConnection(...); connection.connect(); connection.query(/*sql query*/, function(err, rows, fields){ app.get('/', function(req, res){ ...

  1. node js jade
  2. node js jade tutorial
  3. node js jade example
  4. node js jade template
  5. node.js jade vs ejs
  6. node js jade table example
  7. node.js jade template tutorial
  8. node js jade vs pug
  9. node js jade bootstrap
  10. node.js jade alternatives
  11. node js jade table
  12. node js jade button onclick
  13. node js jade vs handlebars
  14. node js jade form
  15. node js jade template engine
  16. node js jade to html
  17. node js jade mysql
  18. node js jade css
  19. node.js jade template variables
  20. node js jade tutorial pdf