PHP Form Required

The PHP form required field is used in many web pages. When, user doesn’t enter values in the text box or text area or any other field, then error message display on the webpage.

How PHP Form Required Field Work?

The PHP form required field is very useful for all Web Pages. Whenever any user entered value in a webpage like login, registration, search product, change password etc. Many times, some text box remains empty, and then form required field display an error message on a webpage.

In the below example we used a text box for student name and student email id and text area to display an address of the student. Students don’t enter any value in a text box or text area, and then required field error message display on a webpage.

Step 1: PHP Code

<?php

// variables are defined………

$stu_name=" ";
$stu_name_display=" ";
$stu_email=" ";
$stu_address=" ";
$stu_email_display=" ";
$stu_address_display=" ";

// when click on the submit button

if(isset($_REQUEST["btn_submit"]))
{
// check student name is entered or not(empty)....
if (empty($_POST["txt_name"]))
{
$stu_name = "Please enter Name...";
}
else
{
$stu_name_display =$_POST["txt_name"];
}
// check student_email id is entered or not….
if (empty($_POST["txt_email"]))
{
$stu_email = "Please Enter Email Id..";
}
else
{
$stu_email_display =$_POST["txt_email"];
}
// check student address is entered or not….
if (empty($_POST["txt_address"]))
{
$stu_address = "Please Enter Address...";
}
else
{
$stu_address_display =$_POST["txt_address"];
}
}

?>

Step 2: Design Page

<body>
<form method="post" action=" ">

<b> PHP FORM REQUIRED </b>

<table border="2">
<tr>
<td><table border="0">
<tr>
<td> Student Name </td>
<td><label>
<input type="text" name="txt_name">
</label></td>
<td> <?php echo $stu_name ; ?> </td>
</tr>
<tr>
<td> Student Email Id </td>
<td><label>
<input type="text" name="txt_email">
</label></td>
<td> <?php echo $stu_email ; ?> </td>
</tr>
<tr>
<td>Student Address </td>
<td>
<textarea name="txt_address"></textarea>
</td>
<td> <?php echo $stu_address ; ?> </td>
</tr>
<tr>
<td><label>
<input type="submit" name="submit" value="Submit">
</label></td>
</tr>
</table> </td>
</tr>
</table>
</form>
</body>

Result:
php-form-required




Leave a Reply

avatar
  Subscribe  
Notify of