Latest web development tutorials

PHP complete form instance

This chapter explains how to allow users to click on "Submit (submit)" button to submit the data before to ensure that all the fields correctly entered.


PHP - be sure to enter a value in the form

The user clicks the submit button, to ensure that the field values ​​are entered correctly, we add the PHP script in the HTML input element inserted in each field named: name, email, and website. In remarks in the textarea field, we will put in the script between the <textarea> and </ textarea> tag.

PHP script output value: $ name, $ email, $ website, and $ comment variable.

Then, we also need to check the radio button is selected, for this, we must set checked property (not the radio button's value property):

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
   名字: <input type="text" name="name" value="<?php echo $name;?>">
   <span class="error">* <?php echo $nameErr;?></span>
   <br><br>
   E-mail: <input type="text" name="email" value="<?php echo $email;?>">
   <span class="error">* <?php echo $emailErr;?></span>
   <br><br>
   网址: <input type="text" name="website" value="<?php echo $website;?>">
   <span class="error"><?php echo $websiteErr;?></span>
   <br><br>
   备注: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
   <br><br>
   性别:
   <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?>  value="female">女
   <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?>  value="male">男
   <span class="error">* <?php echo $genderErr;?></span>
   <br><br>
   <input type="submit" name="submit" value="Submit"> 
</form>

PHP - complete form instance

Here is the complete PHP form validation code examples:

Results of a similar example is shown below: