Home arrow PHP arrow Page 5 - Democracy, The PHP Way

Vote Now, Or Forever Hold Your Peace - PHP

One of the simplest and most popular add-ons to a Web site is anonline poll, allowing visitors to vote on hot-button issues. In thisarticle, find out how PHP can be used to build a powerful, good-lookingonline poll for your Web site, and also learn a little bit about its imageand cookie manipulation functions.

TABLE OF CONTENTS:
  1. Democracy, The PHP Way
  2. The Plan
  3. Design View
  4. Start Me Up
  5. Vote Now, Or Forever Hold Your Peace
  6. The Number Game
  7. Down Memory Lane
  8. Cookie-Cutter Code
  9. Adding More...
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 24
April 16, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Once the form is submitted, "vote.php" takes over to process the vote. This script first checks to ensure that the form has been correctly submitted, by verifying the presence of the $submit and $response variables

<? // vote.php - record votes // check to ensure that the form has been submitted if (!$submit || !$response) { ?> <html> <head> <basefont face="Arial"> </head> <body bgcolor="white"> <i>Error! Please <a href=start.php>try again</a></i> <? } else { // vote processing code } ?>

and, assuming that all is well, updates the database to reflect the new vote and displays an appropriate message.

<? // vote.php - record vote // check to ensure that the form has been submitted if (!$submit || !$response) { // error message } else { // all is well - process the vote ?> <html> <head> <basefont face="Arial"> </head> <body bgcolor="white"> <? // includes include("config.php"); include("common.php"); // connect and update table $connection = mysql_connect($hostname, $user, $pass) or die ("Unable to connect!"); $fieldname = "votes" . $response; $query = "UPDATE $table SET $fieldname = $fieldname+1 WHERE id = $id"; $result = mysql_db_query($database, $query, $connection) or die ("Could not execute query: $query. " . mysql_error()); // query successful,display status message... if ($result) { echo "Thank you for voting. Here are the results so far:<p>"; // code to display results - keep reading! } // or error in query - display error message else { echo "<i>Error! Please <a href=start.php>try again</a></i>"; } // close connection mysql_close($connection); } ?> </body> </html>

As you can see, the value of the $response variable is used to determine which option field is updated with the user's vote.

This article copyright Melonfire 2001. All rights reserved.

 
 
>>> More PHP Articles          >>> More By Vikram Vaswani, (c) Melonfire
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap

Dev Shed Tutorial Topics: