Implement an Ajax-based survey program

Source: Internet
Author: User

<HTML>
<Head>
<Title> vote </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 ();
?>

</Head>
<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);" onsubmit = "onsubmit ();">
<? 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>
</Html>

Server

Function poll ($ formdata ){
Global $ dB;
$ TMP = "";
$ Objresponse = new xajaxresponse ();

$ Poll_id = $ formdata ['Poll _ id'];
$ Pollitem_id = $ formdata ['poliitem'];

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 database table is as follows:

Create Table tbl_poll (
Poll_id int (11) unsigned not null default '0 ',
Title varchar (100) not null default '',
Created_date bigint (20) unsigned not null default '0 ',
User_id int (11) unsigned not null default '0 ',
Primary Key (poll_id)
) Type = MyISAM;

Create Table tbl_pollitem (
Pollitem_id int (11) unsigned not null default '0 ',
Poll_id int (11) unsigned not null default '0 ',
Content varchar (100) not null default '',
Count int (11) unsigned not null default '0 ',
Primary Key (pollitem_id)
) Type = MyISAM;

In this example, the survey option is read only when the page is loaded, and the latest voting information is displayed after voting. No window is required

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.