在AJAX插入后查询数据

时间:2021-11-27 作者:juan

在使用Ajax成功插入一个条目后,我希望看到该条目的ID和url是什么,并在模式窗口中显示它

有没有办法得到这些数据?

<script>
    ajax_url = "<?php echo admin_url(\'admin-ajax.php\'); ?>";
</script>
<script>
    $("#enquiry_email_form").on("submit", function (event) {
        event.preventDefault();

        var form= $(this);
        var ajaxurl = form.data("url");
        var detail_info = {
            post_title: form.find("#post_title").val(),
            post_description: form.find("#post_description").val()
        }

        if(detail_info.post_title === "" || detail_info.post_description === "") {
            alert("Fields cannot be blank");
            return;
        }

        $.ajax({

            url: ajaxurl,
            type: \'POST\',
            data: {
                post_details : detail_info,
                action: \'save_post_details_form\' // this is going to be used inside wordpress functions.php
            },
            error: function(error) {
                alert("Insert Failed" + error);
            },
            success: function(response) {
                modal.style.display = "block";

                body.style.position = "static";
                body.style.height = "100%";
                body.style.overflow = "hidden";   
                    
            }
        });
    })
</script>

<button id="btnModal">Abrir modal</button> 
<div id="tvesModal" class="modalContainer">
    <div class="modal-content">
        <span class="close">×</span>
        <h2>Modal</h2>
        <p><?php ***echo $title_post, $url, $ID*** ?></p> 
    </div>
</div> 

function.php

function save_enquiry_form_action() {
 
    $post_title = $_POST[\'post_details\'][\'post_title\'];
    $post_description = $_POST[\'post_details\'][\'post_description\'];
    $args = [
        \'post_title\'=> $post_title,
        \'post_content\'=>$post_description,
        \'post_status\'=> \'publish\',
        \'post_type\'=> \'post\',
        \'show_in_rest\' => true,
        \'post_date\'=> get_the_date()
    ];
 
    $is_post_inserted = wp_insert_post($args);
 
    if($is_post_inserted) {
        return "success";
    } else {
        return "failed";
    }
}

1 个回复
SO网友:FS-GSW

功能wp_insert_post() 成功时返回post ID,错误时返回值0。

您的PHP函数应该如下所示:

function save_enquiry_form_action() {
    $args = [
        // Your postdata...
    ];
 
    $is_post_inserted = wp_insert_post($args);
 
    if( !empty($is_post_inserted) ) {
        wp_send_json_success([ 
            \'post_id\' => $is_post_inserted,
            \'post_title\' => get_the_title( $is_post_inserted ),
        ]);
    } else {
        wp_send_json_error();
    }
}
最后,在Ajax返回中,我们得到要发送到弹出窗口的信息。

<script>
    $.ajax({
        [...] // everything before success function was good. 

        success: function(response) {
            if ( response.success == true ) {
                // in case of `wp_send_json_success()`

                var post_id = response.data.post_id;
                var post_title = response.data.post_title;
                $(\'#tvesModal .modal-content p\').html(post_id + "<span>" + post_title + "</span>");
                
                modal.style.display = "block";
                body.style.position = "static";
                body.style.height = "100%";
                body.style.overflow = "hidden";  
            } else {
                // in case of `wp_send_json_error()`
                alert("Insert Failed");
            }   
        }
    });
</script>

相关推荐

在PHP函数中使用来自AJAX调用的值

我有一个ajax脚本,它将从onchange上的select from中获取一个值。这在控制台上运行良好。日志我可以看到正确的值。我的select表单会触发重新加载,在重新加载页面之前,我会在控制台中看到该值。我想使用此值为WPML设置后端语言,但不确定如何使用该值。PHPfunction my_action( ) { $dropdown_shop_order_language = $_POST[\'dropdown_shop_order_language\'];