0

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:

<script>
        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('http://10.200.152.19/courses/wp-admin/admin-ajax.php', data, function(response) {

                                                console.log(response);
                                                //alert("Done");



                                                //results = jQuery.parseJSON( response );
                                                //alert(response[0].min);

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

                    });

                }
    });



}

</script>




[{"min":"100000","max":"150000"}]

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.

Thanks

  • Which explains why I couldn't use jQuery.parseJSON() on the result. – Requin Creative Feb 6 '15 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. – czerspalace Feb 6 '15 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 '15 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. – Requin Creative Feb 6 '15 at 22:35
0

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, privacy policy and cookie policy

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