I have read this post, Jquery Ajax call in wordpress plugin page not working...
It is very close to my problem.... I have a very basic Wordpress plugin to provide a very specific membership form, it passes payment to PayPal to handle and only emails inputted data. With the same button click of the PayPal button there is also a jQuery script to pickup the submit button click and pass the same data to generate different email message.
This was all working prior to moving it to Wordpress, now under a Wordpress plugin it is all working except for the AJAX function on a submit button click. When the payment form is submitted there is jQuery script to pickup the submit button click and then it sends the payment form data via AJAX.
Here is the js file... When I check the pages source I see the proper Wordpress header line to include the js script and picking the link in the source I get the correct js file. Also the alert("help") pops up also when un-commented.
jQuery(document).ready(function(){
jQuery('#paypal').submit(function(){
//alert("help");
jQuery.ajax({
url : ajax_object.ajaxurl,
type: 'POST',
action: 'memreg_process_request',
//Is this the correct way to pass form data under Wordpress.
data: $(this).serialize(),
success: function( data ){
//Do something with the result from server
console.log( data );
}
});
});
});
Here is the code in myplugin in Wordpress.....
function myplugin_register_script()
{
// Register the style
wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', false, '1.10.2', false) ;
wp_register_script('memreg_process', plugins_url( '/js/memreg.js',__FILE__), false, '1.0.0', false) ;
// enqueing:
wp_enqueue_script('jquery');
wp_enqueue_script('memreg_process');
wp_localize_script( 'ajaxHandle', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin_ajax.php' ) ) );
}
function myplugin_styles()
{
// Register the style
wp_register_style( 'memreg_style', plugins_url('style.css', __FILE__),false, '1.0.0', false );
// enqueing:
wp_enqueue_style( 'memreg_style' );
}
add_action('wp_enqueue_scripts', 'myplugin_register_script');
add_action('wp_enqueue_scripts', 'myplugin_styles');
add_action( 'wp_ajax_memreg_process_request', 'memreg_process_request_wp_ajax_function' );
add_action( 'wp_ajax_nopriv_memreg_process_request', 'memreg_process_request_wp_ajax_function' );
function memreg_process_request_wp_ajax_function(){
$email_to = "[email protected]";
$email_subject = "Testing Email Function from PHP script";
$email_message .= "This is a test message to test AJAX result\n";
$email_message .='Address:'.$_POST["Street"].', '.$_POST["City"].', '.$_POST["State"].' '.$_POST["Zip"]."\r\n";
$email_message .='Phone:'.$_POST["Phone"].'Email:'.$_POST["Email"]."\r\n";
$email_message .='Chapter:'.$_POST["Chapter"]."\r\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
//To send back a response you have to echo the result!
//echo $_POST['Email'];
//echo $_POST['Chapter'];
wp_die(); // ajax call must die to avoid trailing 0 in your response
}
function myform(){
........
}
Again I had all the AJAX portion working before moving the Membership Form in to a Wordpress Plugin. Prior to Wordpress the AJAX just sent the form data to a separate process.php file on the web server to pull out a few fields and generate a email message. Reading the post listed above and many others I am unable to get the AJAX part working under Wordpress.