8791 sujets

Développement web côté serveur, CMS

Bonjour,

J'essaye de réaliser un formulaire d'inscription en PHP pour mon site. Seulement, j'ai quasiment terminé mais rien n'est envoyé dans le $_POST. Cela fait 3 fois que je recommence et je ne vois pas d'où cela peut venir...

Je vous mets mes 3 fichiers :

Le fichier de classe (fmanager.php) :


<?php
class FormManager{
	private $nom;
	private $erreurs;
	private $prenom;
	private $login;
	private $pwd;
	private $pwd2;
	private $email;
	private $struc;
	private $cp;
	private $ville;
	private $tel;
	private $check;
	public function __construct($tab){
		if(is_array($tab)){
			$nom= htmlspecialchars(stripslashes(trim($tab["nom"])), ENT_QUOTES);
			$prenom= htmlspecialchars(stripslashes(trim($tab["prenom"])), ENT_QUOTES);
			$login= htmlspecialchars(stripslashes(trim($tab["login"])), ENT_QUOTES);
			$email= htmlspecialchars(stripslashes(trim($tab["email"])), ENT_QUOTES);
			$pwd= htmlspecialchars(stripslashes(trim($tab["pwd"])), ENT_QUOTES);
			$pwd2= htmlspecialchars(stripslashes(trim($tab["pwd2"])), ENT_QUOTES);
			$struc= htmlspecialchars(stripslashes(trim($tab["struc"])), ENT_QUOTES);
			$cp= htmlspecialchars(stripslashes(trim($tab["cp"])), ENT_QUOTES);
			$ville= htmlspecialchars(stripslashes(trim($tab["ville"])), ENT_QUOTES);
			$tel= htmlspecialchars(stripslashes(trim($tab["tel"])), ENT_QUOTES);
		}
	}
	public function formulaire(){
		$html=<<<EOT
			<form method="post" action="forms.php">
						<fieldset>
							<legend>Formulaire d'inscription</legend>
							<br/>
							<dl>
								<dt><label for="prenom">Votre prénom</label></dt>
								<dd><input type="text" name="prenom" value="{$this->prenom}" size="25" maxlength="15" />{$this->erreurs["prenom"]}</dd>
							</dl>
							<dl>
								<dt><label for="nom">Votre nom</label></dt>
								<dd><input type="text" name="nom" value="{$this->nom}" size="25" maxlength="15" />{$this->erreurs["nom"]}</dd>
							</dl>
							<dl>
								<dt><label for="login">Votre nom d'utilisateur</label></dt>
								<dd><input type="text" name="login" value="{$this->login}" size="25" maxlength="15" />{$this->erreurs["login"]}</dd>
							</dl>
							<dl>
								<dt><label for="pwd">Votre mot de passe</label></dt>
								<dd><input type="password" name="pwd" size="25" maxlength="12" />{$this->erreurs["pwd"]}</dd>
							</dl>
							<dl>
								<dt><label for="login">Confirmez votre mot de passe</label></dt>
								<dd><input type="password" name="pwd2" size="25" maxlength="12" />{$this->erreurs["pwd2"]}</dd>
							</dl>
							<dl>
								<dt><label for="email">Votre E-mail</label></dt>
								<dd><input type="text" name="email" value="{$this->email}" size="25" maxlength="20" />{$this->erreurs["email"]}</dd>
							</dl>
							<dl>
								<dt><label for="struc">Votre entreprise / association</label></dt>
								<dd><input type="text" name="struc" value="{$this->struc}" size="25" maxlength="25" />{$this->erreurs["struc"]}</dd>
							</dl>
							<dl>
								<dt><label for="cp">Votre code postal</label></dt>
								<dd><input type="text" name="cp" value="{$this->cp}" size="5" maxlength="5" />{$this->erreurs["cp"]}</dd>
							</dl>
							<dl>
								<dt><label for="ville">Votre ville</label></dt>
								<dd><input type="text" name="ville" value="{$this->ville}" size="25" maxlength="15" />{$this->erreurs["ville"]}</dd>
							</dl>
							<dl>
								<dt><label for="tel">Votre numéro de telephone</label></dt>
								<dd><input type="text" name="tel" value="{$this->tel}" size="10" maxlength="10" />{$this->erreurs["tel"]}</dd>
							</dl>
							<hr class="clear"/>
							<input class="submitbutton" name="form_submitted" type="submit" value="Envoyer"/> 
							<input name="form_reset" type="reset" value="Recommencer"/>
						</fieldset>
					</form>		
EOT;
		return $html;
	}
	public function verifLogin() {
		include('connexion.inc.php');
		mysql_select_db("db235149319");
		$query="SELECT login FROM identification WHERE login='{$this->login}'";
		if(!mysql_query($query)) {
			return false;
		}
		else return true;
	}
	public function verifCP() {
		if($this->cp.length==5) {
			$test= "^([0-9]{5})$";
			return preg_match($test,$this->cp);
		}
	else return false;
	}
	public function verifMail() {
		if (preg_match("/[a-zA-Z0-9-.]+\@[a-zA-Z0-9-.]+/",$this->email)){
			include('connexion.inc.php');
			mysql_select_db("db235149319");
			$query="SELECT mail FROM identification WHERE mail='{$this->email}'";
			if(!mysql_query($query)) {
				return false;
			}
			else return true;
		}
 		else return false;
	}
	function generateKey() {
		$caracteres = array("a", "b", "c", "d", "e", "f", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
		$caracteres_aleatoires = array_rand($caracteres, 8);
		$key = "";
		foreach($caracteres_aleatoires as $i) {
			  $key .= $caracteres[$i];
			}
		return $key;
	}
	public function verifier(){
		$this->erreurs= array("prenom"=>null, "nom"=>null, "login"=>null, "pwd"=>null, "pwd2"=>null, "email"=>null, "struc"=>null, "cp"=>null, "ville"=>null, "tel"=>null);
		$verifer=TRUE;
		if($this->nom==null){
			$this->erreurs["nom"]="Entrez votre nom";
			$verifier=FALSE;
		}
		if($this->prenom==null){
			$this->erreurs["prenom"]="Entrez votre prenom";
			$verifier=FALSE;
		}
		if($this->login!=null){
			if(!verifLogin()) {
			$this->erreurs["login"]="Ce nom d'utilisateur est déjà pris";
			$verifier=FALSE;
			}
		}
		else {
			$this->erreurs["login"]="Entrez votre nom d'utilisateur";
			$verifier=FALSE;
		}
		if($this->email!=null){
			if(!verifMail()) {
			$this->erreurs["email"]="Cette adresse est déjà prise ou n'est pas valide";
			$verifier=FALSE;
			}
		}
		else {
			$this->erreurs["email"]="Entrez votre adresse électronique";
			$verifier=FALSE;
		}
		if($this->pwd== null){
			$this->erreurs["pwd"]="Entrez votre mot de passe";
			$verifier=FALSE;
		}
		if($this->pwd2!=null){
			if($this->pwd2!=$this->pwd) {
				$this->erreurs["pwd2"]="Entrez le même mot de passe qu'au dessus";
				$verifier=FALSE;
			}
		}
		else {
			$this->erreurs["pwd2"]="Confirmez votre mot de passe";
			$verifier=FALSE;
		}
		if($this->cp!=null){
			if(!verifCP()) {
				$this->erreurs["cp"]="Entrez un code postal correct";
				$verifier=FALSE;
			}
		}
		return $verifier;
	}
	public function afficher(){
		$html=<<<EOT
			<fieldset>
				<legend>Inscription non réussie</legend>
				Nous sommes désolés mais l'inscription ne s'est pas déroulé correctement faute d'un problème technique...<br/><br/>
				Veuillez nous excuser pour la gêne occasionnée. Il vous faudra réessayer ultérieurement.
			</fieldset>
EOT;
		$mail_message="Bonjour ".$this->prenom." ".$this->nom.",<br/><br/>L'équipe Lambda Comm. vous remercie de votre confiance.<br/><br/>Votre compte a bien été créé, gardez précieusement vos 
		identifiants : <br/><ul><li>Nom d'utilisateur : ".$this->login."</li><li>Votre mot de passe : ".$this->pwd."</li></ul><br/>Pour activer votre compte, rendez 
		vous à cette adresse : ";
		$activation_key=generateKey();
		$mail_message .= "<a href='http://www.lambda-comm.fr/validation.php?login=".$this->login."&key=".$activation_key."'>http://www.lambda-comm.fr/validation.php?login=".$this->login."&key=".$activation_key."</a><br/><br/>A très bientôt sur le 
		site de Lambda Comm.";
		$mail_message2="Un nouveau client s'est inscrit : ".$this->prenom." ".$this->nom;
		$ip = $_SERVER["REMOTE_ADDR"];
		$subject = 'Inscription Lambda Comm.';
		$mail_header = "";
		$mail_header .= "MIME-Version: 1.0\r\n"; 
		$mail_header .= "X-Sender-IP: $ip\r\n";
		$mail_header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		$mail_header .= "From: Lambda Comm. <contact@lambda-comm.fr>";
		$admin="contact@lambda-comm.fr";
		include('connexion.inc.php');
		mysql_select_db("db235149319");
		$query="INSERT INTO identification VALUES('','{$this->nom}','{$this->prenom}','{$this->login}','user','{$this->pwd}','{$this->email}','false','{$activation_key}','{$this->struc}','{$this->cp}','{$this->tel}','{$this->ville}')";
		if(mysql_query($query)) {
			if (mail($this->email, $subject, $mail_message, $mail_header)) {
				mail($admin, $subject, $mail_message2, $mail_header);
				$html=<<<EOT
			<fieldset>
				<legend>Inscription terminée</legend>
				Merci {$this->prenom} {$this->nom},<br/>
				Votre compte a été créé avec succés. Vous de vez cependant l'activer grâce au lien de validation qui vient de vous être envoyé par 
				E-mail à cette adresse : <strong>{$this->email}</strong>.
			</fieldset>
EOT;
			}
		}
		return $html;
	}
}
?>


Mon fichier appelant la classe (forms.php) :


<?php
	require_once("fmanager.php");
	if($_POST){
		$t= $_POST;
	}
	else{ 
		$t= array("prenom"=>null, "nom"=>null, "login"=>null, "pwd"=>null, "pwd2"=>null, "email"=>null, "struc"=>null, "cp"=>null, "ville"=>null, "tel"=>null);
	}
	$F= new FormManager($t);
	if(sizeof($_POST)== 0){
		$c= $F->formulaire();
	}
	elseif(!$F->verifier()){
	$c= "<h2>Completez le formulaire comme indique</h2>";
	$c.= $F->formulaire();
	}
	else{
		$c= $F->afficher();
	}
	require_once("forms.html.php");
?>


Mon fichier d'affichage (forms.html.php) :


<html>
	<head>
		<title>Gestion dun formulaire</title>
	</head>
	<body>
		<h1>Formulaire d'inscription</h1>
		<?php
			echo $c;
		?>
	</body>
</html>


Voilà, désolé de vous poster ça comme ça mais je ne vois pas d'où l'erreur peut venir... A mon avis, cela doit être une erreur toute bête en plus Smiley langue . Aidez moi par pitié
Ton constructeur... ne construit rien ^^

Enfin si, l'objet est bien construit mais tes affectation de valeur ne sont pas faites.
public function __construct($tab)
{
	if(is_array($tab))
	{
		$nom = (...)
		$prenom = (...)
		$login = (...)
		(...)
	}
}

Tu affectes tes valeurs à des variables locales qui disparaissent à la fin de la construction. Pour affecter des valeurs à des variables de classes, il faut utiliser la syntaxte particulière $this.
public function __construct($tab)
{
	if(is_array($tab))
	{
		$this->nom = (...)
		$this->prenom = (...)
		$this->login = (...)
		(...)
	}
}

http://fr2.php.net/manual/en/language.oop5.basic.php
Ok, merci ça marche impec.

Seulement mon formulaire ne passe jamais dans l'état correct où il doit envoyer les données. on dirait qu'il reste tjs une erreur bien qu'il soit rempli correctement.

Aussi, ma vérification de login et de mail ne marche pas...

Quelqu'un sait d'où cela vient ? Smiley sweatdrop

Merci pour tout
Bonjour,

il y a un problème qu niveau de verifLogin();



	public function verifLogin() {
		include('connexion.inc.php');
		mysql_select_db("db235149319");
		$query="SELECT login FROM identification WHERE login='{$this->login}'";
		if(!mysql_query($query)) {
			return false;
		}
		else return true;
	}



Il faut savoir que la valeur de retour de la fonction mysql_query() (en cas de succès) est une resource, lorsque la requette est de type SELECT. Et non pas un booléen !

Je te propose de faire ceci :




	public function verifLogin() {
		include('connexion.inc.php');
		mysql_select_db("db235149319");
		$response = mysql_query("SELECT login FROM identification");
                $loginArray = mysql_fetch_array($response);
                foreach ( $loginArray as $value ) {
                         if ($this->login == $value)  return true;
		}
		return false;
	}