Je suis en train de faire mes 1er pas en PHP et j'essaye de faire, pour une association, une petite appli de vote sur internet en PHP-MySql.
J'ai prévu 4 pages :
1/ - un formulaire index.php où on doit saisir un code, pour accéder au vote. Si on a déja voté-->fin de l'appli.
2/ - une page vote.php (formulaire de vote)
3/ - un page valid.php : confirmation du vote et envoi du vote dans la BDD
4/ - results.php qui affiche le résultat du vote.
on utilise 2 tables distinctes dans la BDD : 1 pour l'émargement, et une autre pour les votes
Code : Tout sélectionner
CREATE TABLE `vote_rennes` (
`vote_id` int(11) NOT NULL auto_increment COMMENT 'Index',
`vote_key` varchar(20) NOT NULL COMMENT 'Identifiant',
`vote_choix` int(1) NOT NULL default '0' COMMENT 'Choix',
`vote_flag` int(1) NOT NULL default '0' COMMENT 'Emargement auto',
UNIQUE KEY `vote_key` (`vote_key`),
KEY `vote_id` (`vote_id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='Consultation élections' AUTO_INCREMENT=3 ;Code : Tout sélectionner
CREATE TABLE `vote_result` (
`vote_id` int(11) NOT NULL auto_increment COMMENT 'Index',
`vote_choix` int(1) NOT NULL default '0' COMMENT 'Choix',`vote_flag` int(1) NOT NULL default '0' COMMENT 'Emargement auto',
KEY `vote_id` (`vote_id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='Consultation élections' AUTO_INCREMENT=3 ;
Démo ici : http://www.rabiet.fr/vote
index.php
saisie du code
<html>
<head>
<title>Consultation Adhérents</title>
</head>
<body>
<div align="center">
<center>
<form method="POST" action="test.php">
<input type="text" name="Identifiant" size="20">
<input type="submit" value="Envoyer" name="B1">
<input type="reset" value="Rétablir" name="B2"></p>
</form>
</center>
</div>
</body>
</html>
vote.php
qui affiche la variable 'Identifiant'
et le résultat du vote
<html>
<head>
<script language="JavaScript">
<!--
function test(champ) {
for(i=0;i<champ.length;i++)
if(champ[i].checked) return true
alert("Faites un choix !")
return false
}
//-->
</script>
<head>
<body>
<?php
$Identifiant = isset($_POST['Identifiant']) ? $_POST['Identifiant'] : '';
$vote_choix = isset($_POST['vote_choix']) ? $_POST['vote_choix'] : '';
if ($Identifiant == "")
header("location: index.php");
{
// Connexion à la BD
include("_conn.php");
$result = mysql_query("SELECT vote_key FROM vote_emargement WHERE vote_key = '". addslashes($_POST["Identifiant"]) ."'");
$count = mysql_num_rows($result);
// Si l'utilisateur n'est pas trouvé dans la base de données
if($count <= 0)
{
// Redirection vers la page index.php
header("location: index.php");
exit;
}
}
// Les options du formulaire
$options = array(
'Option 1',
'Option 2',
'Option 3'
);
echo "<p align='center'>Identifiant : ".$Identifiant." vérifié</p><br />";
echo "<p align='center'>Choisissez une option : ".$vote_choix."</p><br />";
?>
<center>
<form name="form1" action="valid.php" method="POST">
<?php
foreach ($options as $v) {
if ($v == $vote_choix) {
echo '<input type="radio" name="vote_choix" value="' . $v . '" checked/>' . $v . '<br/>';
} else {
echo '<input type="radio" name="vote_choix" value="' . $v . '"/>' . $v . '<br/>';
}
}
?>
<input type="hidden" name="Identifiant"
value="<? echo $Identifiant; ?>">
<p> </p>
<input type="submit" value="Voter" onSubmit="return test(this.vote_choix)" >
</form>
</center>
</body>
</html>
et valid.php
<?php
$Identifiant = isset($_POST['Identifiant']) ? $_POST['Identifiant'] : '';
$vote_choix = isset($_POST['vote_choix']) ? $_POST['vote_choix'] : '';
echo "<p align='center'> Votre identifiant est : ".$Identifiant." </p><br/>";
echo "<p align='center'> Vous avez choisi : <b>".$vote_choix." </b></p><br/>";
?>
A ce stade, j'ai 2 points que je n'arrive pas à traiter:1°/ comment faire un test sur le code saisi dans index.php pour accéder accèder au vote si on a pas déjà voté ('vote_flag'=0), ou a un message "vous avez déjà voté" si 'vote_flag'=1.
2°/ comment s'assurer qu'un choix a bien été fait avec les boutons radio de la page vote.php
Je préférerai faire le test en php, mais j'ai des pb avec la commande "header". J'ai essayé en javascript mais ma solution ne marche pas.
D'avance, merci s'il y a une "bonne âme" pour m'aider.