THOUGHTS

**The youth needs to develop an attitude : I can do it! We can do it !! India will do it!!! ""மரங்களை நடுவோம் பசுமை இல்ல விளைவை தடுப்போம்"" !!! PLEASE DO IT**

Wednesday, August 10, 2011

PHP Tutorial Part 10


The getVote() Function
This function executes when "yes" or "no" is selected in the HTML form.
1.    Defines the url (filename) to send to the server
2.    Adds a parameter (vote) to the url with the content of the input field
3.    Adds a random number to prevent the server from using a cached file
4.    Calls on the GetXmlHttpObject function to create an XMLHTTP object, and tells the object to execute a function called stateChanged when a change is triggered
5.    Opens the XMLHTTP object with the given url.
6.    Sends an HTTP request to the server

The PHP Page

The server page called by the JavaScript code is a simple PHP file called "poll_vote.php".
<?php
$vote = $_REQUEST['vote'];
//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);
//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];
if ($vote == 0)
 {
 $yes = $yes + 1;
 }
if ($vote == 1)
 {
 $no = $no + 1;
 }
//insert votes to txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>
<h2>Result:</h2>
<table>
<tr>
<td>Yes:</td>
<td>
<img src="poll.gif"
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>
<img src="poll.gif" 
width='<?php echo(100*round($no/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($no/($no+$yes),2)); ?>%
</td>
</tr>
</table>
The selected value is sent from the JavaScript and the following happens:
1.    Get the content of the "poll_result.txt" file
2.    Put the content of the file in variables and add one to the selected variable
3.    Write the result to the "poll_result.txt" file
4.    Output a graphical representation of the poll result



                                                                     THANK YOU 
                                                                      THAMARAI

No comments:

Post a Comment