|
|
|
|
![]() ![]() |
Feb 17 2008, 11:12 AM
Post
#1
|
|
|
Newbie [Level 1] ![]() Group: Members Posts: 24 Joined: 5-February 08 From: Business, Nonya Member No.: 57,376 |
I plan on making a script for basic voting between different options, and I'd like to know what PHP coding I would require. Basically, each choice will be as simple as this:
CODE <form method="post" action="process.php"> Best falsetto?<br><br> <input type="radio" name="1"> Person A<br> <input type="radio" name="2"> Person B<br> <input type="submit" value="Submit"> </form> What PHP would be used to basically add 1 value to a specified .txt file based on which option is chosen? (Like, if person A was selected, it would add 1 to persona.txt, and if person B was chosen, it would add 1 to personb.txt) Thanks in advance to whoever helps. I'm not good at this kind of intermediate/advanced PHP. |
|
|
|
Feb 17 2008, 11:23 AM
Post
#2
|
|
|
A computer once beat me at chess, but it was no match for me at kick boxing. ![]() Group: [MODERATOR] Posts: 3,874 Joined: 24-July 05 From: In Trouble Again... still? Member No.: 9,787 ![]() |
The script you write will need to check the value of $_POST[name] and increase the counter for that name. Easy enough I suppose, but consider that I could sit at my computer all day and vote a number of times. This will skew the results, so I would do some checking of IP's and allow only one vote per IP. Another method to control the 'duplication' problem is to set a Cookie or a Session value to control duplicate votes. Or a time-based solution, allowing votes only after a period of time.
There are likely several voting or Poll scripts available around the 'net all ready to go. Check at Hotscripts.com or Google on it. No sense re-inventing the wheel. Or search around the Trap17. looks like someone else had the same question once: http://www.trap17.com/forums/index.php?s=&...st&p=239559 Another possible solution is to start a Forum. Most of them include some sort of Voting or Poll system. Check out AEF Forums at http://anelectron.com |
|
|
|
Feb 17 2008, 04:54 PM
Post
#3
|
|
|
Ephesians 6:10-17 ![]() Group: [MODERATOR] Posts: 1,861 Joined: 22-June 05 From: The World of Gentoo Member No.: 8,528 ![]() |
By the way, input elements (except a few) require the value attribute to be set in order to be able to send a value to the server-side script, otherwise the variable containing the POST information will be empty.
|
|
|
|
Feb 17 2008, 09:04 PM
Post
#4
|
|
|
A clever man learns from his own mistakes, a WISE man learns from those of OTHERS ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 884 Joined: 12-April 06 From: Essex, UK Member No.: 21,719 |
I think you might be better to use a database instead of a file to store the data, there is a little more PHP to be learned (or used, depending on your level) but its well worth it.
the difference in difficulty isnt really that much and the advantages are pretty big...As Jlhaslip said you might want to add a method for stopping a user voting more than once. an IP check is a good idea but an IP changes a lot so cookies might be an idea, or if the user has logged in then you can of course use their username, thats a reliable method. With a DB you could do several things much easier than a file methinks. You could add details to it easily like their IP/name then which option they voted for. Then use code to count how many votes were for each option, this is quite easy using something like: EDIT: I just realized that i completely missed anything about inserting values to a database but follow the instructions at the bottom about a tutorial site and it will show you how to input data into a DB. CODE $result_option_1 = mysql_query("SELECT * FROM table WHERE option='1'", $link); $votes_1 = mysql_num_rows($result_option_1); echo "Total votes for option one is: $votes_1"; you would repeat code like this for each option. Its rather basic and the code above is messy and wouldnt work (its not complete with a connection to an actual database, this is just the query part of it, and i left out a few backslashes to make it look better 8-)) but the general idea is there, query the database to find all votes for option one, then count the number of rows (which is the number of votes for option one) and then tell the user that number. If you need help with the code search google for "php mysql tutorial tizag.com" and you should find a nice, easy to follow tutorial string about php and mysql and yo will learn what i just wrote above and how to fully complete such a code. I wrote this assuming it will help you, if you already know this then dont be offended, i just have no idea what sort of level you're at with PHP so im writing for a beginner. This post has been edited by shadowx: Feb 17 2008, 09:07 PM |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 24th July 2008 - 06:34 AM |