删除按钮不通过AJAX发送数据

发布于 2025-02-08 09:35:50 字数 1262 浏览 4 评论 0原文

我的WordPress帐户的后端在自定义管理页面上有多个删除按钮,这些按钮会影响公共网页。在我的html文件中,我得到了:

< td>< butt value =“ 2” onclick =“ del(this.value)”> delete project project 2 names&lt</button>>>/td</td> /code>

进行脚本时

<script>        
        var ajaxurl = '<?=admin_url('admin-ajax.php'); ?>';
        function del(val){
                        var CellNum = val;
                        console.log(CellNum);
                        // url of the data that is to be delete
                        
                        $.post(ajaxurl, {
                            
                            action: 'DeleteProject',
                            data: CellNum,
                            
                        }), function (data, status) {};
                      }


</script>

然后在我的custom_function.php中

function DeleteProject()
  { 
    global $wpdb;
    
    $DelNum = $_POST['data'];
    echo $DelNum;
    print_r($DelNum);

    $wpdb->query($wpdb->prepare('DELETE `fxq4_projectsignup1` WHERE ProjectNumber = %d', $DelNum));

    die();

  }

:我在console.log中看到cellnum,但没有迹象表明正在输入deleteproject函数。

有线索吗?

非常感谢!

前夕

I have multiple delete buttons on a custom admin page in the backend of my wordpress account which effect a public webpage. In my html file I've got:

<td><button value = "2" onclick = "del(this.value)">Delete Project 2 Names</button></td>

with my script being

<script>        
        var ajaxurl = '<?=admin_url('admin-ajax.php'); ?>';
        function del(val){
                        var CellNum = val;
                        console.log(CellNum);
                        // url of the data that is to be delete
                        
                        $.post(ajaxurl, {
                            
                            action: 'DeleteProject',
                            data: CellNum,
                            
                        }), function (data, status) {};
                      }


</script>

Then in my custom_function.php:

function DeleteProject()
  { 
    global $wpdb;
    
    $DelNum = $_POST['data'];
    echo $DelNum;
    print_r($DelNum);

    $wpdb->query($wpdb->prepare('DELETE `fxq4_projectsignup1` WHERE ProjectNumber = %d', $DelNum));

    die();

  }

I am seeing the CellNum in the console.log but there is no sign that the DeleteProject Function is being entered.

Any clues?

Thank you very much!

Eve

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

硬不硬你别怂 2025-02-15 09:35:50

问题将是您注册ID button1 的单击事件,但不存在。您的jQuery.post也有拼错的成功回调。

尝试此代码:

<button value="2" id="button1">Delete Project 2 Names</button>

<script>
    var ajaxurl = '<?=admin_url("admin-ajax.php"); ?>';
    
    jQuery(document).ready(function ($) {
        $('#button1').click(function () {
            let CellNum = $(this).attr("value");
    
            // url of the data that is to be delete
            $.post(ajaxurl, {
                action: 'DeleteProject',
                data: CellNum,
            }, function (data, status) {
                // success callback
            });
        });
    });
</script>

The problem will be that you register the click event for id button1, but it does not exist. You also have a misspelt success callback for the jQuery.post.

Try this code:

<button value="2" id="button1">Delete Project 2 Names</button>

<script>
    var ajaxurl = '<?=admin_url("admin-ajax.php"); ?>';
    
    jQuery(document).ready(function ($) {
        $('#button1').click(function () {
            let CellNum = $(this).attr("value");
    
            // url of the data that is to be delete
            $.post(ajaxurl, {
                action: 'DeleteProject',
                data: CellNum,
            }, function (data, status) {
                // success callback
            });
        });
    });
</script>
强辩 2025-02-15 09:35:50

最后,我重新调整了代码,可能存在问题在“ onClick”属性内的创建函数。现在我有:

<html>
<td><button id = button2 value = 2>Delete Project 2 Names</button></td>
</html>

<script>
var ajaxurl = '<?=admin_url("admin-ajax.php"); ?>';
          jQuery(document).ready(function ($) {
$('#button1').click(function(){          
                  
              let CellNum = $("#button1").attr("value");
                  console.log(CellNum);
                  // url of the data that is to be delete
                  
                  $.post(ajaxurl, {
                      
                      action: 'DeleteProject',
                      data: CellNum,
                      
                  }), function (data, status) {
                  };
                });   
        });

          
      </script>

In the end I've rejigged the code, potentially the problem lay in the creating the function inside the 'onclick' attribute. Now I've got:

<html>
<td><button id = button2 value = 2>Delete Project 2 Names</button></td>
</html>

<script>
var ajaxurl = '<?=admin_url("admin-ajax.php"); ?>';
          jQuery(document).ready(function ($) {
$('#button1').click(function(){          
                  
              let CellNum = $("#button1").attr("value");
                  console.log(CellNum);
                  // url of the data that is to be delete
                  
                  $.post(ajaxurl, {
                      
                      action: 'DeleteProject',
                      data: CellNum,
                      
                  }), function (data, status) {
                  };
                });   
        });

          
      </script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文