PHP Complete Form Example

The purpose of this article is to provide Php Complete Form example and use of HTML value attributes in PHP to set the values of input fields.

Pre-filling input fields with data from a database or another data source is often necessary when creating PHP forms.

Using this method, users will be able to save time and effort and use it in a more convenient and efficient manner to fill out forms.

By the end of this article, you’ll know how to pre-fill input fields in PHP forms so your users have a more enjoyable experience.



Store Values in PHP Forms

A small PHP script is added to the value attribute of each of the following input fields after the user hits the submit button.

The script shows the values in the input fields after the user enters the values in the firstname, lastname, age, email, designation, and linkedin.

A script will be added to the skills field by inserting it between the <textarea> and </textarea> tags.

As you can see from the output of the small script, the values of the variables $firstname, $lastname, $age, $email, $designation, $LinkedIn, and $skills are displayed.

The next thing we need to do is to show what select button has been selected.

This is something that we can achieve by manipulating the selected attribute (not the value attribute for select option buttons):

First name: <input type="text" name="firstname" value="<?php echo $firstname ?>"><span class="error"> * <?php echo $fnameErr; ?></span>

Last name: <input type="text" name="lastname" value="<?php echo $lastname ?>"><span class="error"></span>

Age: <input type="text" name="age" value="<?php echo $age ?>"><span class="error">* <?php echo $ageErr; ?></span>

E-mail: <input type="text" name="email" value="<?php echo $email ?>"><span class="error"> * <?php echo $emailErr; ?></span>

<label for="yrofexp">Year of Experience:</label>
<select name="yrofexp" id="yrofexp">
<option value="">Select number</option>
<option <?php if (isset($yrofexp) && $yrofexp == '1') echo "selected"; ?> value="1">1</option>
<option <?php if (isset($yrofexp) && $yrofexp == '2') echo "selected"; ?> value="2">2</option>
<option <?php if (isset($yrofexp) && $yrofexp == '3') echo "selected"; ?> value="3">3</option>
<option <?php if (isset($yrofexp) && $yrofexp == '4') echo "selected"; ?> value="4">4</option>
<option <?php if (isset($yrofexp) && $yrofexp == '5') echo "selected"; ?> value="5">5</option>
<option <?php if (isset($yrofexp) && $yrofexp == '6') echo "selected"; ?> value="6">6</option>
<option <?php if (isset($yrofexp) && $yrofexp == '7') echo "selected"; ?> value="7">7</option>
<option <?php if (isset($yrofexp) && $yrofexp == '8') echo "selected"; ?> value="8">8</option>
</select>

Designation: <input type="text" name="designation" value="<?php echo $designation ?>"><span class="error"> * <?php echo $designationErr; ?></span>

LinkedIn: <input type="text" name="linkedin" value="<?php echo $linkedin ?>"><span class="error"> * <?php echo $linkedInErr; ?></span>

Write your Skills: <textarea name="skills" rows="5" cols="40"><?php echo $skills ?></textarea><span class="error"> * <?php echo $skillsErr; ?></span>

PHP Complete Form Example

You can find the complete code for this PHP Form Validation Example here:

Example: 

<!DOCTYPE HTML> <html lang="en"><head> <title>PHP Form</title> <style> .error { color: #FF0000; } </style> </head><body><?php $fnameErr = $lnameErr = $ageErr = $emailErr = $designationErr = $skillsErr = $linkedInErr = ""; $firstname = $lastname = $age = $email = $designation = $skills = $linkedin = $yrofexp = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["firstname"])) { $fnameErr = "First name is required"; } else { $firstname = input_validation($_POST["firstname"]); if (!preg_match("/^[a-zA-Z-' ]*$/", $firstname)) { $fnameErr = "Only letters and white space allowed"; } }$lastname = input_validation($_POST["lastname"]);if (empty($_POST["age"])) { $ageErr = "Age is required"; } else { $age = input_validation($_POST["age"]); if (!preg_match("/^[0-9 ]*$/", $age)) { $ageErr = "Only numbers allowed"; } }if (empty($_POST["email"])) { $emailErr = "Email is required"; } else { $email = input_validation($_POST["email"]); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailErr = "Invalid email format"; } }if (empty($_POST["skills"])) { $skillsErr = "Skills is required"; } else { $skills = input_validation($_POST["skills"]); }if (empty($_POST["designation"])) { $designationErr = "Designation is required"; } else { $designation = input_validation($_POST["designation"]); if (!preg_match("/^[a-zA-Z-' ]*$/", $designation)) { $designationErr = "Only letters and white space allowed"; } } if (empty($_POST["linkedin"])) { $linkedInErr = "URL is required"; } else { $linkedin = input_validation($_POST["linkedin"]); if (!preg_match("/^(?:(?:https?|ftp):\\/\\/|www.)[-a-z0-9+&@#\\/%?=~_|!:,.;]*[-a-z0-9+&@#\\/%=~_|]/i", $linkedin)) { $linkedInErr = "URL is not valid"; } } $yrofexp = input_validation($_POST["yrofexp"]); }// define variables and set to empty valuesfunction input_validation($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?><h2>PHP Form Validation</h2> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"> <p><span class="error">* required field</span></p> First name: <input type="text" name="firstname" value="<?php echo $firstname ?>"><span class="error"> * <?php echo $fnameErr; ?></span> <br><br> Last name: <input type="text" name="lastname" value="<?php echo $lastname ?>"><span class="error"></span> <br><br> Age: <input type="text" name="age" value="<?php echo $age ?>"><span class="error">* <?php echo $ageErr; ?></span> <br><br> E-mail: <input type="text" name="email" value="<?php echo $email ?>"><span class="error"> * <?php echo $emailErr; ?></span> <br><br> <label for="yrofexp">Year of Experience:</label> <select name="yrofexp" id="yrofexp"> <option value="">Select number</option> <option <?php if (isset($yrofexp) & $yrofexp == '1') echo "selected"; ?> value="1">1</option> <option <?php if (isset($yrofexp) & $yrofexp == '2') echo "selected"; ?> value="2">2</option> <option <?php if (isset($yrofexp) & $yrofexp == '3') echo "selected"; ?> value="3">3</option> <option <?php if (isset($yrofexp) & $yrofexp == '4') echo "selected"; ?> value="4">4</option> <option <?php if (isset($yrofexp) & $yrofexp == '5') echo "selected"; ?> value="5">5</option> <option <?php if (isset($yrofexp) & $yrofexp == '6') echo "selected"; ?> value="6">6</option> <option <?php if (isset($yrofexp) & $yrofexp == '7') echo "selected"; ?> value="7">7</option> <option <?php if (isset($yrofexp) & $yrofexp == '8') echo "selected"; ?> value="8">8</option> </select> <br><br> Designation: <input type="text" name="designation" value="<?php echo $designation ?>"><span class="error"> * <?php echo $designationErr; ?></span> <br><br> LinkedIn: <input type="text" name="linkedin" value="<?php echo $linkedin ?>"><span class="error"> * <?php echo $linkedInErr; ?></span> <br><br> Write your Skills: <textarea name="skills" rows="5" cols="40"><?php echo $skills ?></textarea><span class="error"> * <?php echo $skillsErr; ?></span> <br> <br><br> <input type="submit" name="submit" value="Submit"></form><?php echo "<h2>Your Form Data:</h2>"; echo "First Name: ".$firstname; echo "<br>"; echo "Last Name: ".$lastname; echo "<br>"; echo "Age: ".$age; echo "<br>"; echo "Email: ".$email; echo "<br>"; echo "Year of Experience: ".$yrofexp; echo "<br>"; echo "Designation: ".$designation; echo "<br>"; echo "LinkedIn URL: ".$linkedin; echo "<br>"; echo "Skills: ".$skills;?></body></html>
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *