user3418973
user3418973

Reputation: 1

call function when onclick <a> , then increase count by one, update the change to database

The div's content are generated by the database ordered by "cts"(a count number). when I click the div, I want the "cts" number to increase by one, and then the change is updated to the database. as a result, the content will change accordingly. 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 CODE:

<?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));
    }
?>

-------This does not work at all-------Thank you in advance.

Upvotes: 0

Views: 552

Answers (3)

user3418973
user3418973

Reputation: 1

the problem I'm having is like Quentin said:keep id and p identical. so I changed ?q="+id to ?id="+id

the other problem I'm having is $(this).rowid does not refers to the exact value I want. Therefore, I used parameter to solve this problem.

it's now working rather well. Thanks you all for you help. 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));
    }
?>

Thank you:)

Upvotes: 0

Oscar Paz
Oscar Paz

Reputation: 18302

You don't explain exactly what your problem is, but one you have is that you're setting onreadystatechange before calling open(). open resets the XMLHttpRequest object, so your callback won't ever execute, even if server-side the applications works fine.

Change the order to:

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;
    }
}

Upvotes: 0

Quentin
Quentin

Reputation: 944005

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

You need to use the same name for your query string parameter throughout.

Change ?q="+id to ?id="+id or $_GET['id'] to $_GET['q'].

Upvotes: 1

Related Questions