FRIHOST • FORUMS • SEARCH • FAQ • TOS • BLOGS • COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


A poll that allows users to specify another option....





KHO
I need a poll that would allow users to vote on options, but also to allow users to post a new option to the poll in-case theirs isn't listed, and other users would see it then on out. Ex:

Default Poll:
What is your favorite color?
1)Red
2)Blue
3)Green
Other: [textbox for input]
[OK]

*user adds purple*

New Poll
What is your favorite color?
1)Red
2)Blue
3)Green
4)Purple
Other: [textbox for input]
[OK]

If anyone could help me with this, it would be greatly appreciated. Thanks. =]
powers1983
I assume you have the poll linked to a database since you're posting in this forum.
If you have created a table as follows:

Code:

CREATE TABLE voting_table(colour CHAR(20) PRIMARY KEY DEFAULT 'Pinky Green', num_votes INT DEFAULT 0);


then placing this code in whatever file deals with your poll form:

Code:

<?php

if (isset($_POST['sending_form'])){ // form sent
   if (isset($_POST['fav_colour'])){ // favourite colour sent
      $selected_fav_colour=$_POST['fav_colour'];
      $result = mysql_query("SELECT num_votes FROM voting_table WHERE colour = '$selected_fav_colour';");
      if ($result && mysql_num_rows($result)) {
         $row = mysql_fetch_assoc($result);
         $number_of_votes=$row['num_votes'];
         $number_of_votes++;
      } // end if votes are available for that colour
      $result = mysql_query("UPDATE voting_table SET num_votes = $number_of_votes WHERE colour = $selected_fav_colour;");
   } // end if favourite colour sent
   
   if (isset($_POST['new_colour_option'])){ // favourite colour sent
      $new_colour_option=$_POST['new_colour_option'];
      $result = mysql_query("INSERT INTO voting_table (colour, num_votes) VALUES ('$new_colour_option', 1);");
   } // end if favourite colour sent
   
} else { // No form sent
   print "No form sent so please fill in the details below.<br>";
} // end else

?>


and using this form:

Code:
         
<form method="post" action="vote_for_colours.php" >
   <input type="hidden" name="sending_form" value="yes">
   <table width="90%" align="center">
      
      
      <tr align="left">
         <td>Favourite Colour:</td>
         <td>
            <select name="fav_colour">
               <?php
               $result = mysql_query("SELECT colour FROM voting_table;");
                if ($result && mysql_num_rows($result)) {
                  while ($row = mysql_fetch_assoc($result)) {
                     $colour_option=$row['colour'];
                     print '<option value="'.$colour_option.'">'.$colour_option.'</option>'."\n";
                  }
               }
               ?>
            </select>
         </td>
      </tr>
      <tr align="left">
         <td>New Colour?</td>
         <td><input type="text" name="new_colour_option"></td>
      </tr>
      
      
      <tr>
         <td colspan="2" align="center">
            <input type="submit" value="Submit">
         </td>
      </tr>
   </table>
</form>


then it should do what you need.

Notice that the list of options in the form is dynamically created so you need to have connected to the database on whatever page it is on. You can have both parts on the same page and the hidden attribute will ensure that the page will display correctly.

Before placing online you'll need to add more input checking to make it safe but I think it'll work as is and it should be fairly self-explanatory.

Hope that helps.

David.
KHO
Cool, thanks. This will work great, I can fiddle with it enough to get it to do what I want.
manum
do checkout hotscripts.com also
Related topics
Help with PhpBB
Windows XP Tricks & Tips!!!!
making a site but I need help I am new to php and Runy scrip
Windows Live Messenger 9.0 (2009)
extern harddsik
why do you post message on this forum??
234 productions
Help! Installation Problem.
Favorite Free E-mail
Which Linux software is easier to install & configure?
Discussion ABOUT the “What is morality?...” thread
150 frih to make this SIMPLE script
Software Forums - based on phpBB
State your Political Philosophy! (1000 FRIH$ to the best!)
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.