PHP online survey system

Source: Internet
Author: User
Tags php online string back

PHP online survey system
This is a simple vote Program It is a good entry-level program for PHP and friends. I would like to introduce you here, hoping to help you.
The system consists of the following four files: notepad we can create them with notepad and upload them to the program directory. The file data.txt is stored in the project to be investigated. Each project should be in the same line, and survey.txt can be an empty file without any content.
Survey.htm's Code It can be as follows:
<HTML>
<Head>
<Title> survey </title>
</Head>
<Body>
<Form method = "Post" Action = "survey. php">
<P> <input type = "radio" value = "0" name = "Vote"> Project 1 </P>
<P> <input type = "radio" name = "Vote" value = "1"> Project 2 </P>
<P> <input type = "radio" name = "Vote" value = "2"> Project 3 </P>
<P> <input type = "radio" name = "Vote" value = "3"> Project 4 </P>
<P> <input type = "radio" name = "Vote" value = "4"> Project 5 </P>
<P> <input type = "hidden" name = "go" value = "1">
<P> <input type = "Submit" value = "Submit" name = "B1"> </P>
<A href = "survey. php? Result = 1 "> View results </a>
</Form>
</Body>
</Html>
The survey items in the survey file data.txt must be consistent with the number and order of the above survey items. Otherwise, errors may occur or the survey results may be inaccurate. At the same time, in order to display the survey results in a bar chart, we should prepare several bar images of different colors. For example, 0.gif,1.gif,2.gif,3.gif,4.gif.
The following is the survey. PHP code that implements the investigation function:
<?
$ DATA = "data.txt ";
$ Votes = "survey.txt ";
$ Dataf = file ($ data);/* read the project in the survey project file */
$ File_votes = fopen ($ votes, "R ");
$ Line_votes = fgets ($ file_votes, 255);/* read the recorded Survey Results */
Fclose ($ file_votes );
$ Single_vote = explode ("|", $ line_votes);/* cut the data according to the specified string, and then pass the string back to the array variable */
If ($ result! = 1)/* if you have already received the investigation */
{
$ File_votes = file ($ votes, "R ");
If ($ remote_addr = $ file_votes [1])/* check whether the same person is the same */
{
Echo "<center> <font color = Red> you have voted. Thank you for your participation! </Font> </center> ";
Exit;
}

/* If the IP address is not repeated, execute the following Program */
$ Ficdest = fopen ($ votes, "W ");
For ($ I = 0; $ I <= count ($ dataf)-1; $ I ++)
{
If ($ I = $ vote)
{/* Determine the selected project */
$ Single_vote [$ I] + = 1;
}
Fputs ($ ficdest, "$ single_vote [$ I] |");/* write data back to a file */
}
Fputs ($ ficdest, "\ N $ remote_addr");/* // ip address of the writer */
Fclose ($ ficdest );
$ Result = 1;/* vote successful */
}

/* Write the voting result and display the voting result */
If ($ result = 1)
{
Echo "<Table cellpadding = 10> ";
For ($ I = 0; $ I <= count ($ dataf)-1; $ I ++)
{
/* Obtain the total number of votes */
$ Tot_votes + = $ single_vote [$ I];
}
For ($ I = 0; $ I <= count ($ dataf)-1; $ I ++)
{
$ Imag = strval ($ I). ". GIF";/* determine which bar image is used to display the statistical result */
$ Stat [$ I] = $ single_vote [$ I]/$ tot_votes * 100;/* calculate the percentage */
$ SCLA = $ stat [$ I] * 5;/* bar chart and magnification, which are displayed by a pixel width of 5 times of the security percentage */
Echo "<tr> <TD> <li> <font face = verdana size = 2> ";
Echo "$ dataf [$ I] </font> </TD> <TD align = left> <font face = verdana size = 2> ";
Echo "Printf ("%. 1f", "$ stat [$ I]");
Echo "% </font> </TD> <TD align = center> <font face = verdana size = 2> ";
/* Output the number of votes in this topic */
Echo "$ single_vote [$ I] </font> ";
Echo "</TD> </tr> ";
}
Echo "</table> <p> ";
Echo "<font face = verdana size = 2> total number of votes: $ tot_votes </font> ";
}
?>
Note:
Here, to prevent one-man multi-vote from being implemented by recording the IP address of the most recent voter, the most recent voting IP address is stored in the environment variable remote_addr when the Web Client sends a request to the server.
I am also a beginner. Article There may be many mistakes and mistakes. You are welcome to give your valuable comments and suggestions. Thank you!
Personal Homepage: http://bamboo.oso.com.cn
Email: wangyy@363.net

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.