Php and Ajax Voting Systems

Source: Internet
Author: User

 

<? Php

$ Vote = $ _ GET ['Vote'];

// Echo $ vote;

$ Filename = "poll.txt ";

$ Content = file ($ filename); // read the entire file into an array.

$ Arr = explode ('|', $ content [0]);

$ Yes = $ arr [0];

$ No = $ arr [1];

If ($ vote = 0 ){

$ Yes = $ yes + 1;

}

If ($ vote = 1 ){

$ No = $ no + 1;

}

$ Intpoll = $ yes. "|". $ no;

$ Fp = fopen ($ filename, 'w ');

Fputs ($ fp, $ intpoll );

Fclose ($ fp );

?>

<H2> POLL:

<Table>

<Tr>

<Td> Yes: </td>

<Td>

Width = '<? Php echo (100 * round ($ yes/($ no + $ yes), 2);?> '

Height = '20'>

<? Php echo (100 * round ($ yes/($ no + $ yes), 2);?> %

</Td>

</Tr>

<Tr>

<Td> No: </td>

<Td>

Width = '<? Php echo (100 * round ($ no/($ no + $ yes), 2);?> '

Height = '20'>

<? Php echo (100 * round ($ no/($ no + $ yes), 2);?> %

</Td>

</Tr>

</Table>

// Poll. js

Var xmlHttp

Function GetXmlHTTPObject (){

Var Object = null;

If (window. XMLHttpRequest)

{

Object = new XMLHttpRequest ()

}

Else if (window. ActiveXObject)

{

Object = new ActiveXObject ("Microsoft. XMLHTTP ")

}

Return Object;

}

Function statechang (){

If (xmlHttp. readyState = 4 | xmlHttp. readyState = "complete "){

Document. getElementById ("poll ").

InnerHTML = xmlHttp. responseText;

}

}

Function getVote (int ){

XmlHttp = GetXmlHTTPObject ()

If (xmlHttp = null ){

Alert ('error! ');

Return

}

Var url = "poll. php"

Url = url + "? Vote = "+ int

Url = url + "& sid =" + Math. random ()

XmlHttp. onreadystatechange = statechang

XmlHttp. open ("GET", url, true)

XmlHttp. send (null)

}

// Poll.html

<Html>

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>

<Title> POLL </title>

<Script src = "poll. js"> </script>

</Head>

<Body>

 

<Div id = "poll">

<H2> php and ajax poll

 

<Form>

Yes:

<Input type = "radio" name = "vote"

Value = "0" onclick = "getVote (this. value)">

<Br/>

No:

<Input type = "radio" name = "vote"

Value = "1" onclick = "getVote (this. value)">

</Form>

</Div>

Www.2cto.com

</Body>

</Html>

// There is also a poll.txt File

 

From chaojie2009

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.