duancai1904 2017-03-29 18:23
浏览 149
已采纳

WordPress按钮触发了ajax请求

So I have actually read stackoverflow questions about this, but they are somewhat quite old to work with newest version of wordpress.

My end goal is to submit to database some data from my forms but for now ajax response is not working for me. On custom page load in WP all code is loaded so all functions should work. All of this is inside of PHP file for now that why echo is used to create JS scripts. Here's the important part of my code

echo '<button id="ZapisPrace">Save</button>
<script>
jQuery("#ZapisPrace").click(function($){
    var data={
        action: "addToDB",
        info: "nomz"
    };
    jQuery.post(ajaxurl,data,function(response){
    alert("Response was "+ response);
    });
});
</script>';
add_action('wp_ajax_addToDB','pridajDoDB');
function pridajDoDB(){
    echo '<script>console.log("AAA")</script>';
    wp_die();
}

Using current version of WP so variable ajaxurl is pointing to the

/wordpress/wp-admin/admin-ajax.php

No console.log is happening, response is always 0, even when I remove pridajDoDB function or add_action. It's just not triggering the ajax request correctly. Can somebody let me know why? Also I have not used yet functions like wp_localize_script, wp_register_script or wp_enqueue_script because all of this is in one PHP file that's loaded, and I don't need to import jquery as far as I know its default available in WP. I am just learning how to use WP, PHP AJAX and jQuery, so I have still quite a lot to learn.

PS: I am supposed to use the WP way of using ajax.

  • 写回答

2条回答 默认 最新

  • doudou3213 2017-03-31 08:49
    关注

    Ok so I didn't figure out how code above works, however I managed to get it working trough different wp structure I found online:

    BTW: I used onClick function here but it works even when replaced with jQuery click event.

    add_action('wp_ajax_addToDB','pridajDoDB');
    
    echo '<button onClick='triggerAjax()'>Save</button>?>';
    
    <script>
    function triggerAjax(){
        <?php $nonce = wp_create_nonce( 'subbmitData' );?>//used so ajax response can verify from where is the request coming
        jQuery.ajax({
            type: "post",url: "admin-ajax.php",data: { action: 'addToDB', _ajax_nonce: '<?php echo $nonce; ?>' },
            success: function(html){
                    console.log(html);//this will console log everything that happens in ajax called php function. Echo works as well.
                }
            });
        }
    </script>
    <?php
    
    function pridajDoDB(){
        check_ajax_referer( "subbmitData" );//this check from where is the request coming from
        //here database commands works but if you echo or console log something it will be just passed to success function above
        die();
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵