PHP 5 Complete Form Example

This section will show you how to make sure that all fields are entered correctly before clicking the "submit" button to submit data.

PHP-make sure to enter values in the form

After the user clicks the submit button, to ensure that the field value is entered correctly, we insert a PHP script in the HTML input element, The fields are named: name, email, and website. In the textarea field in the note, we put the script in <textarea> with </textarea> Between tags.

PHP script output values: $ name, $ email, $ website, and $ comment variables。 

Then we also need to check the selected radio button, For this, we The checked attribute must be set (not the value attribute of the radio button):

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
   first name: <input type="text" name="name" value="<?php echo $name;?>">
   <span class="error">* <?php echo $nameErr;?></span>
   E-mail: <input type="text" name="email" value="<?php echo $email;?>">
   <span class="error">* <?php echo $emailErr;?></span>
   URL: <input type="text" name="website" value="<?php echo $website;?>">
   <span class="error"><?php echo $websiteErr;?></span>
   Note: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
   <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?>  value="female">Female
   <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?>  value="male">male
   <span class="error">* <?php echo $genderErr;?></span>
   <input type="submit" name="submit" value="Submit"> 

PHP-full form example

The following is the complete PHP form validation example code: