I’m using this example as a guide link to ajax example i’m using to my plugin example. I get Uncaught ReferenceError: the_ajax_script is not defined
in the JS console .
I’ve put my JS in the main php file. The PHP that processes a response in a separate php file, as eventually I’m going to replace this simple example with calculations.
I don’t understand what I’ve done wrong. As far as i’m aware I’ve defined ajax
My code:
activate2.php (main file)
function add_my_css_and_my_js_files(){
wp_enqueue_script('jquery-validate-min', plugins_url('jquery_validate_min.js', __FILE__ ) );
wp_enqueue_script( "the-calcs", plugins_url('the_calcs.php', __FILE__ )) ;
}
add_action('wp_enqueue_scripts', "add_my_css_and_my_js_files");
function include_jQuery() {
if (!is_admin()) {
wp_enqueue_script('jquery');
}
}
add_action('init', 'include_jQuery');
add_action( 'init', 'register_shortcodes' );
function register_shortcodes() {
add_shortcode( 'displaycalcs', 'mp_calcs_display' );
}
function mp_calcs_display() {
echo '<a class="ajax-link" href="#">click me</a>';
$output = <<<HTML
<form action="" method="post" name="formsubmit" id="formsubmit" >
<h1> Process </h1>
<p> operation type always robot </p>
Number of welds: <input type="number" name="numberofwelds" id="numberofwelds" >
Number of construction welds: <input type="number" name="numberofconwelds" id="numberofconwelds" >
Total one: <input type="text" name="totalone" id="totalone" disabled>
<div id="totalfail1"></div>
Total two: <input type="text" name="totaltwo" id="totaltwo" disabled>
<div id="totalfail2"></div>
Total three: <input type="text" name="totalthree" id="totalthree" disabled>
<div id="totalfail3"></div>
<input type="submit" value="Calculate" id="submit" name="submit">
<div id="result"> </div>
</form>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#formsubmit').validate({
rules: {
numberofwelds: "required",
numberofconwelds: "required"
},
messages: {
numberofwelds: "Please enter the number of welds",
numberofconwelds: "Please enter number of con"
},
submitHandler: function(form) {
form.submit();
}
});
$(".ajax-link").click( function() {
var data = {
action: 'test_response',
post_var: 'this will be echoed back'
};
$.post(the_ajax_script.ajaxurl, data, function(response) {
alert(response);
});
return false;
});
});
</script>
HTML;
return $output; }
the_calcs.php (processes the ajax response)
function test_ajax_load_scripts() {
wp_localize_script( 'activate2', 'the_ajax_script', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
add_action('wp_enqueue_scripts', 'test_ajax_load_scripts');
function text_ajax_process_request() {
if ( isset( $_POST["post_var"] ) ) {
$response = $_POST["post_var"];
echo $response;
die();
}
}
add_action('wp_ajax_test_response', 'text_ajax_process_request');
Until ajaxurl is always defined as of WP 2.8 you can simply remove the_ajax_script from here
$.post(the_ajax_script.ajaxurl
.You create the object the_ajax_script for the activate2 script but don’t but the code that uses this object in that file.
Dear you just need to implement this code, afterwords add any form or functionality what you like. This code is 100% working.
First create a ajax.js file in the plugin directory, paste this following jQuery code and save.
The following code goes in the main plugin file.