简体   繁体   English

从jQuery / AJAX请求执行PHP文件

[英]Executing a PHP file from a jQuery / AJAX request

I'm making a chat function where staff members with the appropriate privileges can delete a line that has been posted to the chat room. 我正在创建一个聊天功能,具​​有适当特权的工作人员可以删除已发布到聊天室的一行。

I'd like this function to work without refreshing the page, but I can't seem to get the jQuery / AJAX part of it right? 我希望该功能在不刷新页面的情况下就可以工作,但是我似乎无法正确理解jQuery / AJAX部分吗? Nothing happens at all. 什么都没发生。

As a jQuery newbie I was hoping that someone could point out the line that I'd have to look at? 作为jQuery新手,我希望有人可以指出我必须要看的那一行?

a href that needs to be clicked to delete line: 需要单击以删除行的href:

for (var i in json.lines) {
    lines.push("[<a class='delete' href='#' data-lineID='" + json.lines[i].id + "'>x</a>] " + json.lines[i].user + json.lines[i].divide + " " + json.lines[i].message);
}

jQuery: jQuery的:

$('a.delete').on('click', function(event) {
var id = $(this).data('lineID');

    /* Request to .php file */
    var request = $.ajax({
    url: "/communications/chat.php?page=delete",
    type: "POST",
    data: { id : id },
    dataType: "html"
    });

  event.preventDefault();
  return false;

}); });

Part of the PHP script to execute: 要执行的PHP脚本的一部分:

case 'delete' :

if(isset($_POST['id']) && !empty($_POST['id'])) {
    $id = $_POST['id'];

    try {
        $query = $udb->prepare("UPDATE `chat_lines` SET `deleted` = '1' WHERE `id` = ?");
        $query->bindValue(1,$id);
        $query->execute();
    } catch (Exception $e) {
        die($e->getMessage());
    }
}

break;

Any help is appreciated :) 任何帮助表示赞赏:)

This could be a long shot, but I think that your issue is caused by HTML element not being there when you load the Javascript. 这可能需要花费很长时间,但是我认为您的问题是由加载Javascript时HTML元素不存在引起的。
jQuery will attach an event listener (in this case the click one) when the script is loaded. 加载脚本时,jQuery将附加一个事件侦听器(在本例中为click事件)。
If the element is added after the script loads, the listener is not attached so it won't trigger your code. 如果在脚本加载添加了元素,则不会附加侦听器,因此不会触发您的代码。

What you have to do is to use event delegation . 您要做的就是使用事件委托 This means that you have to change your javascript code to this: 这意味着您必须将javascript代码更改为此:

$('#mainContainerOfYourChat').on('click', 'a.delete', function(event) {
    var id = $(this).data('lineID');

    /* Request to .php file */
    var request = $.ajax({
            url: "/communications/chat.php?page=delete",
            type: "POST",
            data: { id : id },
            dataType: "html"
        });

  event.preventDefault();
  return false;
});

In this way jQuery will listen to the main container of your page, when a click is fired, if it matches the a.delete selector, it will trigger your code. 这样,jQuery将侦听页面的主容器,当触发单击时,如果它与a.delete选择器匹配,它将触发您的代码。

If your PHP is deleting the post from the DB your only problem is it stays loaded on the client side until you reload the page. 如果您的PHP正在从数据库中删除帖子,则唯一的问题是它会一直加载在客户端,直到您重新加载页面为止。 If that is so you'll only need to include a line to remove the comment if the ajax call is successful: 如果是这样,那么只要ajax调用成功,您只需添加一行即可删除注释:

var request = $.ajax({
    url: "/communications/chat.php?page=delete",
    type: "POST",
    data: { id : id },
    dataType: "html",
    success: function(){
      $("a.delete[data-lineID="+id+"]").parent().remove();
     }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM