Link to home
Start Free TrialLog in
Avatar of kansun
kansun

asked on

Php Ajax simple Counter

Hi,
I am implimenting a like and dislike Ajax php counter on my website.
There are 2 buttons on my page "Like" "Dislike"

When i click "Like" the database must update itself by incrimenting the value by "1" and must display the new value besides it.

This needs to work with Ajax... Should not refresh the page.
I am wondering what wrong in my code.

have copy pasted the code that i have for the pages...
Also i am writing the columns of my database table.

---index.php begins----

<html>
<head>
<script type="text/javascript">
function ajx(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<input type="button" name="buttn" id="buttn" value="submit" onClick="ajx(1)">
</form>

<br />
<div id="txtHint"><b>votes will be displayed here.</b></div>

</body>
</html>
---index.php ends-----

----getuser.php begins-----

<?php
$q=$_GET["q"];

$con = mysql_connect("localhost","user","pass");


if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("mydb",$con);

$sql="SELECT * FROM ajaxtest WHERE id = '1'";

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
  {
      $oldlike = $row['likes'];
  }
         $newlike =  1 + $oldlike ;
        
         $idf = 1;

$sqlupd = "UPDATE ajaxtext set likes='$newlike' where id='$idf'";
$result1 = mysql_query($sqlupd,$con);

echo $newlike ;

mysql_close($con);
?>
----getuser.php ends-----


ajaxtest is the table name with columns "id", "likes", "dislike" all the colms are itegers.

Thank you,

kansun
Avatar of kansun
kansun

ASKER

I will appreciate if you can provide me a working tested solution.
I have been breaking my head over this simple thing 2 days.

Thank you
Have you got this on a server somewhere so that we can see it in action and suggest some ways to visualize what might be going wrong?
Also, please post the CREATE TABLE statement for the ajaxtest table, thanks.
Avatar of kansun

ASKER

Hi,

this worked for me. I just misspelled the table name while updating.
The problem is that this code now works in firefox but not in ie8.

My god !!!!
any suggestions and solutions..

kansun
http://www.laprbass.com/RAY_temp_kansun.html
<html>
<head>
<title>kansun</title>

<script type="text/javascript">
function getVotes()
{
    var xmlhttp;

    if (window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
    }
    else
    {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
        }
    }

    xmlhttp.open("GET","RAY_temp_kansun.php?q=1",true);
    xmlhttp.send();
}
</script>

</head>
<body>

<h2>Vote to see the count <div id="myDiv" style="display:inline;">here</div></h2>
<button type="button" onclick="getVotes()">Vote Now</button>

</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kansun

ASKER

thanks for the solution..

that was quick
Thanks for the points - it's a great question.  If you want to do some reading you might find this article interesting:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_5256-Simple-Vote-Counting-in-PHP-and-MySQL.html