Jump to content

Recommended Posts

hey i am pretty new to PHP. I am working on creating a secure login voting site for a school system that will allow teachers to login with a user name i will provide and cast votes for different positions. I have gotten the secure login portion of the site done but i cant seem to figure out how to update the sql with the data selected by the user and how to ensure each person gets to vote only once for the positions listed. I am new to PHP like i said but know Javascript, HTML, and SQL. any help or pointers would be greatly appreciated thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/204897-creating-a-voting-site/
Share on other sites

Thats actually pretty simple. I assume you are creating your users in the database with a unique id. What you can do is when the form that you create for the voting is posted you can then insert the user that is logged in user id and vote into a new table storing there user_id and there selection. Then when you display the form if the users id is in the new table you will just display something saying that they have already voted.

This site is great for beginners:

 

http://www.w3schools.com/PHP/php_mysql_intro.asp

 

An easy way to ensure that each person can only vote once is by adding a has_voted property to your users. However, if you intend to do multiple polls/surveys then perhaps it would be better to log user names or IDs along the voting data for each candidate/answer and check whether they have already voted before logging the vote or even displaying the form. Just like ngreenwood6 said really.

i am working on getting a basic knowledge of php here is the code i have for sending the data from the voting site to mysql. the problem here is that i am not getting any actual data from the radio buttons. when i select other and fill in the text i get it in the database but not the radio selections. any pointers in the right direction would be great thanks.

the radio buttons are in four groups with an other option with text type in.

 

<?

$president=$_POST['Selection1'];

$vp=$_POST['RadioGroup2'];

$tr=$_POST['RadioGroup3'];

$sect=$_POST['RadioGroup1'];

mysql_connect("website", "username", "passwrod") or die(mysql_error());

mysql_select_db("membership") or die(mysql_error());

mysql_query("INSERT INTO `data` VALUES ('$president', '$vp', '$tr', '$sect')");

Print "Your information has been successfully added to the database.";

?>

 

I recently helped on a project that is exactly what you are trying to do.  A school voting site.  Here are the things that you need to consider.

 

1. You need to have the option of dividing up the classes into groups.  ie. 8th grade, 9th grade.  This is because some will use it for individual grades.

2. You need to make it flexible, so that schools can also use it for other things such as: voting for banners, shirts, uni-forms, etc.

3. You should make it where you can upload images (of the subject, or person) as well as fill in a short description/biography.

4. You should make the voting tied to the user's/ students ID, so they can only vote once per election/poll.

5. You may want to give the user an option of several different layouts.

6. You may want to give the user the option of showing the results after a vote, or waiting until the voting time limit is over.

 

There is much more, but this was all things that the customers came back and asked to be added in.

 

As for your code, there are a couple of things you could add to it for debugging purposes.

<?php
//Show all $_POST variables the page is receiving.
echo '<pre>Debugging:'; print_r($_POST); echo '</pre>';
reset($_POST); //print_r() moves the array pointer to the last value, this resets it to number one.
//OR,
var_dump($_POST);
?>

still cant seem to get the data to pass from the radio buttons to the actual mysql. it populates the database with an empty file. here is the html along with the php.

 

<form id="form1" name="form1" method="post" action="process.php">

 

<p>President</p>

  <p>

    <label>

      <input type="radio" name="president" value="field11" id="Selection1_0" />

Me</label>

    <br />

    <label>

      <input type="radio" name="president" value="field12" id="Selection1_1" />

You</label>

    <br />

    <label>

      <input type="radio" name="president" value="field13" id="Selection1_2" />

Other </label>

    <label>

    <input type="text" name="president" id="Selection1_3" value="" size="25" />

  </p>

  <p>

    </label>

  </p>

<p>VicePresident</p>

  <p>

    <label>

    <input type="radio" name="vp" value="field21" id="RadioGroup2_0" />

John</label>

    <br />

    <label>

    <input type="radio" name="vp" value="field22" id="RadioGroup2_1" />

Marty</label>

    <br />

    <label>

    <input type="radio" name="vp" value="field23" id="RadioGroup2_2" />

Other</label>

    <input type="text" name="vp" id="RadioGroup2-3" value="" size="25" />

    </label>

  </p>

  <p> </p>

<p>Treasurer</p>

  <p>

    <label>

    <input type="radio" name="tr" value="field31" id="RadioGroup3_0" />

Bob</label>

    <br />

    <label>

    <input type="radio" name="tr" value="field32" id="RadioGroup3_1" />

Rob</label>

    <br />

    <label>

    <input type="radio" name="tr" value="field33" id="RadioGroup3_2" />

Other</label>

    <input type="text" name="tr" id="RadioGroup3_3" value="" size="25" />

    </label>

  </p>

<p>Sectretary</p>

  <p>

    <label>

    <input type="radio" name="sect" value="field41" id="RadioGroup1_0" />

Richard</label>

    <br />

    <label>

    <input type="radio" name="sect" value="field42" id="RadioGroup1_1" />

Bella</label>

    <br />

    <label>

    <input type="radio" name="sect" value="field43" id="RadioGroup1_2" />

Other</label>

    <label>

    <input type="text" name="sect" id="RadioGroup1_3" value="" size="25" />

    </label>

  </p>

  <p>

    <label>

    <input type="reset" name="reset" id="reset" value="Reset" />

    <input type="submit" name="Submit" id="Submit" value="Submit" />

    </label>

    <br />

  </p>

</form>

 

 

<?php

//Show all $_POST variables the page is receiving.

echo '<pre>Debugging:'; print_r($_POST); echo '</pre>';

reset($_POST); //print_r() moves the array pointer to the last value, this resets it to number one.

//OR,

var_dump($_POST);

 

$president=$_POST['field11'];

$vp=$_POST['field21'];

$tr=$_POST['field31'];

$sect=$_POST['field41'];

mysql_connect("localhost", "root", "kungfu") or die(mysql_error());

mysql_select_db("membership") or die(mysql_error());

mysql_query("INSERT INTO `data` VALUES ('$president', '$vp', '$tr', '$sect')");

Print "Your information has been successfully added to the database.";

?>

 

i know the php is not complete and i am missing something i just dont know what.

 

 

sorry this is the correct php file.

 

<?php

//Show all $_POST variables the page is receiving.

echo '<pre>Debugging:'; print_r($_POST); echo '</pre>';

reset($_POST); //print_r() moves the array pointer to the last value, this resets it to number one.

//OR,

var_dump($_POST);

 

$president=($_POST['president']);

$vp=($_POST['vp']);

$tr=($_POST['tr']);

$sect=($_POST['sect']);

mysql_connect("localhost", "root", "kungfu") or die(mysql_error());

mysql_select_db("membership") or die(mysql_error());

mysql_query("INSERT INTO `data` VALUES ('$president', '$vp', '$tr', '$sect')");

Print "Your information has been successfully added to the database.";

?>

ok so i figured it out with some help on a separate site but figured i would post the answer anyways so if anyone else has an issue like this they can figure it out. the problem was the text boxes. they were named the same as the radio button and this caused an override of the radio buttons. the fix was to rename the text boxes and access them using an if statement.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.