Created
March 20, 2024 16:55
-
-
Save Rizaaal/cb0a06a761fc3e81d267deb8731f7053 to your computer and use it in GitHub Desktop.
php sample form with validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>form</title> | |
</head> | |
<body> | |
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST"> | |
<h1>Fill the form below</h1> | |
<p>Complete the blow form to get instant access.</p> | |
<input type="text" name="Company_name" placeholder="Company name"> | |
<input type="text" name="full_name" placeholder="Full name"> | |
<input type="text" name="email" placeholder="Email"> | |
<input type="text" name="phone" placeholder="Phone"> | |
<select name="service"> | |
<option value="none">Choose service...</option> | |
<option value="delivery">delivery</option> | |
<option value="pick up">pick up</option> | |
<option value="digital delivery">digital delivery</option> | |
</select> | |
<input type="submit" value="Send request"> | |
</form> | |
<?php | |
if ($_SERVER["REQUEST_METHOD"] === "POST"){ | |
$passed = true; | |
if($_POST["Company_name"] === "") { | |
echo "company name required" . "<br>"; | |
$passed = false; | |
} | |
if($_POST["full_name"] === "") { | |
echo "full name required" . "<br>"; | |
$passed = false; | |
} | |
if($_POST["email"] === "") { | |
echo "email required" . "<br>"; | |
$passed = false; | |
} | |
if($_POST["phone"] === "") { | |
echo "phone required" . "<br>"; | |
$passed = false; | |
} | |
if($_POST["service"] === "none") { | |
echo "please select a service <br>"; | |
$passed = false; | |
} | |
//validazione numero telefono | |
if ($_POST["phone"] !== "" && !preg_match("/^\+?[\d\s()-]*[\d]{3}[-\.\s()]*[\d]{3}[-\.\s()]*[\d]{4}$/", $_POST["phone"])) { | |
echo "write a valid phone number. the following formats are available: <br> | |
* ###-###-#### <br> | |
* (###) ###-#### <br> | |
* ###.###.#### <br> | |
* ### ### #### <br> | |
* +-###-###-#### <br> | |
* +(###) ###-#### <br> | |
* +###.###.#### <br> | |
* +### ### #### <br>"; | |
$passed = false; | |
} | |
//validazione mail | |
if ($_POST["email"] !== "" && !filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) { | |
echo "Invalid email format <br>"; | |
$passed = false; | |
} | |
}; | |
?> | |
<script> | |
if (<?php echo $passed ?>){ | |
document.body.innerHTML = "form sent successfully!"; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment