如何在wordpress中用php编写ajax?

hgqdbh6s  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(284)

当用户单击我的提交按钮时,我想向一个特殊的url发送post请求,该url是根据表单中的信息创建的。我还想让它在wordpress mysql数据库中插入一行。
我附加到结尾 admin-ajax.php :

function add_query_db_callback(){

global $wpdb;

    $id_instagram = $_POST['id_instagram'];
    $table_name = 'wp_insta_email';

        $data_array = array(
            'id_instagram' => $id_instagram
        );

        $rowResult = $wpdb->insert($table_name, $data_array, $format=NULL);

echo $_POST['data'];

if (!$rowResult) {
    echo "FAILED TO UPDATE";
} else {
    $rowResult;
    echo "WILL UPDATE SUCCESSFULLY - CALL RESULT FUNCTION";
};

wp_die();
die();

}`

在文件中 header.php :

<!-- Search_box --> 
<div class="s006">
    <form  method="post" id="form_id" onsubmit="return myFunction()">
        <fieldset>
            <div class="inner-form">
                <div class="input-field">
                    <button class="btn-search" type="button submit" value="Submit" name="BtnSubmit">
                        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24">
                            <path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
                        </svg>
                    </button>
                    <input id="search" type="text" placeholder=" INPUT YOUR ID INSTAGRAM " value="" name="id_instagram"/>
                </div>
            </div>
        </fieldset>
    </form>
</div>

以及

<script type="text/javascript">
function myFunction() {
    var act= "//app.neolyze.com/public/"+document.getElementById("search").value;
    var name = document.getElementById("search").value;
    document.getElementById("form_id").action = act;
    document.getElementById("form_id").submit();
    if (name == '') {
        alert("Please Fill All Fields");
    } else {
        // AJAX code to submit form.
        var ajaxData = {
            'action': 'add_query_db',
            'id_instagram': name
        }
        jQuery.ajax({
            type: "POST",
            url: '<?php echo admin_url('admin-ajax.php'); ?>',
            data: ajaxData,
            success: function( response ) {
                console.log("Data returned: " + response );
                $statusSelectCell.parent().css({"background-color": "#b3e6b3"});
                $statusSelectCell.parent().animate({backgroundColor: currentBackgroundColor}, 1200);
            },
            error: function() {
                alert("FAILED TO POST DATA!!");
            }

    });
    }
    return act;
}
</script>

但它不会保存到我的数据库(只传递到我想要的url)。

roqulrg3

roqulrg31#

为什么要在javascript代码中调用document.getelementbyid(“form\u id”).submit()函数?实际上,submit()函数会将您重定向到所需的url,并且下面的行不会被执行。或者,如果您想重定向到特定的页面,那么您需要在ajax成功部分的最底部添加代码

<script type="text/javascript">
function myFunction() {
    var act= "//app.neolyze.com/public/"+document.getElementById("search").value;
    var name = document.getElementById("search").value;
    document.getElementById("form_id").action = act;
    // document.getElementById("form_id").submit();
    if (name == '') {
        alert("Please Fill All Fields");
    } else {
        // AJAX code to submit form.
        var ajaxData = {
            'action': 'add_query_db',
            'id_instagram': name
        }
        jQuery.ajax({
            type: "POST",
            url: '<?php echo admin_url('admin-ajax.php'); ?>',
            data: ajaxData,
            success: function( response ) {
                console.log("Data returned: " + response );
                $statusSelectCell.parent().css({"background-color": "#b3e6b3"});
                $statusSelectCell.parent().animate({backgroundColor: currentBackgroundColor}, 1200);

                // if you want to be redirected place submit function here
                document.getElementById("form_id").submit();
            },
            error: function() {
                alert("FAILED TO POST DATA!!");
            }

    });
    }
    return act;
}
</script>

希望这对你有帮助

相关问题