提问者:小点点

用AJAX请求将表单数据发送到自定义post类型WP


所以我想将表单数据发送到我在WP中创建的自定义post类型,包括名称,电子邮件和消息(文本)。 但是我不能发送它,AJAX请求它不工作。 没有ajax请求,一切都能正常工作。 我错过了什么?

 <div class="group">
        <form id="form" class="form" method="post" action="<?php echo admin_url('admin-ajax.php'); ?>">
            <div class="form-control1">      
                <input type="text" placeholder="Name" id="username" name="nume" />   
                <small>Error message</small>
            </div>
            <div class="form-control1">
                <input type="email" placeholder="Email" id="email" name="email"/>
                <small>Error message</small>
            </div>
            <div class="form-control1">     
                        <textarea class="input" name="mesaj"  id="text" name="mesaj" cols="40" rows="5"  placeholder="Write a nice message for us:)"></textarea>     
                <small>Error message</small>
            </div>    
            <button type="submit" id="submit" name="submit">Submit</button>
        </form>
    </div> 

jQuery:

jQuery('#form').submit(function (event) {
    event.preventDefault();

    var data = $('#form').serialize();
    $.post(window.ajaxObject.ajaxUrl,{
        method: 'POST',
        action: 'messaging_post',
        'data':data,
        success: function (response) { 
            console.log(data);

        }
  })

}); 

和PHP:

function ajax_scripts1() {
        wp_enqueue_script( 'ajax-script', get_template_directory_uri() .'/form.js', array ( 'jquery' ), 1.12, true);
        wp_localize_script( 'ajax-script', 'ajaxObject',
                array( 'ajaxUrl' => admin_url( 'admin-ajax.php') ));
    }
    add_action( 'wp_enqueue_scripts', 'ajax_scripts1' );

    add_action( 'wp_ajax_messaging_post', 'messaging_post' );
    add_action('wp_ajax_nopriv_messaging_post', 'messaging_post');
    function messaging_post(){

        if(isset($_POST['submit']) == '1') {
            $new_post = array(
                  'ID' => '',
                  'post_type' => 'dn_message',
                  'post_status' => 'publish',
                  'post_title' => $_POST['nume'], 
                  'post_content' => $_POST['mesaj'],
                );        
            //here i introduce the data in the custom type post
            $post_id = wp_insert_post($new_post);
            $post = get_post($post_id);

            $field_key1 = "movie_form";
            $value1 = $title;
            $update1 = update_field( $field_key1, $value1, $post_id );  

            $field_key = "email";
            $value = $_POST['email'];
            $update = update_field( $field_key, $value, $post_id );   
              };
    }

共1个答案

匿名用户

尝试以下ajax调用:

jQuery('#form').submit(function(event) {
event.preventDefault();
var data = "action=messaging_post&" + $('#form').serialize();
console.log(data);
jQuery.ajax(
        {
            type: "POST",
            url: window.ajaxObject.ajaxUrl,
            data: data,
            success: function(msg) {
                console.log(msg);
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log(errorThrown);
            }
        });

 });

看起来您正在将wordpress操作视为ajax属性。 Ajax操作应该像这样与FormData一起发送。