I am trying to retrieve a user's email and store it in the database using Ajax. I can see the form Values in the console after the ajax request,but it doesn't store the data in the database. Everything else works fine,except that nothing is stored in the Database.Any help with be much appreciated. You can test this using any default Wordpress TwentyFourteen installation. I am providing the following files. .subsc.js is the subscription script, the functions to send info is in the functions.php file and my subscription widget has been included. Copy and past
class subscribeFm extends WP_Widget {
/**
* Register widget with Wordpress
*/
function __construct() {
parent::__construct(
'subscribe_widget', // Base ID
__( 'Newsletter Subscription', 'text_domain' ), // Name
array( 'description' => __( 'Displays Subscription form', 'text_domain'), ) // args
);
}
/* Front end Display of widget */
public function widget($args, $instance) {
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
?>
<div>
<form id="formsub" name="subscribe" method="POST" onsubmit="return validateSub(this)">
<input id="subEmail" name="subEmail" class="input-text" placeholder="Your Email Address">
<button type="submit">Signup</button>
<p id="notifEmail" style="color:#fff"></p>
<i class="fa fa-chevron-right"></i>
</form>
</div>
<?php
}
// echo __('Subscribe to our mailing list', 'text_domain');
echo $args['after_widget'];
}
public function form( $instance ) {
$title =! empty( $instance['title'] ) ? $instance['title'] : __('New title', 'text_domain' );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"> <?php _e( 'Title'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" >
</p>
<?php
}
/**
* Sanitize widget form values as they are saved
*
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
?>
Add this lines to function twentyfourteen_scripts() in functions.php
/* Email Subscription queue */
wp_enqueue_script('subsc_Scripts',get_template_directory_uri() . '/js/Subsc.js', array('jquery' ), 1.0, true );
wp_localize_script('subsc_Scripts', 'MySubsc', array('ajaxurl' => admin_url('admin-ajax.php')));
the following code processes the returned data in functions.php
add_action( 'wp_ajax_ph_process', 'ph_process_fm' ); // ajax for logged in users
add_action( 'wp_ajax_nopriv_ph_process', 'ph_process_fm' ); // ajax for not logged in users
// Process submission after Ajax request
function ph_process_fm($info) {
global $wpdb;
if(isset($_POST['subEmail'])) {
$subscripEmail = $_POST['subEmail'];
echo $subscripEmail . " </br>";
// $nSubEmail = $wpdb->real_escape_string($subEmail);
$wpdb->insert('wp_subscription_email', array('email' => $subscripEmail ), array( '%s' ) );
}
die();
}
This is the Javascript Code
function validateSub() {
//"use strict";
var x, exp, result, atpos, dotpos, formData;
formData = jQuery("#formsub").serialize();
x = document.subscribe.subEmail.value;
exp = /(\s|.$|.%)@(?=.)/gmi;
atpos = x.indexOf("@");
dotpos = x.lastIndexOf(".");
result = exp.test(x); // check Reg exp on input value
if (result === true || (atpos < 1 || dotpos < atpos + 2)) {
document.getElementById("notifEmail").innerHTML = "Invalid Email address"; //display error messages
document.getElementById("notifEmail").style.color="#FCFCFC";
ERR = 1;
return false;
}
else {
jQuery.ajax({
url: MySubsc.ajaxurl,
type: 'POST',
action: 'ph_process_fm',
data: formData,
success:function(response){
alert("Check your email to confirm this subscription");
alert(formData);
}
});
return false;
}
}
It will result success because the url is correct.
The error in your code is in here action: 'ajaxurl',
add add_action( 'wp_ajax_ajaxurl', 'my_action_callback' );
to your ajaxurl
and function my_action_callback(){}