Wordpress数据库没有插入通过Ajax发布的值

时间:2014-12-10 06:13:48

标签: javascript php jquery ajax wordpress

我正在尝试检索用户的电子邮件并使用Ajax将其存储在数据库中。我可以在ajax请求之后在控制台中看到表单值,但它不会将数据存储在数据库中。其他一切工作正常,除了数据库中没有任何内容。非常感谢帮助。您可以使用任何默认的Wordpress TwentyFourteen安装进行测试。我提供以下文件。 .subsc.js是订阅脚本,发送信息的函数在functions.php文件中,我的订阅小部件已包含在内。复制并过去

  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;
    }
}


?>

将这些行添加到functions.php

中的函数twentyfourteen_scripts()中
/* 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'))); 

以下代码处理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();

}

这是Javascript代码

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;
    }
}

1 个答案:

答案 0 :(得分:0)

这将导致成功,因为网址是正确的。

您的代码中的错误位于此处action: 'ajaxurl',

add_action( 'wp_ajax_ajaxurl', 'my_action_callback' );添加到您的ajaxurlfunction my_action_callback(){}