【问题标题】:How to write ajax with php in wordpress?如何在wordpress中用php编写ajax?
【发布时间】:2019-05-16 17:42:37
【问题描述】:

当用户单击我的提交按钮时,我想向基于表单中的信息创建的特殊 URL 发送 POST 请求。我还希望它在 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= "//example.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)。

【问题讨论】:

  • 您是否检查了请求是否到达了 POST URL?我建议使用 wp_enqueue_script()wp_localize_script() 附带的单独 JavaScript 文件通过变量包含 Ajax URL。
  • 请注意,您不应该混合使用 PHP 和 JS - 它们的执行方式不同并且可能导致意外行为,

标签: javascript php mysql ajax wordpress


【解决方案1】:

你为什么在你的javascript代码中调用document.getElementById("form_id").submit()函数?

实际上.submit() 函数会将您重定向到预期的url,并且它下面的行不会被执行。

或者,如果您想重定向到特定页面,则需要在 ajax success 部分的最底部添加代码:

<script type="text/javascript">
function myFunction() {
    var act= "//example.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>

希望对你有帮助

【讨论】:

  • 你试过我上面写的代码了吗?如果您不想重定向,则可以在成功部分删除 document.getElementById() 函数。
  • 我删除了代码document.getElementById("form_id").submit();,但它不起作用但我想重定向一个特定的url
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-26
  • 1970-01-01
  • 1970-01-01
  • 2014-04-07
  • 2014-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多