I am developing a wordpress plugin where I use an ajax call to submit form data, it was working fine and then all of a sudden I get a console error saying that ajaxurl (the url passed to the ajax post) is not defined? My jQuery function:

 $(".button").click(function() {


            var name = $(":input.name").val();
            if (name == "") {

                return false;

            var email = $(":input.email").val();
            if (email == "") {

                return false;

            var phone = $(":input.phone").val();
            if ((phone == "") || (isNaN(phone))) {

                return false;

            var info = 'name:' + name + '&email:' + email + '&phone:' + phone;
            var ajaxurl = '<?php echo admin_url("admin-ajax.php"); ?>';


                url: ajaxurl,
                data: {action: 'submit_data', info: info},
                success: function(response) {
                    if (response.type == "success") {

                    else {


            return false;               


The var ajaxurl = ''; line is a solution I just found on the web but this now leaves me with an error of 403 forbidden.

  • Try var ajaxurl = '<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>';
    – user27457
    Apr 3, 2014 at 20:31
  • 403 forbidden was sent by the HTTP server so your PHP code did execute it just generated a bad URL.
    – user27457
    Apr 3, 2014 at 20:39
  • Strangely enough I managed to fix it by changing 'url: ajaxurl' to 'url: myAjax.ajaxurl' ... Thanks for the help though peeps
    – Bob
    Apr 3, 2014 at 21:40
  • If you have the time, I would be interested in knowing if the change I suggested would also work. If you are too busy don't worry about it - I am just curious.
    – user27457
    Apr 3, 2014 at 22:07
  • Where is this code? is it in a .js file? Inline? Is the file an AJAX handler? A page template? More details are necessary. As a sidenote, you shouldn't be able to put PHP tags inside a javascript file. If you can then you've either not told us something or something is very wrong
    – Tom J Nowell
    Aug 15, 2014 at 18:16

1 Answer 1


take a look here:


First add Data in your php file

// Register the script first.

wp_register_script( 'some_handle', 'path/to/myscript.js' );

// Now we can localize the script with our data.

$translation_array = array( 'some_string' => __( 'Some string to translate' ), 'a_value' => '10' );

wp_localize_script( 'some_handle', 'object_name', $translation_array );

// The script can be enqueued now or later.
wp_enqueue_script( 'some_handle' );


After that you can use the data in js


// alerts 'Some string to translate'

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.