<? 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