Champ obligatoire php

Résolu/Fermé
Miimidedel Messages postés 345 Date d'inscription mercredi 4 août 2010 Statut Membre Dernière intervention 5 juin 2019 - 13 avril 2011 à 16:51
wolves'sbrother Messages postés 218 Date d'inscription samedi 7 février 2009 Statut Membre Dernière intervention 13 juillet 2011 - 14 avril 2011 à 15:11
Bonjour,

Voilà je débute en phph et je fais un petit forum, juste un mini-ptojet, mais j'ai un souci.. J'aimerais par exemple que le champ Username soit obligatoire
J'ai trouvé une solution en php mais je comprends pas pourquoi ça ne marche pas :

-------------------------------------------------------------------------------------------
<?php
$username = $_POST['username'];
$password = $_POST['password'];
if(empty($username))
{
print("<center>Le '<b>Name</b>' est vide !</center>");
exit();
}
?>

-------------------------------------------------------------------------------------------

Et aussi.. Question : où dois je positionner dans le code html ?

-------------------------------------------------------------------------------------------

<!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="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Login Page</title>
<link href="design.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="blabla.png" type="image/x-icon"/>
<link rel="icon" href="blabla.png" type="image/x-icon"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"</script>

</head>
<body>
<div id="header">

<h1>SupForum</h1>

</div> <div id="menu">
<ul>
<li><a href="index.php">Categories</a></li>
<li><a href="login.php">Topics</a></li>
<li><a href="login.php" class="right">Login</a></li>
<li><a href="index.php" class="right">Register</a></li>
</ul>

</div> <div id="content">

<h1>Register Page</h1>


<form method="post" action="page.php" enctype="multipart/form-data">

<fieldset>
<legend> Identification Informations : </legend>
<div>
<label for="username">Username: </label>
<input id="username" type="text" name="username" value="" />
<span style="color:red" ></span>
<!--<span class="info"></span>-->

</div>
<div>
<label for="password">Password:</label>
<input id="password" type="password" name="password" value="" />
</div>
<div>
<label for="password">Password Confirmation:</label>
<input id="passwordconf" type="password" name="passwordconf" value="" />
</div>
</fieldset>
<fieldset>
<legend> Personal Informations : </legend>
<div>
<label for="firstname">Firstname: </label>
<input id="firstname" type="text" name="firstname" value="" />
</div>
<div>
<label for="lastname">Lastname:</label>
<input id="lastname" type="text" name="lastname" value="" />
</div>
<label for="email">Email: </label>
<input id="email" type="text" name="email" value="" />
</fieldset>
<div>
<label for="avatar"> Avatar:</label>
<input id="avatar" type="file" name="nom" />
<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
</div>
<div>
<input type="submit" value="Submit" />

</div>
</div>

</form>

</div>
</body>
</html>

-------------------------------------------------------------------------------------------

Merci ! =D

A voir également:

5 réponses

wolves'sbrother Messages postés 218 Date d'inscription samedi 7 février 2009 Statut Membre Dernière intervention 13 juillet 2011 34
13 avril 2011 à 16:59
Bonjour,
Est que tu veux empêcher l'utilisateur de valider le formulaire tant qu'il n'a pas remplie le champ username, ou tu veux le rediriger vers le formulaire après validation s'il n'a pas remplie le champs username??
0
Miimidedel Messages postés 345 Date d'inscription mercredi 4 août 2010 Statut Membre Dernière intervention 5 juin 2019 71
13 avril 2011 à 17:12
Je souhaite le rediriger vers le formulaire après validation avec des champs manquants.
J'aimerais aussi qu'il s'affiche côté du champ username "This field cannot be empty !"
0
wolves'sbrother Messages postés 218 Date d'inscription samedi 7 février 2009 Statut Membre Dernière intervention 13 juillet 2011 34
14 avril 2011 à 10:42
ok.
J'ai ajouter une condition à la ligne 30 :
if(!isset($_POST['username'])  || empty($_POST['username']) ) 
{?>
        // tu mets le code de ton formulaire ici
<?php 
} ?>

Cette condition affiche le formulaire si la variable $_POST['username'] n'existe pas ou si elle est vide.
Et une autre à la ligne 40 :
if(isset($_POST['username']) && empty($_POST['username'])){echo "This field cannot be empty !";}

Cette condition affiche le msg "This field....." si la variable $_POST['username'] existe et vide

voici le code complet, si tu as besoin d'autre chose, n'hésite pas.

<!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="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Login Page</title>
<link href="design.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="blabla.png" type="image/x-icon"/>
<link rel="icon" href="blabla.png" type="image/x-icon"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"</script>

</head>
<body>
<div id="header">

<h1>SupForum</h1>

</div> <div id="menu">
<ul>
<li><a href="index.php">Categories</a></li>
<li><a href="login.php">Topics</a></li>
<li><a href="login.php" class="right">Login</a></li>
<li><a href="index.php" class="right">Register</a></li>
</ul>

</div> <div id="content">

<h1>Register Page</h1>
<?php
if(!isset($_POST['username'])  || empty($_POST['username']) ) 
{
?>
<form method="post" action="test.php" enctype="multipart/form-data">

<fieldset>
<legend> Identification Informations : </legend>
<div>
<label for="username">Username: </label>
<input id="username" type="text" name="username" value="" />
<span style="color:red" ><?php if(isset($_POST['username']) && empty($_POST['username'])){echo "This field cannot be empty !";} ?></span>
<!--<span class="info"></span>-->

</div>
<div>
<label for="password">Password:</label>
<input id="password" type="password" name="password" value="" />
</div>
<div>
<label for="password">Password Confirmation:</label>
<input id="passwordconf" type="password" name="passwordconf" value="" />
</div>
</fieldset>
<fieldset>
<legend> Personal Informations : </legend>
<div>
<label for="firstname">Firstname: </label>
<input id="firstname" type="text" name="firstname" value="" />
</div>
<div>
<label for="lastname">Lastname:</label>
<input id="lastname" type="text" name="lastname" value="" />
</div>
<label for="email">Email: </label>
<input id="email" type="text" name="email" value="" />
</fieldset>
<div>
<label for="avatar"> Avatar:</label>
<input id="avatar" type="file" name="nom" />
<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
</div>
<div>
<input type="submit" value="Submit" />

</div>
</div>

</form>
<?php }?>
</div>
</body>
</html> 
0
mpmp93 Messages postés 6652 Date d'inscription mercredi 13 avril 2011 Statut Membre Dernière intervention 28 septembre 2015 1 339
14 avril 2011 à 10:56
Une réponse dans le code qui suit. Note: je l'ai réécrit compatible HTML5:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Login Page</title>
<link href="design.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="blabla.png" type="image/x-icon"/>
<link rel="icon" href="blabla.png" type="image/x-icon"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"</script>
</head>
<body>
<header>
<h1>SupForum</h1>
</header>
<nav>
<ul>
<li><a href="index.php">Categories</a></li>
<li><a href="login.php">Topics</a></li>
<li><a href="login.php" class="right">Login</a></li>
<li><a href="index.php" class="right">Register</a></li>
</ul>
</nav>

<article>

<h1>Register Page</h1>

<form method="post" action="page.php" enctype="multipart/form-data">

<fieldset>
<legend> Identification Informations : </legend>
<p><label for="username">Username: </label>
<input id="username" type="text" name="username" value="" />
<?php if(isset($_POST['username']) && empty($_POST['username'])): ?>
<span style="color:red; text-align: center;" >Le <b>Name</b> est vide !</span>
<?php endif; ?></p>
<p><label for="password">Password:</label>
<input id="password" type="password" name="password" value="" /></p>
<p><label for="password">Password Confirmation:</label>
<input id="passwordconf" type="password" name="passwordconf" value="" /></p>
</fieldset>
<fieldset>
<legend> Personal Informations : </legend>
<p><label for="firstname">Firstname: </label>
<input id="firstname" type="text" name="firstname" value="" /></p>
<p><label for="lastname">Lastname:</label>
<input id="lastname" type="text" name="lastname" value="" />
</p>
<p><label for="email">Email: </label>
<input id="email" type="text" name="email" value="" /></:/p>
</fieldset>
<p><label for="avatar"> Avatar:</label>
<input id="avatar" type="file" name="nom" />
<input type="hidden" name="MAX_FILE_SIZE" value="1048576" /></p>
<p><input type="submit" value="Submit" /></p>


</form>

</article>
</body>
</html>
0
Miimidedel Messages postés 345 Date d'inscription mercredi 4 août 2010 Statut Membre Dernière intervention 5 juin 2019 71
14 avril 2011 à 12:05
C'est super gentil en tout cas !
Je viens d'essayer le code.. Mais j'ai un souci.. Plus rien ne s'affiche sur a page SAUF le background =X...
0
Miimidedel Messages postés 345 Date d'inscription mercredi 4 août 2010 Statut Membre Dernière intervention 5 juin 2019 71
14 avril 2011 à 14:31
Ah toi tu me proposes une autre méthode ?
0
wolves'sbrother Messages postés 218 Date d'inscription samedi 7 février 2009 Statut Membre Dernière intervention 13 juillet 2011 34
14 avril 2011 à 14:18
c'est bizarre, je viens de l'essayer, ça marche!!
t'as rien de changer d'autre dans le code que t'as posté?
0
Miimidedel Messages postés 345 Date d'inscription mercredi 4 août 2010 Statut Membre Dernière intervention 5 juin 2019 71
14 avril 2011 à 14:31
Bah en théorie non.. J'ai même copié/collé tout ton code et rien =X..
Est ce que ce serait possible, si tu as du temps, de discuter via msn ou skype où je pourrai t'envoyer tout le dossier ?.. =X
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
wolves'sbrother Messages postés 218 Date d'inscription samedi 7 février 2009 Statut Membre Dernière intervention 13 juillet 2011 34
14 avril 2011 à 15:11
je t'ai envoyé un msg privé
0