Ajax| Program
<title> Polling </title>
<meta Http-equiv=content-language content=zh-cn>
<meta http-equiv=content-type content= "text/html; charset=gb2312 ">
<style type= "Text/css" >
<!--
. Poll {Font-size:10pt;display:block}
. Pollresult {Font-size:12pt;display:none}
-->
</style>
<?php
Include_once ("server1.server.php"); #servidor para xajax
$xajax->printjavascript ();
?>
<body>
<script language=javascript>
function back () {
document.getElementById (' Poll '). Style.display = ' block ';
document.getElementById (' Pollresult '). style.display = ' None ';
document.getElementById (' Pollresult '). InnerHTML = ';
}
</script>
<div Id=pollresult class=pollresult>
</div>
<?php
Global $db;
$poll = $db->getrow ("SELECT * from Tbl_poll ORDER BY poll_id desc LIMIT 1");
$poll _id = $poll ["poll_id"];
$pollitems = $db->getall ("select * from Tbl_pollitem where poll_id= $poll _id");
?>
<div Id=poll class=poll>
<form id= "Pollform" action= "javascript:void (null);" >
<?php echo $poll ["title"];?><br>
<?php for ($i = 0, $count = count ($pollitems); $i < $count; $i + +) {?>
<input type= "Radio" style= Background-color: #CCCCCC; "Name=" Pollitem "value=" <?php echo $pollitems [$i] [' pollitem_id ']?> "><?php echo $pollitems [$i] [' content ']?><br>
<?php}?>
<input type= "hidden" name= "poll_id" value= "<?php echo $poll _id;?>" >
<input type= "Submit" value= "enter" >
</form>
<script language=javascript>
function OnSubmit () {
Xajax_poll (Xajax.getformvalues ("Pollform"));
document.getElementById (' Poll '). Style.display = ' None ';
document.getElementById (' Pollresult '). style.display = ' block ';
}
</script>
</div>
</body>
Server-side
Function Poll ($formData) {
Global $db;
$tmp = "";
$objResponse = new Xajaxresponse ();
$poll _id = $formData [' poll_id '];
$pollitem _id = $formData [' Pollitem '];
if ($pollitem _id > 0 && $poll _id > 0) {
$db->query ("Update"). Tbl_pollitem. "Set count=count+1 where pollitem_id = $pollitem _id");
}
$poll = $db->getrow ("select * from tbl_poll where poll_id = $poll _id");
$pollitems = $db->getall ("select * from Tbl_pollitem where poll_id= $poll _id");
$tmp. = "<div align=center>". $poll ["title"]. " </div><br> ";
for ($i = 0, $count = count ($pollitems); $i < $count; $i + +) {
$tmp. = "<div align=left>". $pollitems [$i] [' content ']. ":" $pollitems [$i] [' count ']. " </div> ";
}
$tmp. = "<div align=center>". <input type=\ "button\" value=\ "return" onclick=\ "back (); \" > ". </div> ";
$objResponse->addassign ("Pollresult", "InnerHTML", $tmp);
return $objResponse->getxml ();
}
The table of the database is as follows
CREATE TABLE Tbl_poll (
poll_id Int (one) unsigned not NULL default ' 0 ',
Title varchar not NULL default ',
Created_date bigint () unsigned not NULL default ' 0 ',
user_id Int (one) unsigned not NULL default ' 0 ',
PRIMARY KEY (poll_id)
) Type=myisam;
CREATE TABLE Tbl_pollitem (
pollitem_id Int (one) unsigned not NULL default ' 0 ',
poll_id Int (one) unsigned not NULL default ' 0 ',
Content varchar (MB) Not NULL default ',
Count Int (one) unsigned not NULL default ' 0 ',
PRIMARY KEY (pollitem_id)
) Type=myisam;
In this example, the survey options are read only when the page is loaded, and the latest voting information is displayed in situ after the vote. No pop-up window required