/>
Copyright © vietjack.com
Ví dụ Complete Form trong PHP
Ví dụ dưới đây sẽ nhận input field dạng text, radio button, dropdown menu, và checked box.
Ví dụ
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $class = $course = $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
{
Trang chia sẻ các bài học online miễn phí
Page 1
/>
Copyright © vietjack.com
$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["course"])) {
$course = "";
}
else
{
$course = test_input($_POST["course"]);
}
if (empty($_POST["class"])) {
$class = "";
}
else
{
$class = test_input($_POST["class"]);
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
}
else
{
$gender = test_input($_POST["gender"]);
}
if (empty($_POST["subject"])) {
Trang chia sẻ các bài học online miễn phí
Page 2
/>
Copyright © vietjack.com
$subjectErr = "You must select 1 or more";
}
else
{
$subject = $_POST["subject"];
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
Absolute classes registration
<span class="error">* required field.</span>
Page 3
/>
Copyright © vietjack.com
</td>
</tr>
<tr>
<td>Time:</td>
<td> <input type="text" name="course">
<span class="error"><?php echo $websiteErr;?></span>
</td>
</tr>
<tr>
<td>Classes:</td>
<td> <textarea name="class" rows="5" cols="40"></textarea></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>Select:</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>
Trang chia sẻ các bài học online miễn phí
Page 4
/>
Copyright © vietjack.com
<option value="VB script">VB script</option>
</select>
</td>
</tr>
<tr>
<td>Agree</td>
<td><input type="checkbox" name="checked" value="1"></td>
<?php if(!isset($_POST['checked'])){ ?>
<span class="error">* <?php echo "You must agree to terms";?></span>
<?php } ?>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="Submit">
</td>
</tr>
</table>
</form>
echo "
Your given values are as :
";
echo ("
Your name is $name
");
echo ("
your email address is $email
");
echo ("
Your class time at $course
");
echo ("
your class info $class
");
echo ("
your gender is $gender
");
for($i=0; $i < count($subject); $i++)
{
echo($subject[$i] . " ");
Trang chia sẻ các bài học online miễn phí
Page 5
/>
Copyright © vietjack.com
}
?>
</body>
</html>
Nó sẽ cho kết quả:
Absolute classes registration
* required field.
* You must agree to terms
Name:
*
E-mail:
*
Time:
Classes:
Gender:
Female
Male *
A n d r o id
Select:
Ja v a
C#
D a ta B a s e
Agree
S u b m it
Your given values are as :
Your name is
Trang chia sẻ các bài học online miễn phí
Page 6
/>
Copyright © vietjack.com
your email address is
Your class time at
your class info
your gender is
Trang chia sẻ các bài học online miễn phí
Page 7