0

In my jquery script I have;

jQuery.ajax(
        ajaxurl, {
        action: 'lu_ajax',
        status: 'enabled'
        },
        success: function (a) {
        alert(a)
        }

In my file.php i have;

add_action('wp_ajax_lu_ajax', 'lu_ajax');
add_action('wp_ajax_nopriv_lu_ajax', 'lu_ajax'); //for logged users
function lu_ajax() {

    if($_GET['status'] != '') {
            $value = $_GET['status'];
            echo $value;
            setcookie("status", $value, time()+3600, "/");
    }
}

I am including the file.php in the main plugin such as require_once('inc/file.php');

However, no cookie is being set or any value being echoed, can anyone tell me what the issue is please. initially the issue that I had is that I was calling the file directly and caused wp functions to be undefined so i was recommended to use ajaxurl and then the action to perform and the variables i wanted to send over to php, but nothing is happening now.

ps. both jquery and php are in the same file.php i do not have a separate js file as it doesnt fit my purpose.

update new issue As I am making the ajax call from jquery i am firing them up based on conditionals, the following is a sample;

var stat= true;
                jQuery.ajax({
                    type: 'POST',
                    url: ajaxurl,
                    data: {action: 'lu_ajax', status: 'enabled'},
                    success: function (a) {
                        alert(a)
                    }
                });
        } else {
            var stat= false;
            jQuery.ajax({
                    type: 'POST',
                    url: ajaxurl,
                    data: {action: 'lu_ajax', status: 'false'},
                    success: function (b) {
                        alert(b)
                    }
                });
            }

however the cookie is always set to enabled, meaning that the first call is only being made, the problem is not in the condition as i test it by setting it to false manually.

4
  • The wordpress way to handle js is to put in separate js file. Ajax or not. Nobody can force you to do so, but I'll not edit my answer to accomplish a worst practise, sorry...
    – gmazzap
    Commented Sep 13, 2013 at 22:38
  • I have been stuck for 3 days as I am learning how to program, so i have to do loads of research for simple stuff, i know enqueueing is the best practice and way forward, however to fit my purposes i need to print the code in the page not load it in a js file. :( what do you suggest please. Commented Sep 13, 2013 at 22:48
  • You can output the correct url through <?php echo admin_url( 'admin-ajax.php' ); ?> in your file... Commented Sep 13, 2013 at 22:56
  • i already did, var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>'; but i am getting really confused as my jquery code is not in a js file. Commented Sep 13, 2013 at 22:58

2 Answers 2

2

First of all, your js is wrong, you are mixing the syntax of jQuery.post() and of jQuery.ajax()

You should use

jQuery.ajax({
  type: "POST",
  url: ajaxurl,
  data: { action: "lu_ajax", status: "enabled" },
  success: function( data ) {
    alert(data);
  }
});

or

jQuery.post(
  ajaxurl,
  { action: "lu_ajax", status: "enabled" },
  function(data) {
    alert(data);
  }
);

After that the ajaxurl js variable is available only in admin pages, not in frontend.

So the code above will work only in admin pages, but once you use add_action('wp_ajax_nopriv_lu_ajax', 'lu_ajax'); I think you want to use ajax in frontend, in this case you have to pass the ajax url to the script via wp_localize_script.

So if the js script in which reside the js code above is names 'my_ajax.js' and resides in the 'js' subfolder of your plugin, do something like this

add_action('wp_enqueue_scripts', function() {
  wp_enqueue_script('my-ajax', plugins_url('js/my_ajax.js', __FILE__) );
  wp_localize_script(
    'my-ajax',
    'myAjaxData',
     array( 'ajaxurl' => admin_url('admin-ajax.php') )
  );
});

Now the in the js code above replace ajaxurl with myAjaxData.ajaxurl

8
  • how about if I have the js being output by a function in the same php file, i do not have a js file. Commented Sep 13, 2013 at 22:33
  • @GM i am not using a form, so i guess i dont need to use post? apparently $_GET is by default, how can i grab the value sent by the ajax call then? Commented Sep 13, 2013 at 23:05
  • @JoseDavidGarciaLlanos who said the $_POST is only for form? POST should be used every time you send information/data to server. GET when you require information/data from server. However, to check data passed via POST method check the global $_POST array, exactly how you use $_GET.
    – gmazzap
    Commented Sep 13, 2013 at 23:30
  • ah thanks, im gona test it i think im going crazy, please bear with me this its been 5 days since i started php ajax and jquery, does it matter if I use double quotes or single quotes? Commented Sep 13, 2013 at 23:59
  • OMG IT FINALLY WORKED, the success function is firing up and the cookie is being set.but when the alert is popsup it has a 0 at the end "enabled0" where is the zero coming from? Commented Sep 14, 2013 at 0:12
1

ajaxurl is set only in admin. If this is a frontend ajax (looks like it due to nopriv), you have to localize the script on enqueueing:

add_action( 'wp_enqueue_scripts', 'my_enqueue' );
function my_enqueue($hook) {

    wp_enqueue_script( 'ajax-script', plugins_url( '/js/my_query.js', __FILE__ ), array('jquery'));

    // in javascript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
    wp_localize_script( 'ajax-script', 'ajax_object',
            array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
}

In the JS file, you can reach the ajax_url as a variable of ajax_object:

jQuery.ajax(
        ajax_object.ajax_url, {
        action: 'lu_ajax',
        status: 'enabled'
        },
        success: function (a) {
        alert(a)
        }

See http://codex.wordpress.org/AJAX_in_Plugins

3
  • thanks for that, but i do not have a js file, the javascript is being output by a function in the same php file Commented Sep 13, 2013 at 22:34
  • 1
    I got notified of your answer when I ended to write the mine. Normally I'd not post after seen your answer (that's is generally correct) but you mantain the OP error in js code and you use admin_enqueue_scripts to enqueue the script, after said is frontend... For these to reasons I decided to post my answer, sorry.
    – gmazzap
    Commented Sep 13, 2013 at 22:35
  • Thank you for your note! You're right..Fixing the answer Commented Sep 13, 2013 at 22:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.