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("t xtHint").i nnerHTML=" ";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.X MLHTTP");
}
xmlhttp.onreadystatechange =function( )
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("t xtHint").i nnerHTML=x mlhttp.res ponseText;
}
}
xmlhttp.open("GET","getuse r.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","pa ss");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb",$co n);
$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
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("t
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.X
}
xmlhttp.onreadystatechange
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("t
}
}
xmlhttp.open("GET","getuse
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",
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb",$co
$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
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.
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
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks for the solution..
that was quick
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
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_5256-Simple-Vote-Counting-in-PHP-and-MySQL.html
ASKER
I have been breaking my head over this simple thing 2 days.
Thank you