Formulaire PHP avec test champs vides

Fermé
boss183 Messages postés 181 Date d'inscription lundi 7 janvier 2008 Statut Membre Dernière intervention 7 octobre 2011 - 25 mai 2011 à 14:16
tompsy Messages postés 251 Date d'inscription mercredi 5 décembre 2007 Statut Membre Dernière intervention 19 décembre 2011 - 29 mai 2011 à 19:09
Bonjour à tous,

alors je vous explique mon problème, j'avais réalisé un formulaire PHP qui testait les champs vides, ce formulaire était composé de deux scripts (un pour le formulaire et le "fichier action") ça marchait très bien sauf que s'il y avait un champ vide le fichier action indiquait l'erreur ainsi qu'un lien pour revenir au formulaire mais l'utilisateur perdait alors ce qu'il avait déjà tapé.
On m'a parlé de session PHP mais j'ai pas trop réussi à mettre en place le truc, j'ai donc décidé de réaliser ce formulaire avec un seul fichier mais j'ai l'erreur suivante :

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test_ajout.php:1) in C:\xampp\htdocs\test_ajout.php on line 50

Voilà ci-dessous mon code en entier :

<?php
// Paramètres pour accéder à ta base MysQl
$host = "localhost";
$user = "root";
$password = "";
$bdd = "application";

// Connection à la Base MySql
mysql_connect($host, $user, $password) or die ("impossible de se connecter au serveur" );
mysql_select_db($bdd) or die ("impossible de se connecter a la base de donnees" );

// Déclaration des champs (vides ou récupération des $_POST)...

$title = (isset($_POST['title'])) ? htmlspecialchars($_POST['title']) : '';
$logiciel = (isset($_POST['prenom'])) ? htmlspecialchars($_POST['logiciel']) : '';
$version = (isset($_POST['version'])) ? htmlspecialchars($_POST['version']) : '';
$link = (isset($_POST['link'])) ? htmlspecialchars($_POST['link']) : '';
$logiciel = (isset($_POST['description'])) ? htmlspecialchars($_POST['description']) : '';

// Si le formulaire a été envoyé, on teste les champs...

$erreur = false;

if (!empty($_POST)) { 

	// vérification dans l'ordre inverse pour que le dernier soit le premier  smile 

	if (empty($title)) {

		$erreur = true;

		$errmsg = 'Merci de saisir le titre !';

	}
	
	if (empty($logiciel)) {

		$erreur = true;

		$errmsg = 'Merci de saisir le logiciel/matériel !';

	}

		// Si tout est OK on exécute l'action (INSERT dans la base, envoi d'email, etc...)

	if (!$erreur) {

		mysql_query("INSERT INTO infolog (title,logiciel,version,link,description) VALUES('$title','$logiciel','$version','$link','$description')");

		header("Location: ajout_info.php");

		exit;

	}

}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
  <head>
       <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Ajouter nouvelle info</title>
    <link href="CSS/style.css" rel="stylesheet" type="text/css" media="screen" />
	<style type="text/css">

.alert {

	color:#990000;

}

</style>
  </head>

<BODY background="metal.jpg" style="background-repeat:no-repeat; background-attachment:fixed">
<?php

if ($erreur) {
	echo '<p class="alert">'.$errmsg.'</p>';
}

?>
<br>
<ul id="menu_dynamique">

    <li class="extend">Informations<ul>
	<li><a href="traitement.php">Infos Logiciels</a></li>
	<li class="sans"><a href="traitementmat.php">Infos Matériels</a></li>
	<li><a href="traitementcontrat.php">Infos Contrats</a></li>
	</ul>
	</li>
	
	<li class="extend none"><a href="http://192.168.66.110/ouapi1.0">Gérer le parc Informatique</a></li>
		
	<li class="extend">Gestion des infos<ul>
		
		<li><a href="nouvelle_info.html">Ajouter une info</a></li>
		<li class="sans"><a href="modifinfo.php">Modifier/Supprimer info</a></li>
	    <li><a href="removerss.php">Supprimer document</a></li>
		</ul>
		
	</li>
		
	<li class="extend none">Gestion des flux RSS
		
		<ul>
		
		<li><a href="actu_flux.php">Actualiser les flux</a></li>
		<li class="sans"><a href="nouv_flux.html">Ajouter un flux</a></li>
	    <li><a href="removeflux.php">Supprimer un flux</a></li>
				
		</ul>
		
	</li>
	
	<li class="extend"><a href="https://www.cheops.fr/fr/division-reseau-securite-et-communications-unifiees" title="Lien vers le site d'Ocealis Sécurité">Ocealis Sécurité</a></li>
	
</ul>
<br><br><br><br>
<table align="center" width="800" height="390" border="0" cellpadding="0" cellspacing="0">
   <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
      <tr>
        <!--<td width="6%">&nbsp;</td>-->
        <td colspan="5"><P ALIGN='center'><FONT SIZE=5 COLOR='#377693'><span style="font-variant: small-caps;"><B>Ajouter une nouvelle Information</B></SPAN></FONT><BR><BR><BR></P></td>
     </tr>
	
	  <tr>
		<td><div><font size=4 COLOR='#377693'><span style="font-variant: small-caps;"><B><label for="title">Titre *</label></B></SPAN></font></div></td>
	  </tr>
	<tr>
      <td colspan="35"><input type="text" name="title" id="title" size="76" maxlength="500" value="<?php echo stripslashes($title); ?>" /></td>
	</tr>

<tr>
      <td><div><font size=4 COLOR='#377693'><span style="font-variant: small-caps;"><B><label for="logiciel">Logiciel / Matériel *</label></B></SPAN></font></div></td></tr>
	  <tr>
      <td colspan="5"><input type="text" name="logiciel" id="logiciel" size="50" maxlength="50" value="<?php echo stripslashes($logiciel); ?>" /></td>
</tr>

<tr>
      <td><div><font size=4 COLOR='#377693'><span style="font-variant: small-caps;"><B><label for="version">Version</label></B></SPAN></font></div></td>
	  </tr>
<tr>
      <td colspan="5"><input type="text" name="version" id="version" size="50" maxlength="50" value="<?php echo stripslashes($version); ?>" /></td>
</tr>

<tr>
<td><div><font size=4 COLOR='#377693'><span style="font-variant: small-caps;"><B><label for="link">Lien *</label></B></SPAN></font></div></td></tr>
<tr>
	<td colspan="5"><input type="text" name="link" id="link" size="50" maxlength="50" value="<?php echo stripslashes($link); ?>" /></td>
</tr>

<tr>       
      <td><BR><div><font size=4 COLOR='#377693'><B><span style="font-variant: small-caps;"><label for="description">Description *</label></SPAN></B></font></div></td>
	  </tr>
	  <tr>
      <td colspan='5'><textarea rows="5" cols="76" name="description" id="description" value="<?php echo stripslashes($description); ?>"></textarea></td>
</tr>

<tr>
  <td height="24"></td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td height="14"></td>
  <td width="16%"><center><input type="reset" name="Submit2" value="Effacer"></center></td>
  <td width="28%"><left><input type="submit" name="Submit" value="Enregistrer l'info"></left></td>
</tr>
<tr>                
<td height="20"></td>
     <td></td>
     <td></td>
</tr>
</form>
</table></td></tr>
</table>
</div>
<BR><BR><BR>
<P align="right"><FONT COLOR='#377693' STYLE="font-family:trebuchet MS;">* indique un champ requis</FONT></P>
</body>
</html>


Merci d'avance pour votre aide !
A voir également:

3 réponses

tompsy Messages postés 251 Date d'inscription mercredi 5 décembre 2007 Statut Membre Dernière intervention 19 décembre 2011 49
Modifié par tompsy le 29/05/2011 à 19:09
l'erreur se situ ici :

$title = (isset($_POST['title'])) ? htmlspecialchars($_POST['title']) : ''; 
$logiciel = (isset($_POST['prenom'])) ? htmlspecialchars($_POST['logiciel']) : ''; 
$version = (isset($_POST['version'])) ? htmlspecialchars($_POST['version']) : ''; 
$link = (isset($_POST['link'])) ? htmlspecialchars($_POST['link']) : ''; 
$logiciel = (isset($_POST['description'])) ? htmlspecialchars($_POST['description']) : '';


Tu a 2 fois $logiciel et il va donc récupérer "description"
1
tompsy Messages postés 251 Date d'inscription mercredi 5 décembre 2007 Statut Membre Dernière intervention 19 décembre 2011 49
25 mai 2011 à 15:10
Salut,

peux tu vérifier que rien n'est affiché avant la fonction HEADER ?

-> https://www.php.net/manual/fr/function.header.php <-

Ou il est noté :
N'oubliez jamais que header() doit être appelée avant que le moindre contenu ne soit envoyé, soit par des lignes HTML habituelles dans le fichier, soit par des affichages PHP...
0
boss183 Messages postés 181 Date d'inscription lundi 7 janvier 2008 Statut Membre Dernière intervention 7 octobre 2011 17
25 mai 2011 à 15:35
bah je pense que ça peut marcher avec la fonction header et je ne pense pas que mon problème vienne de là : je remplis tous les champs, et lorsque je soumet le formulaire il me met le message d'erreur comme quoi je n'ai pas rempli le champ desciption (j'ai rajouté des cas d'erreurs) et plus surprenant; ce que j'avais tapé dans description passe dans le champ logiciel.
0