如何在*上发表评论?

时间:2022-01-09 08:18:50

How can I make comments like on *?

如何在*上发表评论?

What I mean more specificly, I use php/mysql how would I add a comment without reloading the page, I know this is a simple process of using AJAX but when you post a comment on SO it also has the option to delete it right away, when I insert comments into my DB there ID number is auto increment, so my real question now is

我的意思更具体地说,我使用php / mysql如何在不重新加载页面的情况下添加注释,我知道这是一个使用AJAX的简单过程但是当你在SO上发表评论时它也可以选择立即删除它当我在我的数据库中插入注释时,ID号是自动增量的,所以现在我真正的问题是

After posting the comment, is the comment posted, just added to the page with some javascript to append the contents to the page

发布评论后,评论已发布,只是添加到页面中,并附带一些javascript以将内容附加到页面

OR

Does it use AJAX to retrieve that comment you just posted and then display it on the page?

它是否使用AJAX检索您刚发布的评论然后在页面上显示?

The reason I am wondering is because since my system uses auto increment ID then if I did it the first method which would be faster to just ad the contents I posted on submit but this method would not give me the ID which is needed to be able to delete the comment by ID number

我想知道的原因是因为我的系统使用了自动增量ID,那么如果我这样做了第一种方法,只需更新我在提交时发布的内容,但这种方法不会给我需要的ID按ID号删除评论

Hope that makes sense

希望有道理

UPDATE I posted below what I think now after reading other post on here

更新我在这里阅读其他帖子之后发布了我现在想的内容

6 个解决方案

#1


5  

I'm going to go out on a limb and say it's right in between the two. The content of the comment is just posted from what you typed...

我要走出去,说两者之间是正确的。评论的内容仅根据您输入的内容发布...

...but the page waits to append your comment until the AJAX magic has occurred and the page has the ID of your new comment.

...但页面等待附加您的评论,直到AJAX魔法发生并且页面具有您的新评论的ID。

(that should read: Here's how I would do it if I were you...fast, lightweight, and functional)

(应该是这样的:如果我是你,我会怎么做...快速,轻便,功能齐全)

#2


2  

My guess is that after the page does an AJAX post to add the comment, it waits for a response from the server that gives it the id of the comment, then makes another AJAX call to render the comment based on the id returned. If you wanted to do it without retrieving the comment back from the server, you definitely could by just adding the comment via javascript. However things like user profile links might be a bit tedious to inject.

我的猜测是,在页面执行AJAX帖子添加注释后,它等待来自服务器的响应,它给出了注释的id,然后根据返回的id进行另一个AJAX调用以呈现注释。如果你想在不从服务器返回评论的情况下这样做,你绝对可以通过javascript添加评论。但是,用户配置文件链接之类的内容可能会有点繁琐。

Edit: An easier method would be to have the first AJAX call return the HTML necessary to render the entire comment, then inject that response straight into the page. This would remove the need for 2 AJAX calls.

编辑:一个更简单的方法是让第一个AJAX调用返回呈现整个注释所需的HTML,然后将该响应直接注入页面。这将消除2个AJAX调用的需要。

#3


1  

I would try something like this using jQuery:

我会尝试使用jQuery这样的东西:

function commentSubmit()
{
  $.post('/ajax/comment',{comment:$('#comment').val()},function(d){
    if(d is error) alert(d);
    else $('#allcomments').append(d);
  })
}

Where d can be error message or html with comment.

其中d可以是错误消息或带注释的html。

#4


0  

I would do an ajax request to a php script which would add the comment to the mysql database. The ajax callback would retrieve the ID and add the comment visually to the page (without a reload). The delete button would do another ajax request to a php script passing the ID to the script. The callback would remove the comment from the page.

我会对php脚本执行ajax请求,该脚本会将注释添加到mysql数据库中。 ajax回调将检索ID并以可视方式将注释添加到页面(无需重新加载)。删除按钮将对php脚本执行另一个ajax请求,将ID传递给脚本。回调将从页面中删除评论。

#5


0  

If you want to be able to delete the comment, you must communicate with the server, so you must use ajax. Just send the comment to the server, wait for for the comments ID to be returned, then format the comment into some HTML, hide the new comment ID somewhere in the new HTML (probably in the link to delete the comment) and wala.

如果您希望能够删除注释,则必须与服务器通信,因此必须使用ajax。只需将注释发送到服务器,等待返回注释ID,然后将注释格式化为某些HTML,在新HTML中隐藏新注释ID(可能在删除注释的链接中)和wala。

#6


0  

UPDATE

After reading answers here is my basic idea, I don't know how to write javascrpt but you will get the design flow idea:

在阅读答案后,这是我的基本想法,我不知道如何编写javascrpt,但你会得到设计流程的想法:

POST comment input to script with AJAX {    
    if commentID is returned{
        Insert comment DIV into page, userID and userPicture are in a user session variable and commentID is returned from ajax post
    }ELSE IF error is returned{
        Insert an error message DIV into page
    }
}

Then the comment div or whatever that is inserted would include
- a users name from session variable
- userID from session variable for linking to there profile
- user picture from session variable
- comment ID for allowing the user to delete that comment

然后注释div或插入的任何内容将包括 - 来自会话变量的用户名 - 来自会话变量的userID,用于链接到那里的配置文件 - 来自会话变量的用户图片 - 用于允许用户删除该注释的注释ID

Then for deleting ANY comment on the page that a user published

然后删除用户发布的页面上的任何评论

POST comment ID and userID to DELETION script with AJAX {    
    if commentID is deleted{
        REMOVE comment DIV from page
    }ELSE IF error is returned{
        Insert an error message DIV into page
    }
}

#1


5  

I'm going to go out on a limb and say it's right in between the two. The content of the comment is just posted from what you typed...

我要走出去,说两者之间是正确的。评论的内容仅根据您输入的内容发布...

...but the page waits to append your comment until the AJAX magic has occurred and the page has the ID of your new comment.

...但页面等待附加您的评论,直到AJAX魔法发生并且页面具有您的新评论的ID。

(that should read: Here's how I would do it if I were you...fast, lightweight, and functional)

(应该是这样的:如果我是你,我会怎么做...快速,轻便,功能齐全)

#2


2  

My guess is that after the page does an AJAX post to add the comment, it waits for a response from the server that gives it the id of the comment, then makes another AJAX call to render the comment based on the id returned. If you wanted to do it without retrieving the comment back from the server, you definitely could by just adding the comment via javascript. However things like user profile links might be a bit tedious to inject.

我的猜测是,在页面执行AJAX帖子添加注释后,它等待来自服务器的响应,它给出了注释的id,然后根据返回的id进行另一个AJAX调用以呈现注释。如果你想在不从服务器返回评论的情况下这样做,你绝对可以通过javascript添加评论。但是,用户配置文件链接之类的内容可能会有点繁琐。

Edit: An easier method would be to have the first AJAX call return the HTML necessary to render the entire comment, then inject that response straight into the page. This would remove the need for 2 AJAX calls.

编辑:一个更简单的方法是让第一个AJAX调用返回呈现整个注释所需的HTML,然后将该响应直接注入页面。这将消除2个AJAX调用的需要。

#3


1  

I would try something like this using jQuery:

我会尝试使用jQuery这样的东西:

function commentSubmit()
{
  $.post('/ajax/comment',{comment:$('#comment').val()},function(d){
    if(d is error) alert(d);
    else $('#allcomments').append(d);
  })
}

Where d can be error message or html with comment.

其中d可以是错误消息或带注释的html。

#4


0  

I would do an ajax request to a php script which would add the comment to the mysql database. The ajax callback would retrieve the ID and add the comment visually to the page (without a reload). The delete button would do another ajax request to a php script passing the ID to the script. The callback would remove the comment from the page.

我会对php脚本执行ajax请求,该脚本会将注释添加到mysql数据库中。 ajax回调将检索ID并以可视方式将注释添加到页面(无需重新加载)。删除按钮将对php脚本执行另一个ajax请求,将ID传递给脚本。回调将从页面中删除评论。

#5


0  

If you want to be able to delete the comment, you must communicate with the server, so you must use ajax. Just send the comment to the server, wait for for the comments ID to be returned, then format the comment into some HTML, hide the new comment ID somewhere in the new HTML (probably in the link to delete the comment) and wala.

如果您希望能够删除注释,则必须与服务器通信,因此必须使用ajax。只需将注释发送到服务器,等待返回注释ID,然后将注释格式化为某些HTML,在新HTML中隐藏新注释ID(可能在删除注释的链接中)和wala。

#6


0  

UPDATE

After reading answers here is my basic idea, I don't know how to write javascrpt but you will get the design flow idea:

在阅读答案后,这是我的基本想法,我不知道如何编写javascrpt,但你会得到设计流程的想法:

POST comment input to script with AJAX {    
    if commentID is returned{
        Insert comment DIV into page, userID and userPicture are in a user session variable and commentID is returned from ajax post
    }ELSE IF error is returned{
        Insert an error message DIV into page
    }
}

Then the comment div or whatever that is inserted would include
- a users name from session variable
- userID from session variable for linking to there profile
- user picture from session variable
- comment ID for allowing the user to delete that comment

然后注释div或插入的任何内容将包括 - 来自会话变量的用户名 - 来自会话变量的userID,用于链接到那里的配置文件 - 来自会话变量的用户图片 - 用于允许用户删除该注释的注释ID

Then for deleting ANY comment on the page that a user published

然后删除用户发布的页面上的任何评论

POST comment ID and userID to DELETION script with AJAX {    
    if commentID is deleted{
        REMOVE comment DIV from page
    }ELSE IF error is returned{
        Insert an error message DIV into page
    }
}