Skip to content

Instantly share code, notes, and snippets.

@Florencelg
Last active September 18, 2018 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Florencelg/d36ce53a874e2ca3dae94b4de8a36e2a to your computer and use it in GitHub Desktop.
Save Florencelg/d36ce53a874e2ca3dae94b4de8a36e2a to your computer and use it in GitHub Desktop.
Les formulaires en PHP
<?php
if($_POST)
{
//not empty
//atleast 6 characters long
$errors = array();
//start validation
if(empty($_POST['firstName']))
{
$errors['firstName1'] = "Your first name cannot ba empty";
}
if(strlen($_POST['firstName'])<2)
{
$errors['firstName2']="Your first name must be atleast 2 characters long";
}
if(empty($_POST['lastName']))
{
$errors['lastName1'] = "Your last name cannot be empty";
}
if(strlen($_POST['lastName'])<2)
{
$errors['lastName2']="Last name must be atleast 2 characters long";
}
if(empty($_POST['email']))
{
$errors['email1'] = "Email cannot be empty";
}
if(strlen($_POST['email'])<8)
{
$errors['email2']="Email must be atleast 8 characters long";
}
if(empty($_POST['tel'])){
$errors['tel1'] = "tel cannot be empty";
}
if(strlen($_POST['tel'])<10)
{
$errors['tel2']="tel must be atleast 10 characters long";
}
//check errors
if(count($errors)==0)
{
//redirect to success pages
echo "Success, Well done!";
exit();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>formulaire</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="main.js"></script>
</head>
<body>
<form method="POST" enctype="multipart/form-data">
<p>
<label for="firstName">First name</label>
<input type="text" id="firstName" name="firstName" pattern="[A-Za-z]{,}"value="<?php if (isset($_POST['firstName']))echo $_POST['firstName'];?>" />
</p>
<p><?php if (isset($errors['firstName1'])) echo $errors['firstName1'];?></p>
<p><?php if (isset($errors['firstName2'])) echo $errors['firstName2'];?></p>
<p>
<label for="lastName">Last name</label>
<input type="text" id="lasName" name="lastName" pattern="[A-Za-z]{,}" value="<?php if (isset($_POST['lastName']))echo $_POST['lastName'];?>" required/>
</p>
<p><?php if (isset($errors['lastName1'])) echo $errors['lastName1'];?></p>
<p><?php if (isset($errors['lastName2'])) echo $errors['lastName2'];?></p>
<p>
<label for="email">E-mail</label>
<input type="email" id="email" name="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{,}$" value="<?php if (isset($_POST['email']))echo $_POST['email'];?>" required />
</p>
<p><?php if (isset($errors['email1'])) echo $errors['email1'];?></p>
<p><?php if (isset($errors['email2'])) echo $errors['email2'];?></p>
<p>
<label for="tel">user_tel</label>
<input type="tel" id="tel" name="tel" pattern="^\+?\s*(\d+\s?){,}$" value="<?php if (isset($_POST['tel']))echo $_POST['tel'];?>" required />
</p>
<p><?php if (isset($errors['tel1'])) echo $errors['tel1'];?></p>
<p><?php if (isset($errors['tel2'])) echo $errors['tel2'];?></p>
<p>
<select name="subject">
<option>Subject1</option>
<option>Subject2</option>
<option>Subject3</option>
<option>Subject4</option>
<option>Subject5</option>
</select>
</p>
<p>
<label for="msg">User_message</label>
<textarea id="msg" name="message" rows="3" cols="25"required>User_message</textarea>
</p>
<p><?php if (isset($errors['msg1'])) echo $errors['msg1'];?></p>
<p>
<input type="submit" value="Submit"/>
</p>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment