When the user clicks the 'submit' button on my form, I would like 'this will be echoed back' to be displayed within an alert.
The only JS console error I get:
Failed to load resource http://nikkomsgchannel/e?0017005e002b00560036004400190026002d0025002c005300…240020003700620024005b004a005500790053002c0045002b003600300026002f005d004c
The only code I've changed is the Jquery, before this I was using a hyperlink & click
event in Jquery, which was working.
My code:
PHP file
function add_my_css_and_my_js_files(){
wp_enqueue_script( "ajax-test", plugin_dir_url( __FILE__ ) . '/ajax-test.js', array( 'jquery' ) );
wp_localize_script( 'ajax-test', 'the_ajax_script', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
add_action('wp_enqueue_scripts', "add_my_css_and_my_js_files");
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');
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() {
$output = <<<HTML
<form action="" method="post" name="formsubmit" id="formsubmit" >
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>
HTML;
return $output;
}
ajax-test.js
jQuery(document).ready( function($) {
$("#submit").submit( 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;
});
});
wp_localize_script
. Not to be paranoid, but I can't help but think you have a malware problem.