【问题标题】:call function when onclick <a> , then increase count by one, update the change to databaseonclick <a> 时调用函数,然后将计数加一,更新对数据库的更改
【发布时间】:2014-04-19 10:14:55
【问题描述】:

div 的内容是由数据库按“cts”(计数)排序生成的。当我单击 div 时,我希望“cts”数字增加一,然后将更改更新到数据库。因此,内容会相应改变。 AJAX:

function addcount()
{
    console.log('step1');
    var id=this.rowid; 
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
        console.log('step2');
    } else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        console.log('step3');
            document.getElementById("cts").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","addcount.php?q="+id,true);
    xmlhttp.send();
}

php代码:

<?php 
require_once "db.php";
session_start();
$id=intval($_GET['id'])
$sql="UPDATE words SET cts=cts+1 WHERE id='$id'";
    if (!mysqli_query($con,$sql)){
        die('Error: ' . mysqli_error($con));
    }
?>

-------这根本不起作用-------提前谢谢你。

【问题讨论】:

  • 为什么不 jQueryjQuery.Ajax ?看看这个:api.jquery.com/jQuery.post
  • 您的问题被标记为jquery,但我没有看到您在代码中的任何地方使用 jQuery。你真的在用吗?
  • 如今的编程业务很简单。 1.堆积一些随机的代码行。 2.发现它不起作用。 3. 将其发布在 Stack Overflow 上,让他人为您整理。 4.??? 5. 利润!!!
  • 旁注:您的代码成为The Horror of Implicit Globals 的牺牲品,因为您没有在addcount 函数中声明xmlhttp。总是声明你的变量。
  • 您是否尝试过在不使用 Javascript/Ajax 的情况下构建它?这可能更容易调试(只是带有页面刷新的普通链接)。当它起作用时,添加 Ajax 应该是微不足道的。此外,如果出现问题,那么您就知道去哪里找了,因为服务器端部分已经好了。

标签: javascript php ajax database


【解决方案1】:

?q="+id$_GET['id']

您需要在整个查询字符串参数中使用相同的名称。

?q="+id 更改为?id="+id 或将$_GET['id'] 更改为$_GET['q']

【讨论】:

    【解决方案2】:

    您没有准确解释您的问题是什么,但您遇到的问题是您在调用open() 之前设置了onreadystatechangeopen 重置 XMLHttpRequest 对象,因此即使服务器端应用程序运行良好,您的回调也不会执行。

    将顺序改为:

    xmlhttp.open("GET","addcount.php?q="id,true);
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        console.log('step3');
            document.getElementById("cts").innerHTML=xmlhttp.responseText;
        }
    }
    

    【讨论】:

      【解决方案3】:

      我遇到的问题就像昆汀所说的:保持 id 和 p 相同。所以我改了?q="+id to ?id="+id

      我遇到的另一个问题是 $(this).rowid 并不是我想要的确切值。因此,我使用参数来解决这个问题。

      它现在工作得很好。谢谢大家的帮助。 html

      onclick="addcount('.htmlentities($row['id']).');return false;"
      

      ajax:

      function addcount(id)
      {
      if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
        }
      else
        {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      
        xmlhttp.open("GET","addcount.php?id="+id,true);
      xmlhttp.onreadystatechange=function()
        {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
          {
            console.log(xmlhttp);
          document.getElementById("cts").innerHTML=xmlhttp.responseText;
          }
          else{
            console.log(xmlhttp.readystatus);
            console.log(xmlhttp.status);
          }
        }
      xmlhttp.send();
      }
      

      php:

      <?php 
      header("Content-type: text/html; charset=utf-8");
      require_once "db.php";
      session_start();
      $id=intval($_GET['id']);
      $sql="UPDATE words SET cts=cts+1 WHERE id='$id'";
          if (!mysqli_query($con,$sql)){
              die('Error: ' . mysqli_error($con));
          }
      ?>
      

      谢谢:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多