Précédent   Forum des professionnels en informatique > Webmasters - Développement Web > AJAX
AJAX Forum sur la programmation AJAX. Avant de poster : Cours AJAX, FAQ AJAX, Toutes les FAQ JavaScript
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 21/06/2011, 13h36   #1
Candidat au titre de Membre du Club
 
Inscription : mars 2007
Messages : 52
Détails du profil
Informations forums :
Inscription : mars 2007
Messages : 52
Points : 11
Points : 11
Par défaut Validation de formulaire en AJAX : captcha non reconnu

Bonjour à tous,

J'ai un formulaire dont les champs (adresse email et code antispam) sont validés en AJAX. La vérif du champ email se passe très bien...
Le problème est que, même si le code antispam recopié est conforme à l'image captcha affichée, ça me renvoie mon msg d'erreur "code incorrect".

Voilà le code du form :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
<div id="form">
<form method="post" action="" enctype="multipart/form-data" onsubmit="validForm();return false;">
  <fieldset>
    <label for="email">Adresse email :</label>
    <input type="text" name="email" id="email" size="30" /><br />			
    <label for="antispam">Code anti-spam :</label>
    <a onclick="actualiserCaptcha();" title="Si ce code vous parait illisible, cliquez sur l'image pour le changer...">
      <img src="antispam.php" id="img_antispam" />
    </a>
    <input type="text" id="antispam" size="10" />
    <input type="submit" id="confirmation" value="OK" />
</fieldset>
</form>
</div>
Voici le code pour la génération du captcha :
Code :
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
 
<?php
session_start();
 
if ( !defined('ABSPATH') ) define('ABSPATH', dirname(__FILE__) . '/');
 
function getCode($length) {
	$chars = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
	$rand_str = '';
	for ($i=0; $i<$length; $i++) {
		$rand_str .= $chars{ mt_rand( 0, strlen($chars)-1 ) };
	}
	return $rand_str;
}
 
$theCode = getCode(5);
 
$_SESSION['antispam'] = md5($theCode);
 
$char1 = substr($theCode,0,1);
$char2 = substr($theCode,1,1);
$char3 = substr($theCode,2,1);
$char4 = substr($theCode,3,1);
$char5 = substr($theCode,4,1);
 
$fonts = glob('rssrcs/polices/*.ttf');
 
$image = imagecreatefrompng('antispam.png');
 
$colors=array (	imagecolorallocate($image, 200,28,26),
				imagecolorallocate($image, 44,184,193),
				imagecolorallocate($image, 169,11,175),
				imagecolorallocate($image, 252,161,3),
				imagecolorallocate($image, 192,207,11) );
 
function random($tab) {
	return $tab[array_rand($tab)];
}
 
imagettftext($image, 16, -10, 5, 25, random($colors), ABSPATH .'/'. random($fonts), $char1);
imagettftext($image, 19, 20, 25, 25, random($colors), ABSPATH .'/'. random($fonts), $char2);
imagettftext($image, 15, -35, 40, 25, random($colors), ABSPATH .'/'. random($fonts), $char3);
imagettftext($image, 17, 25, 60, 25, random($colors), ABSPATH .'/'. random($fonts), $char4);
imagettftext($image, 15, -15, 75, 25, random($colors), ABSPATH .'/'. random($fonts), $char5);
 
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>
Voici la fonction AJAX de validation du form :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
function validForm() {
	var xhr = null;
	var donnees = "";
	xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			reponse = xhr.responseText;
			document.getElementById('div_contenu').innerHTML = reponse;
		}
	}
	xhr.open("POST","verif.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	adr_email = document.getElementById('email').value;
	code_captcha = document.getElementById('antispam').value;
	donnees="adrEmail="+adr_email+"&captcha="+code_captcha;
	xhr.send(donnees);
}
Et enfin le fichier verif.php :

Code :
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
 
<?php
require "mesfonctions.php";
 
header('Content-Type: text/html; charset=utf-8');
	echo '<div id="form">';
 
	if ( ($_POST['adrEmail']=="") || (is_null($_POST['adrEmail'])) || (empty($_POST['adrEmail'])) ) {
		echo '<h3>OUPS... Vous n\'avez pas saisi votre adresse email !</h3>
			<form method="post" action="" enctype="multipart/form-data" onsubmit="validForm();return false;">
				<fieldset>
					<label for="email">Adresse email :</label><input type="text" name="email" id="email" size="30" onfocus="javascript:this.value=\'\'" value="Votre adresse email" /><br />			
					<label for="antispam">... Code anti-spam :</label><a onclick="actualiserCaptcha();" title="Si ce code vous parait illisible, cliquez sur l\'image pour le changer..."><img src="antispam.php" id="img_antispam" /></a>
					<input type="text" name="antispam" id="antispam" size="10" />
					<input type="submit" name="confirmation" id="confirmation" value="OK" />
				</fieldset>
			</form>';
	}
	else if (!verifEmail($_POST['adrEmail'])) {
		echo '<h3>OUPS... L\'adresse email que vous avez saisie n\'est pas valide !</h3>
			<form method="post" action="" enctype="multipart/form-data" onsubmit="validForm();return false;">
				<fieldset>
					<label for="email">Adresse email :</label><input type="text" name="email" id="email" size="30" onfocus="javascript:this.value=\'\'" value="Votre adresse email" /><br />			
					<label for="antispam">... Code anti-spam :</label><a onclick="actualiserCaptcha();" title="Si ce code vous parait illisible, cliquez sur l\'image pour le changer..."><img src="antispam.php" id="img_antispam" /></a>
					<input type="text" name="antispam" id="antispam" size="10" />
					<input type="submit" name="confirmation" id="confirmation" value="OK" />
				</fieldset>
			</form>';
	}
	else if ( ($_POST['captcha']=="") || (is_null($_POST['captcha'])) || (empty($_POST['captcha'])) ) {
		$rappelEmail = $_POST['adrEmail'];
		echo '<h3>OUPS... Vous n\'avez pas recopié le code anti-spam !</h3>
			<form method="post" action="" enctype="multipart/form-data" onsubmit="validForm();return false;">
				<fieldset>
					<label for="email">Adresse email :</label><input type="text" name="email" id="email" size="30" onfocus="javascript:this.value=\'\'" value="'.$rappelEmail.'" /><br />			
					<label for="antispam">... Code anti-spam :</label><a onclick="actualiserCaptcha();" title="Si ce code vous parait illisible, cliquez sur l\'image pour le changer..."><img src="antispam.php" id="img_antispam" /></a>
					<input type="text" name="antispam" id="antispam" size="10" />
					<input type="submit" name="confirmation" id="confirmation" value="OK" />
				</fieldset>
			</form>';
	}
	else if ( md5(strtoupper($_POST['captcha']))!=$_SESSION['antispam'] ) {
		$rappelEmail = $_POST['adrEmail'];
		echo '<h3>OUPS... Le code anti-spam que vous avez recopié n\'est pas correct ! !</h3>
			<form method="post" action="" enctype="multipart/form-data" onsubmit="validForm();return false;">
				<fieldset>
					<label for="email">Adresse email :</label><input type="text" name="email" id="email" size="30" onfocus="javascript:this.value=\'\'" value="'.$rappelEmail.'" /><br />			
					<label for="antispam">... Code anti-spam :</label><a onclick="actualiserCaptcha();" title="Si ce code vous parait illisible, cliquez sur l\'image pour le changer..."><img src="antispam.php" id="img_antispam" /></a>
					<input type="text" name="antispam" id="antispam" size="10" />
					<input type="submit" name="confirmation" id="confirmation" value="OK" />
				</fieldset>
			</form>';
	}
	else {
		//script de traitement du form validé...
 
	}
	echo '</div>';
 
?>
J'ai testé la valeur de $_SESSION['antispam'] mais celle n'est pas définie (!isset). Le pb vient donc de là, mais comment le résoudre, là je patauge complètement...

Merci beaucoup pour votre aide
@+
Marmotton76 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/06/2011, 12h53   #2
Responsable Développement Web

 
Avatar de Bovino
 
Homme Didier Mouronval
Développeur Web
Inscription : juin 2008
Messages : 13 807
Détails du profil
Informations personnelles :
Nom : Homme Didier Mouronval
Âge : 41
Localisation : France, Gironde (Aquitaine)

Informations professionnelles :
Activité : Développeur Web
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : juin 2008
Messages : 13 807
Points : 35 803
Points : 35 803
Citation:
J'ai testé la valeur de $_SESSION['antispam'] mais celle n'est pas définie
Ben... t'as pas l'impression qu'il te manque un petit session_start(); dans la page verif.php
__________________
Pas de question technique par MP !
Tout le monde peut participer à developpez.com, vous avez une idée, contactez-moi !
Vous possédez un blog et aimeriez diffuser vos billets sur le forum, contactez-moi !
Mes formations video2brain : La formation complète sur JavaScriptJavaScript et le DOM par la pratiquePHP 5 et MySQL : les fondamentaux
Mon livre sur jQuery
Bovino est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 22h49.


 
 
 
 
Partenaires

Hébergement Web