×
×
PHP Tutorial
What is PHP Environment Setup of PHP PHP Syntax PHP Comments PHP Echo & print PHP Variables PHP Scope Variables PHP $ and $$ Variable PHP Constants PHP Operators PHP Data TypesPHP Conditional Statements
PHP Loop Type
PHP Arrays
PHP String PHP FunctionsPHP Form Examples
Form Intro Get & Post Form Validation / Required PHP Complete FormPHP Advanced
PHP Include / Require PHP Date & Time PHP Session PHP Cookies PHP File Handling PHP Open File PHP Read File PHP Write File PHP Append File PHP Delete File PHP File Upload PHP Sending Mail PHP Error HandlingPHP Complete Form
This page explains about time real-time form with actions. Below example will take input fields as text, radio button, drop down menu, and checked box.
Example
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $subjectErr = $descriptionErr = "";
$name = $email = $gender = $description = $subject = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
}else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
}else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
}else {
$gender = test_input($_POST["gender"]);
}
if (empty($_POST["subject"])) {
$subjectErr = "You must select 1 or more";
}else {
$subject = $_POST["subject"];
}
if (empty($_POST["description"])) {
$descriptionErr = "Description is required";
}else {
$description = test_input($_POST["description"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>Registration Form:</h2>
<form method = "POST" action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table>
<tr>
<td>Name:</td>
<td><input type = "text" name = "name">
<span class = "error">* <?php echo $nameErr;?></span>
</td>
</tr>
<tr>
<td>E-mail: </td>
<td><input type = "email" name = "email">
<span class = "error">* <?php echo $emailErr;?></span>
</td>
</tr>
<tr>
<td>Gender:</td>
<td>
<input type = "radio" name = "gender" value = "female">Female
<input type = "radio" name = "gender" value = "male">Male
<span class = "error">* <?php echo $genderErr;?></span>
</td>
</tr>
<tr>
<td>Subject:</td>
<td>
<select name = "subject[]" size = "4" multiple>
<option value = "Android">Android</option>
<option value = "Java">Java</option>
<option value = "C#">C#</option>
<option value = "Data Base">Data Base</option>
<option value = "Hadoop">Hadoop</option>
<option value = "VB script">VB script</option>
</select>
<span class = "error">* <?php echo $subjectErr;?></span>
</td>
</tr>
<tr>
<td>Description:</td>
<td> <textarea name = "description" rows = "5" cols = "40"></textarea>
<span class = "error">* <?php echo $descriptionErr;?></span>
</td>
</tr>
<tr>
<td>
<input type = "submit" name = "submit" value = "Submit">
</td>
</tr>
</table>
</form>
<?php
if($name != ''){
echo "<h2>Details :</h2>";
echo ("<p>Your name is <b> $name </b></p>");
echo ("<p> your email address is <b> $email </b></p>");
echo ("<p>your gender is <b> $gender </b></p>");
echo ("<p>your Info :<b> $description </b></p>");
echo ("<p>Subject :<b> ");
for($i = 0; $i < count($subject); $i++) {
echo($subject[$i] . " ");
}
echo ("</b></p>");
}
?>
</body>
</html>
Output:
Details :
Your name is Elevenstech
your email address is [email protected]
your gender is male
your Info : THis is testing
Subject : Java Data Base
Elevenstech Web Tutorials
Elevenstech Web Tutorials helps you learn coding skills and enhance your skills you want.
As part of Elevenstech's Blog, Elevenstech Web Tutorials contributes to our mission of “helping people learn coding online”.
Read More
Newsletter
Subscribe to get the latest updates from Elevenstech Web Tutorials and stay up to date
Copyright 2018 - 2024 Elevenstech Web Tutorials All rights reserved.