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
jQuery.parseJSON()
on the result.add_action( 'wp_footer', 'my_ajax_script' ); // Write our JS below here
and it works as I expected. Thank you.