Php 使用ajax异步升级mysql字段时;例如;点击

Php 使用ajax异步升级mysql字段时;例如;点击,php,javascript,mysql,ajax,prototypejs,Php,Javascript,Mysql,Ajax,Prototypejs,问题相当简单。每当用户单击“like”时,我都希望使用javascript通过ajax更新mysql数据库。我想尽了一切办法,但没有发现任何有用的东西。我知道我还需要一个服务器端脚本,但ajax是主要问题 这是我设置的计数器代码 document.observe("dom:loaded", function() { $("likeForumLink").onclick=processForumLike; }); function processForumLike(event) {

问题相当简单。每当用户单击“like”时,我都希望使用javascript通过ajax更新mysql数据库。我想尽了一切办法,但没有发现任何有用的东西。我知道我还需要一个服务器端脚本,但ajax是主要问题

这是我设置的计数器代码

 document.observe("dom:loaded", function()
{
  $("likeForumLink").onclick=processForumLike;

});


function processForumLike(event)
{

  var numberOfLikes=parseInt($("hiddenLikes").value); 

   numberOfLikes+=1;

  $("hiddenLikes").value=numberOfLikes+""; 

   this.innerHTML="You liked this"; 

  if(numberOfLikes==1)
  {
   $("numberOfLikes").innerHTML="("+numberOfLikes+" like)";

  }          
 else
 {
 $("numberOfLikes").innerHTML="("+numberOfLikes+" likes)";
 }

var likes=$("hiddenLikes").value;

new Ajax.Request("seeForum.php?id=$("subjectId").value", {

  method:'get', 
  parameters:{hiddenLikes:likes},      
  onSuccess: ????  //Don't know
  onComplete:???   //Don't know
  on Failure:????  //Don't know

 });
}
然后我可以使用我的服务器端脚本,如下所示。 另外,它位于同一页
“seeForum.php?subjectId=$(“subjectId”).value”


仔细想想,如果用户单击like,您真正想要做的就是增加页面本身中的like计数器。您可以在
onComplete:function(){increment the like counter}
中执行此操作。如果确实发生了某种错误,我认为您不想将其显示给用户,这取决于您希望如何处理该部分,有很多方法,您可以记录错误

首先,您不应该使用GET请求来更新数据,这应该是一篇文章。这是:

"seeForum.php?id=$("subjectId").value"
不会做你可能认为它会做的事。它所做的是导致语法错误,因为它在JavaScript解释器中看起来像
“x”x“x”
,而这不是JavaScript。我不确定您使用的是哪一个JavaScript库(可能是?),但我认为您可能希望将所有参数放入
parameters
,以避免担心URL编码之类的问题。此外,没有理由告诉服务器你已经有多少“喜欢”,也有一个很好的理由不告诉服务器:当你在看某个页面时,可能有人“喜欢”了该页面,而你当前拥有的喜欢的数量将是错误的;所以根本不需要
hiddenLikes

在JavaScript方面,这样做会更好:

new Ajax.Request("likeIt.php", {
  method:     'post', 
  parameters: { id: $('subjectId').value },
  onSuccess:  function(transport) {
      // Update the "likes" label with something like this,
      // I'm not that familiar with Prototype though so I'm
      // guessing.
      $('likes_label').update(transport.responseText);
  },
  onComplete: ????  // The function to call when it has finished.
  onFailure:  ????  // The function to call when it doesn't work.
});
然后,在PHP中,只需向数据库发送一个简单的更新,以增加
likeIt.PHP
中的like数,如下所示:

<?php
# Perform whatever authentication you need...
if(isset($_POST["id"])) {
    # Set up your database connection and then...
    $result = mysql_query("UPDATE `subject_table` SET `likes` = `likes` + 1 WHERE `subjectId` = '" . mysql_real_escape_string($_POST["id"]) . "'");
    # Check $result and send some data back to the client (if desired), if
    # you send something back then the onComplete, onFailure, and onSuccess
    # JavaScript functions would be responsible for doing something with it.
}
# Send back the updated number of "likes" as text/plain.
?> 

感谢您深入而详细的回答。你的帮助对我的工作至关重要。对不起,我只是个新手,对ajax不太了解,所以我真正想做的是将我喜欢的东西异步提交到数据库,而不必重新加载页面。你能建议我的
onSuccess
部分应该有什么吗?顺便说一句,我正在使用prototype库。@jbloom:我对prototype不太熟悉,但我已经做了一些更新,希望能为你指明正确的方向。谢谢。你在原型上是对的。只有
transporter
需要替换为
ajax
。我甚至需要
onSuccess
吗?因为我认为如果我能在参数中传递我喜欢的内容,我就可以更新我的表了。页面站点是
seeForum.php?subjectId=31
,如果我发送了一个like,那么它看起来会像
seeForum.php?subjectId=31&likes=2
?@jbloom:我建议您应该将AJAX处理程序移动到一个单独的php文件中,该文件处理AJAX,并且只处理AJAX,您只需将
id
交给它即可。如果要使用新的喜欢数更新页面,则只需要
onSuccess
回调。
<?php
# Perform whatever authentication you need...
if(isset($_POST["id"])) {
    # Set up your database connection and then...
    $result = mysql_query("UPDATE `subject_table` SET `likes` = `likes` + 1 WHERE `subjectId` = '" . mysql_real_escape_string($_POST["id"]) . "'");
    # Check $result and send some data back to the client (if desired), if
    # you send something back then the onComplete, onFailure, and onSuccess
    # JavaScript functions would be responsible for doing something with it.
}
# Send back the updated number of "likes" as text/plain.
?>