This program is for develop poll application using AJAX
,JSON and PHP . Copy and paste following file to develop poll application . Use
Internet Explorer to view the output .In other browser it may not work.
Step 1) Copy and paste below code in your PHP file.
Output:-
Databse table structure:-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script language="javascript">
function ajax(val)
{
var result=val;
var xmlhttp=new XMLHttpRequest();
if(xmlhttp=="")
{
alert("Object not created");
}
else
{
//ajax
object creaTED.......
xmlhttp.onreadystatechange=function()
{
if
(xmlhttp.readyState==4 && xmlhttp.status==200)
{
//var result = xmlhttp.responseText;
//document.getElementById("myp").innerHTML=result;
var jsonText = xmlhttp.responseText;
var jsonObject= eval('('+jsonText+')');
var yes=jsonObject.yes;
var no=jsonObject.no;
document.getElementById("mypicyes").width=yes;
document.getElementById("mypicno").width=no;
document.getElementById("yes").innerHTML=yes-1;
document.getElementById("no").innerHTML=no;
}
}
xmlhttp.open("GET","pollvote.php?vote=" +
result,true);
xmlhttp.send();
}
}
</script>
<title>AJAX testing page.</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
</head>
<body>
<center><h1>Poll your vote
here.</h1></center>
<table>
<tr>
<td>Yes:</td>
<td><img
id="mypicyes" src="bg.jpg"
height="20px"></td>
<td><p
id="yes"></p></td>
</tr>
<tr>
<td>No:</td>
<td><img
id="mypicno" src="bg.jpg"
height="20"></td>
<td><p
id="no"></td>
</tr>
</table>
<form name="myform">
<p id="myp"></p>
Select your vote :-<br>
PHP or ASP.Net
<input type="radio" name="radiovote"
value="1" onClick="ajax(this.value)">ASP.Net
<input type="radio" name="radiovote"
value="0" onClick="ajax(this.value)">PHP
</form>
</body>
</html>
Step 2) Copy the image into same location of your
server directory.
Step 3) Paste below
code and edit database connection and table information part. In my table
information having following column.
·
Id
·
Yes
·
No
<?php
$val=$vote;
$conn=mysql_connect("localhost","root","");
if($conn==NULL)
echo "Database
connection failure";
$db=mysql_select_db("ajax",$conn);
if($db==NULL)
echo "Database
selection failure";
if($val==1)
{
$qur="select
* from vote";
$ans=mysql_query($qur);
while($row=mysql_fetch_array($ans))
{
$val=$row['yes'];
}
$nval=$val + 1;
$qur="UPDATE `ajax`.`vote` SET `yes` =
'$nval' WHERE `vote`.`id` =1";
$ans=mysql_query($qur);
}
else
{
$qur="select * from vote";
$ans=mysql_query($qur);
while($row=mysql_fetch_array($ans))
{
$val=$row['no'];
}
$nval=$val + 1;
$qur="UPDATE `ajax`.`vote` SET `no` =
'$nval' WHERE `vote`.`id` =1";
$ans=mysql_query($qur);
}
$qur="select * from vote where id=1";
$ans=mysql_query($qur);
while($row=mysql_fetch_array($ans))
{
$yes= $row['yes'];
$no= $row['no'];
}
$qur="select
* from vote";
$ans=mysql_query($qur);
while($row=mysql_fetch_array($ans))
{
$valyes=$row['yes'];
$valno=$row['no'];
}
$total = $valyes + $valno;
$yespercent=ceil(($valyes / $total)* 100);
$nopercent=ceil(($valno / $total)* 100);
//Json data creation goes here.
$ans=Array("yes"
=> $yespercent,"no" => $nopercent);
$json_name=json_encode($ans);
echo
$json_name; //Data is returning main
page here
?>
No comments:
Post a Comment