Times of India (The leading News Media Company) confirms that one of the most top-paying coding jobs is in the field of PHP.
One of the basic skills a PHP developer should have is the ability to create a form. This tutorial will walk you through the step-by-step process of creating a PHP registration form, where users can complete the process of registration with the help of other details like username, email, and password.
If you want to see how to log in after creating a registration form, you better refer to the PHP login form article, for better clarity.
Before you begin with these topics, it is vital to know the prerequisites.
Prerequisites-
-
Microsoft Visual Studio
-
XAMPP software
Suppose you want to learn the step-by-step process to download and install this software, please click here.
Now, you will begin this tutorial by creating a responsive form using CSS.
1. Create a Responsive Form With CSS
- First, you should create a folder called ‘register’. If you are using Xampp, create a folder within htdocs or www (if you’re using wamp server).
- Now, go to the Microsoft visual studio and create PHP files under the registration folder.
- Open these files up in a text editor of your choice. The Microsoft Visual Studio is a highly recommended choice.
- Now, write a PHP code for the registration form.
- Next, include a cascading style for the PHP registration form.
For the registration form, write the code mentioned below in a new PHP file. Regiser.php:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body{
font-family: Calibri, Helvetica, sans-serif;
background-color: grey;
}
.container {
padding: 50px;
background-color: #f7dc6f ;
}
input[type=text], textarea {
width: 100%;
padding: 15px;
margin: 5px 0 22px 0;
display: inline-block;
border: none;
background: #f1f1f1;
}
div {
padding: 10px 0;
}
hr {
border: 1px solid #f1f1f1;
margin-bottom: 25px;
}
.registerbtn {
background-color: #4CAF50;
color: white;
padding: 16px 20px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 100%;
opacity: 0.9;
}
.registerbtn:hover {
opacity: 1;
}
</style>
</head>
Note: .css file helps developers design the website in the desired way.
After this, create a file in the Microsoft studio and save it as style.css. Write the code depicted below in the style.css file and modify your file as required:
2. Create a PHP Registration Form
Next comes the most important part, wherein you have to add the required details of a PHP form.
<body>
<?php
$fullname = $email = $gender = $comment = $age = "";
if ($_
“L{SERVER["REQUEST_METHOD"] == "POST") {
$fullname = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$comment = test_input($_POST["comment"]);
$gender = test_input($_POST["gender"]);
$age = test_input($_POST["age"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
- As a result, in the browser, you will see something similar to the registration form below.
- So far, you created a PHP form and also used cascading to change the look of the website.
- Now, it's time to use the POST method to submit data and complete the registration form.
- With the below code, all the information entered by the user from the form will be registered in the back-end.
3. Create a PHP Registration Form Using POST METHOD
- POST is a PHP variable that can be declared global. Its primary function is to gather details after submitting an HTML form with method="post."
- $_POST is mainly used by the developers to pass variables.
Here you will create a form for the registration page with the method as POST.
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="container">
<center> <h1>Registeration Form</h1> </center>
<hr>
<label> <b>Fullname</b> </label>
<input type="text" name="name" placeholder= "Fullname" size="50" required />
<label for="email"><b>Email</b></label>
<input type="text" placeholder="Enter Email" name="email" required>
<label for="age"><b>Age</b></label>
<input type="text" placeholder="Enter Age" name="age" required>
<div>
<label>
<b>Gender : </b>
</label><br>
<input type="radio" value="Male" name="gender" checked > Male
<input type="radio" value="Female" name="gender"> Female
<input type="radio" value="Other" name="gender"> Other
</div>
Here, in the gender field, you used the type as radio to create a selection display.
Now write the PHP code to display the details on the page itself.
<label>
<b>Comment : </b>
<textarea name="comment" cols="50" rows="2" placeholder="Current Address" value="address" required>
</textarea>
<button type="submit" class="registerbtn">Register</button>
</form>
<?php
echo "<h2>Your Input:</h2>";
echo $fullname;
echo "<br>";
echo $email;
echo "<br>";
echo $age;
echo "<br>";
echo $gender;
echo "<br>";
echo $comment;
echo "<br>";
?>
</body>
</html>
- In the above code, within the PHP script, you used a htmlspecialchars() function.
- It is a function that converts special characters into HTML entities to make the code more secure from external attacks.
- Now in this part of the code, you created variables for all the fields in the form and saved all the respective input inside them using the POST method.
- Further, using the echo keyword, you displayed all the information on the same page.
- So, this was a simple registration page without any database used.
Output 1:
Once you enter the data, you will get the following output:
Output2:
Congratulations, you have successfully created a PHP registration form.
Accelerate your career as a skilled MERN Stack Developer by enrolling in a unique Full Stack Developer - MERN Stack Master's program. Get complete development and testing knowledge on the latest technologies by opting for the MERN Stack Developer Course. Contact us TODAY!
Final Thoughts
This brings us to the conclusion of the PHP Registration Form tutorial. Here, you learned how to create a PHP Registration form, to create a CSS sheet for the registration form, and how to save user details with the POST method on Microsoft Visual Studio.
Are you planning to take up a PHP certification? If yes, Simplilearn's PHP course would be the right choice for you. The Full Stack Developer - MERN Stack of Simplilearn includes all the basic and advanced concepts, thus helping you easily learn PHP.
Do you have any questions for us? Please feel free to put them in the comments section of this tutorial; our experts will get back to you at the earliest. To learn more, watch the following video: