I have a plugin tat extendeds Gravity Forms consisting of one PHP file and a JS file.

The PHP file can be found here: http://pastebin.com/VUX5AfLP

The JS File can be found here: http://pastebin.com/CTjQEKzY

I am trying to use WP AJAX with callback to return json encoded results to populate my Gravity Form.

The issue I am having is that instead of returning the JSON encoded object to the AJAX callback, I am getting the entire injuryFilter(); function from the <script> to the </script> tags like so in addition to the json_encoded output:

        injuryFilter = function () {

var injuryClass = ".injury-list select";
var minClass = ".min-value input";
var maxClass = ".max-value input";
var results;

    jQuery(injuryClass).on("change", function () {
    var injurySelect = jQuery(this);
    injury = injurySelect.val();

            if (injury != '') {
                        var data = {
                    'action': 'get_injury_data',
                    'id': injury

                    jQuery.post('', data, function(response) {


                                                //results = jQuery.parseJSON( response );

                    //  jQuery(minClass).val(response.min);
                    //  jQuery(maxClass).val(response.max);






Granted this is my first foray into AJAX and Plugins in Wordpress I am sure I am doing something wrong or in the wrong place.

I am just looking to get the response from AJAX so that can work with it. If I have files or functions out of place, please be specific as to where I went wrong.


  • Which explains why I couldn't use jQuery.parseJSON() on the result. Feb 6, 2015 at 22:15
  • Where is that script tag supposed to appear? It should be added to an enqueued js file or at the minimum placed within a wp_head action call. Feb 6, 2015 at 22:27
  • all that javascript is sitting in your plugin file outside of any sort of function, which means it's output on every request- front end, back end, and ajax.
    – Milo
    Feb 6, 2015 at 22:33
  • Yes, you guys were right on. I added it inside a function and then added an action hook for add_action( 'wp_footer', 'my_ajax_script' ); // Write our JS below here and it works as I expected. Thank you. Feb 6, 2015 at 22:35

1 Answer 1


Wrapping it in a function and then adding the following did the trick:

add_action( 'wp_footer', 'my_ajax_script' ); // Write our JS below here Was all it took

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.