Bonjour,

Je souhaite créer une pétition en ligne. J'ai donc créé un formulaire où il faut indiquer le Pseudo, un Mot de passe, et les Univers habités, puis une option facultative, le commentaire.
Toutes ces infos sont ensuite insérées dans un fichier txt (infos séparées par une tabulation)

Je ne m'y connais pas trop en PHP et je me heurte à plusieurs problèmes :

Pour le choix multiple des univers, j'ai réussi à créer l'apparence dans le formulaire, mais il n'a pas l'air de fonctionner !
En effet, si je sélectionne Univers 1, 10, 12 et 15, il me donne que le 15 !
Je ne vois pas mon erreur dans le script pourtant :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php 	include_once( "petition.lib.php" ); ?>
<html>
<head>
<title>Anti-officiers :: Pétition</title>
......
<table cellspacing='16' cellpadding='0' border='0' align='center' ><tr><td>
<font class='form_title'></font>
</td></tr></table>
 
<?php
	if( !$isHideForm ): 
		global $sErr ;
		if( $sErr ) print "<br><a name='error'></a><center><font class='form_error' >$sErr</font></center><br>"; 
 
		$starColor = $sErr ? "#ff0000" : "#000000";
		$style=" class='form_text' ";
 ?>
 
<form name="send" action="<?php print PHP_SELF ?>" method='post' enctype='multipart/form-data'>
<input type='hidden' name='formmail_submit' value='Y'>
<input type='hidden' name='esh_formmail_recipient' value="mathieu.lamoureux@cegetel.net">
<input type='hidden' name='esh_formmail_subject' value="Anti-officiers">
<input type='hidden' name='esh_formmail_cc' value="">
<input type='hidden' name='esh_formmail_bcc' value="">
<input type='hidden' name='esh_formmail_return_subject' value="">
<input type='hidden' name='esh_formmail_return_msg' value="">
<input type='hidden' name='esh_formmail_mail_and_file' value="">
<input type='hidden' name='esh_formmail_charset' value="">
 
<table cellspacing='16' cellpadding='0' border='0'  >
	<tr>
		<td class="form_field" valign='top' align='right'><font color="#ffffff">Pseudo :</font></td><td width='10'  aligh='right' valign='top'> <font size='2' color='#ff0000'>*</font> </td>
		<td class="form_text">
<input type="text" name="Pseudo"  value="<?php  print HtmlSpecialChars( $HTTP_POST_VARS[ "Pseudo" ] ); ?>" class='text_box'>
		</td>
	</tr>
 
	<tr>
		<td class="form_field" valign='top' align='right'><font color="#ffffff">Mot de passe :</font></td><td width='10'  aligh='right' valign='top'> <font size='2' color='#ff0000'>*</font> </td>
		<td class="form_text">
<input type="password" name="Mot_de_passe"  value="<?php  print HtmlSpecialChars( $HTTP_POST_VARS[ "Mot_de_passe" ] ); ?>" class='text_box'>
		</td>
	</tr>
 
	<tr>
		<td class="form_field" valign='top' align='right'><font color="#ffffff">Univers :</font></td><td width='10'  aligh='right' valign='top'> <font size='2' color='#ff0000'>*</font> </td>
		<td class="form_text">
<select name="Univers[]" size="5" multiple="multiple">
<option  value="Univers 1"  <?php  formSelected( $HTTP_POST_VARS[ "Univers" ], "Univers 1" ); ?> > Univers 1
...
<option  value="Univers 51"  <?php  formSelected( $HTTP_POST_VARS[ "Univers" ], "Univers 51" ); ?> > Univers 51
</select>
 
		</td>
	</tr>
 
	<tr>
		<td class="form_field" valign='top' align='right'><font color="#ffffff">Commentaire :</font></td><td width='10'  aligh='right' valign='top'></td>
		<td class="form_text">
<textarea name="Commentaire" rows=4 cols=25 ><?php  print HtmlSpecialChars( $HTTP_POST_VARS[ "Commentaire" ] ); ?></textarea>
 
		</td>
	</tr>
 
 
	<tr><td colspan=3 align='center'><input type='submit' onClick="valider();" value='Valider'> &nbsp;&nbsp; <input type='button' value='Annuler' onclick="location.href='/';"></td></tr>
</table>
</form>
 
<?php
		if( $sErr ) print "<script language='javascript' type='text/javascript'>location.href='#error';</script>";;; 
 
else: //!$isHideForm
	print( "<br><br><hr><center><b>Tu as bien signé la pétition ! Merci.</b><br><br><input type='button' value='Retour &agrave; l&apos;accueil' onclick=\"location.href='/';\"></center><br><br>" );
endif; //!$isHideForm
			?>
</body>
</font>
</body>
</html>
J'aimerai donc avoir un éclaircissement sur ce sujet...
Je précise que le formulaire a un fichier nommé petition.php (le code ci-dessus) et un fichier petition.lib.php

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?
define( "ADMIN_MAIL", "s6software@users.sourceforge.net" ); // bug report email
define( "HOST_NAME", getEnv( "HTTP_HOST" ) );
define( "PHP_SELF", getEnv( "SCRIPT_NAME" ) );
define( "ERR_MISSING", "Missing required field : " );
define( "ERR_EMAIL", "Please type in a valid e-mail address : " );
define( "ERR_CREDIT_CARD_NUMBER", "Please check the credit card number : " );
define( "ERR_CREDIT_CARD_EXPIRED", "Please check the credit card expiry date : " );
define( "ERR_SELECT_UPLOAD", "Please select upload file : " );
 
error_reporting( E_ERROR | E_WARNING | E_PARSE );
?><?php
// --- Array of Form Elements ---
$form_mail[] = array( "name" => "Pseudo", "text" => "Pseudo",  "type" => "text", 
 
"required" => "Required" ) ;
$form_mail[] = array( "name" => "Mot_de_passe", "text" => "Mot de passe",  "type" => 
 
"password", "required" => "Required" ) ;
$form_mail[] = array( "name" => "Univers", "text" => "Univers",  "type" => 
 
"multiple", "required" => "Required" ) ;
$form_mail[] = array( "name" => "Commentaire", "text" => "Commentaire",  "type" => 
 
"textarea", "required" => "" ) ;
 
// -- Detech Submit & SendMail -- 
$isHideForm = false;
if( $HTTP_POST_VARS["formmail_submit"] ){
	$sErr = checkPass();
	if( ! $sErr ){
		sendFormMail( $form_mail, "liste.txt") ;
		$isHideForm = true;
 
		$redirect = "";
		if( strlen(trim($redirect)) ):
			header( "Location:$redirect" );
			exit;
		endif;
	}
}
?>
<?
// ===============================================
function    sendFormMail( $form_mail, $sFileName = ""  ) 
{ 
    global    $HTTP_POST_VARS ; 
 
	$to = $HTTP_POST_VARS["esh_formmail_recipient"]; // I don't detect spam at this moment. it's to do list.
	$from = "online.submit@" . HOST_NAME ;
	$subject = $HTTP_POST_VARS["esh_formmail_subject"]; 
 
	// first stage keep it simple:
	$sWhatToDo = $sFileName ? "mailandfile" : "" ; //$HTTP_POST_VARS["esh_formmail_mail_and_file"];
 
	//$sFileName = $HTTP_POST_VARS["esh_formmail_save_record_file"];
	$cc = $HTTP_POST_VARS["esh_formmail_cc"];
	$bcc = $HTTP_POST_VARS["esh_formmail_bcc"];
	$charset = $HTTP_POST_VARS["esh_formmail_charset"];
 
    for( $i = 0; $i < count( $form_mail ); $i ++ ){ 
        $value = trim( $HTTP_POST_VARS[ $form_mail[ $i ][ "name" ] ] ); 
        $content .= $form_mail[ $i ][ "text" ] . " \t : " . $value ."\n"; 
        $line .= remove_newline( $value ) . "\t" ; 
		if( strtolower("Sender's email") == strtolower($form_mail[ $i ][ "type" ]) ) {
			//print "Type:[" . $form_mail[ $i ][ "type" ] . "] $value <br>\n"; 
			$from = $value ;		
		}
    }; 
    $content .= "\n\nIP:" . getEnv( "REMOTE_ADDR" ); 
 
	switch( strtolower($sWhatToDo) ){
		case "mailandfile" :
	        mailAttachments( $to , $subject , $content,  $from,  $charset, $cc , $bcc ) ; 
    	    if( ! appendToFile( $sFileName, $line ) ) 
				mailReport( $content . "\n\nWrite Form Mail to File Fail." ); 
			break;			
 
		case "fileonly" :
    	    if( ! appendToFile( $sFileName, $line ) ) 
				mailReport( $content . "\n\nWrite Form Mail to File Fail.", $from ); 
			break;
 
		default :
	        mailAttachments( $to , $subject , $content,  $from,  $charset, $cc , $bcc ) ; 
	} 
 
	mailAutoResponse( $from ) ;
} 
 
 
//------------------------------------------------------------------------------------------ 
function mailAutoResponse( $to ){
    global    $HTTP_POST_VARS ; 
	$subject = $HTTP_POST_VARS["esh_formmail_return_subject"];
	$responseMsg = $HTTP_POST_VARS["esh_formmail_return_msg"];
	if( $to && $responseMsg ) 
		mail( $to, $subject, $responseMsg, "From: " . $HTTP_POST_VARS["esh_formmail_recipient"] );
}
 
//------------------------------------------------------------------------------------------ 
function mailReport( $content = "", $from = "" ){
	mail( ADMIN_MAIL, "Error@" . HOST_NAME . PHP_SELF, $content, "From:$from" );
}
 
//------------------------------------------------------------------------------------------ 
function	remove_newline( $str = "" ){
	$newliner = "<!--esh_newline-->" ; // replace \r\n with $newliner ;
	$newtaber = "<!--esh_newtaber-->" ; // replace \t with $newtaber ;
	$str = ereg_replace( "\t", $newtaber, $str );
	$str = ereg_replace( "\r\n", $newliner, $str );
	return ereg_replace( "\n", $newliner, $str );
}
 
//------------------------------------------------------------------------------------------ 
function	checkPass()
{
	global	$form_mail ;
	global	$HTTP_POST_VARS ;
    global    $HTTP_POST_FILES ; 
 
	for( $i = 0; $i < count( $form_mail ); $i ++ ){
		$type = strtolower( $form_mail[ $i ][ "type" ]  );
		$value = trim( $HTTP_POST_VARS[ $form_mail[ $i ][ "name" ] ] );
		$required = $form_mail[ $i ][ "required" ] ;
		$text = stripslashes( $form_mail[ $i ][ "text" ] );
 
		// simple check the field has something keyed in.
		if( !strlen($value) && (  $required == "Required" ) && $type != "attachment" )   
			return ERR_MISSING . $text  ;
 
		// verify the special case
		if( 
			( strlen($value) || $type == "attachment" ) 
			&&  $required == "Required" 
		):
			switch( $type ){
					case 	strtolower("Sender's Name") :
							  break;
					case 	strtolower("Generic email"):
					case 	strtolower("Sender's email"):
							   if( ! formIsEMail($value) )	 return ERR_EMAIL . 
 
$text ;
							   break;
					case	"text" :
								break;
					case 	"textarea" :
								break;
					case	"checkbox" :
					case 	"radio" :
								break;
					case 	"select" :
								break;
					case 	"attachment" :
								$upload_file = $HTTP_POST_FILES[ 
 
$form_mail[ $i ]["name"] ][ "tmp_name" ] ;
								if( ! is_uploaded_file($upload_file)  )
									return  ERR_SELECT_UPLOAD . 
 
$text; 
								break;
					case strtolower("Date(MM-DD-YYYY)"):
								break;
					case strtolower("Date(MM-YYYY)"):
								break;
					case strtolower("CreditCard(MM-YYYY)"):
								if( $value < date("Y-m") ) return 
 
ERR_CREDIT_CARD_EXPIRED  . $text; 
								break;
					case strtolower("CreditCard#"):
								if( !formIsCreditNumber( $value )  ) return 
 
ERR_CREDIT_CARD_NUMBER  . $text ;
								break;
					case strtolower("Time(HH:MM:SS)"):
								break;
					case strtolower("Time(HH:MM)"):
								break;
					default :
						//return $sErrRequired . $form_mail[ $i ][ "text" ];
				} // switch
		endif;
	} // for
 
	return "" ;
}
 
 
 
//------------------------------------------------------------------------------------------ 
function formSelected( $var, $val ) 
{ 
    echo ( $var == $val ) ? "selected" : ""; 
} 
 
 
//------------------------------------------------------------------------------------------ 
function formChecked( $var, $val ) 
{ 
    echo ( $var == $val ) ? "checked" : ""; 
} 
 
 
//------------------------------------------------------------------------------------------ 
function    formIsEMail( $email ){ 
        return ereg( "^(.+)@(.+)\\.(.+)$", $email ); 
} 
 
 
//------------------------------------------------------------------------------------------ 
function    selectList( $name, $selectedValue, $start, $end, $prompt = "-Select-", $style = "" ) 
{ 
    $tab = "\t" ; 
    print "<select name=\"$name\" $style>\n" ; 
    print $tab . "<option value=''>$prompt</option>\n" ; 
    $nLen = strlen( "$end" ) ; 
    $prefix_zero = str_repeat( "0", $nLen ); 
    for( $i = $start; $i <= $end ; $i ++ ){ 
        $stri = substr( $prefix_zero . $i, strlen($prefix_zero . $i)-$nLen, $nLen ); 
        $selected = ( $stri == $selectedValue ) ? " selected " : "" ; 
        print $tab . "<option value=\"$stri\" $selected >$stri</option>\n" ; 
    } 
    print "</select>\n\n" ; 
} 
 
//------------------------------------------------------------------------------------------ 
// something like CreditCard.pm in perl CPAN 
function formIsCreditNumber( $number ) { 
 
    $tmp = $number; 
    $number = preg_replace( "/[^0-9]/", "", $tmp ); 
 
    if ( preg_match(  "/[^\d\s]/", $number ) )  return 0; 
    if ( strlen($number) < 13  && 0+$number ) return 0;  
 
    for ($i = 0; $i < strlen($number) - 1; $i++) { 
        $weight = substr($number, -1 * ($i + 2), 1) * (2 - ($i % 2)); 
        $sum += (($weight < 10) ? $weight : ($weight - 9)); 
    } 
 
    if ( substr($number, -1) == (10 - $sum % 10) % 10  )  return $number; 
    return $number; 
} 
 
 
// -------------------------- Begin Mail Attachment Functions ----------------------------------------------------------------- 
function    mailAttachments( $to = "" , $subject = "" , $message = "" , $from = "support@lynx.net" , $charset = "iso-8859-1", $cc 
 
= "" , $bcc = "" ){ 
    global    $HTTP_POST_FILES ; 
 
        if( ! strlen( trim( $to ) ) ) return "Missing \"To\" Field." ; 
 
        $boundary = "====_My_PHP_Form_Generator_" . md5( uniqid( srand( time() ) ) ) . "====";  
 
        // setup mail header infomation 
        $headers = "From: $from\r\n";  
        if ($cc) $headers .= "CC: $cc\r\n";  
        if ($bcc) $headers .= "BCC: $bcc\r\n";  
		$plainHeaders = $headers ; // for no attachments header
        $headers .= "MIME-Version: 1.0\nContent-type: multipart/mixed;\n\tboundary=\"$boundary\"\n";  
 
        $txtMsg = "\nThis is a multi-part message in MIME format.\n" .  
                        "\n--$boundary\n" . 
                        "Content-Type: text/plain;\n\tcharset=\"$charset\"\n\n"  . $message . "\n"; 
 
        //create mulitipart attachments boundary 
        $sError = "" ; 
        $nFound = 0; 
        foreach( $HTTP_POST_FILES as $aFile ){ 
                    $sFileName = $aFile[ "tmp_name" ] ; 
                    $sFileRealName = $aFile[ "name" ] ; 
                    if( is_file( $sFileName ) ): 
 
                        if( $fp = fopen( $sFileName, "rb" ) ) : 
                            $sContent = fread( $fp, filesize( $sFileName ) ); 
                            $sFName = basename( $sFileRealName ) ; 
                            $sMIME = getMIMEType( $sFName ) ; 
 
                            $bPlainText = ( $sMIME == "text/plain" ) ; 
                            if( $bPlainText ) : 
                                $encoding = "" ; 
                            else: 
                                $encoding = "Content-Transfer-Encoding: base64\n";  
                                $sContent = chunk_split( base64_encode( $sContent ) );  
                            endif; 
 
                            $sEncodeBody .=     "\n--$boundary\n" .  
                                                        "Content-Type: $sMIME;\n" .  
                                                        "\tname=\"$sFName\"\n" . 
                                                        $encoding .  
                                                        "Content-Disposition: attachment;\n" .  
                                                        "\tfilename=\"$sFName\"\n\n" . 
                                                        $sContent . "\n" ; 
                            $nFound ++;                                                 
                        else: 
                            $sError .= "<br>File $sFileName can not open.\n" ; 
                        endif; // if( $fp = fopen( $sFileName, "rb" ) ) : 
 
                    else: 
                        $sError .= "<br>File $sFileName doesn't exist.\n" ; 
                    endif; //if( file_exists( $sFileName ) ): 
        }; // end foreach 
 
         $sEncodeBody .= "\n\n--$boundary--" ; 
         $sSource = $txtMsg . $sEncodeBody ; 
 
 
		 $nFound ? mail( $to, $subject, $sSource, $headers  )
		                : mail( $to, $subject, $message, $plainHeaders );  
 
        return $sError ;         
} 
 
/* --------------------------------------------------------------------------------------------------- 
    Parameters: $sFileName 
    Return : 
        1. "" :  no extendsion name, or sFileName is empty 
        2. string: MIME Type name of array aMimeType's definition. 
   ---------------------------------------------------------------------------------------------------*/ 
function    getMIMEType( $sFileName = "" ) { 
 
        $sFileName = strtolower( trim( $sFileName ) ); 
        if( ! strlen( $sFileName  ) ) return ""; 
 
        $aMimeType = array(  
                                        "txt" => "text/plain" , 
                                        "pdf" => "application/pdf" , 
                                        "zip" => "application/x-compressed" , 
 
                                        "html" => "text/html" , 
                                        "htm" => "text/html" , 
 
                                        "avi" => "video/avi" , 
                                        "mpg" => "video/mpeg " , 
                                        "wav" => "audio/wav" , 
 
                                        "jpg" => "image/jpeg " , 
                                        "gif" => "image/gif" , 
                                        "tif" => "image/tiff " , 
                                        "png" => "image/x-png" , 
                                        "bmp" => "image/bmp"  
                                    ); 
        $aFile = split( "\.", basename( $sFileName ) ) ; 
        $nDiminson = count( $aFile ) ; 
         $sExt = $aFile[ $nDiminson - 1 ] ; // get last part: like ".tar.zip", return "zip" 
 
        return ( $nDiminson > 1 ) ? $aMimeType[ $sExt ] : "";  
} 
// -------------------------- End Mail Attachment Functions ----------------------------------------------------------------- 
 
 
//------------------------------------------------------------------------------------------ 
function    appendToFile( $sFileName = "", $line = "" ){ 
    if( !$sFileName || !$line ) return 0; 
    $hFile = fopen( "$sFileName", "a+w" ); 
    $nBytes = 0; 
    if( $hFile ){ 
        $nBytes = fputs( $hFile , trim($line)."\r\n" ); 
        fclose( $hFile ); 
    }; 
    return $nBytes ; 
} 
?>

Ensuite, ce problème résolu ou pas, je me heurte à un second problème qui est la comptabilisation des voix. J'ai cherché comme insérer des données à partir d'un fichier txt mais rien d'intéressant ou de compréhensible par moi ^^

Je cherche donc les fonctions php permettant d'afficher le nombre de signatures totales, d'afficher le nombre de fois que chaque univers a été sélectionné, et d'afficher les derniers commentaires posés.

Exemple :
User 1 : univers 5, 6, 12 !! Les officiers, c'est pourri
User 2 : univers 12, 18 !! Quel gachis de faire payer
User 3 : univers 6 !! OGame devient un site commercial
User 4 : univers 23, 49 !! GF fait fort !
User 5 : univers 49, 50, 51 !!
User 6 : univers 51 !! Pas cool !

Ce qui donnerait :
La pétition a été signée par 6 personnes.
Univers 1 : 0 utilisateur
...
Univers 5 : 1 utilisateur
Univers 6 : 2 utilisateurs
etc...

Et les 5 derniers commentaires sont affichés, en sautant bien sûr le vide
D'après ce que j'ai failli comprendre du php, je crois que je dois utiliser input et compagnie, mais après...^^

Puis je me heurte (encore !) à un autre problème : la désinscription.
C'est simple à dire, mais pour moi, c'est dur à faire ! Un formulaire qui enlèverait la ligne concernant l'utilisateur et donc sa voix, si celui-ci fournit son pseudo et son mot de passe.

Après, ce n'est pas un problème, mais juste une question, est-ce que chaque connexion internet n'a droit qu'à une seule signature, là ? Si non, comment le sécuriser (nom de la méthode, je me débrouillerai pour le faire)


Merci beaucoup à ceux qui m'aideront !


EDIT: voici le lien du formulaire pour un aperçu : http://solidifis-unium.goldzoneweb.info/petition.php