PHP-Make sure to enter values in the form
After the user clicks the submit button, in order to ensure that the field value is entered correctly, we insert a PHP script into the input element of HTML, and each field is named: name, email, and website. In the textarea field in the remarks, we put the script between the <textarea> and </textarea> tags.
The output values of the PHP script are: $name, $email, $website, and $comment variables.
Then, we also need to check the selected radio button. For this, we must set the checked attribute (not the value attribute of the radio button):
<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 example
The following is the complete PHP form validation example code:
Instance
The execution result in the example is similar to the following figure: