All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Student Registration Form In PHP Code With Validation

Last Updated : Mar 11, 2024

Student Registration Form In PHP Code With Validation

In this article we will show you the solution of student registration form in PHP code with validation, the source code for this project will teach you how to validate forms.

To proceed with creating a user account, we should put some kind of validation code in place, so that we ensure all the required data is present. Please confirm that the name, email, and password are filled in correctly, as well as that the email is formatted properly.

Adding form validations quickly and easily can be accomplished using the JavaScript form validation script.

Once the registration form has been filled out, click the Submit button. If all the required information is provided correctly, the output will be displayed below the submit button.

There are many PHP components available for registering users on the Internet.

There are a lot of dependencies on these, so they are heavy.

As soon as the form is submitted, the PHP code prompts the user to register if they do not have an existing email address.

The @ symbol and the . symbol must be present in a valid email. An example. Email addresses can be validated using various PHP methods. Email addresses will be validated here using regular expressions.

Step By Step Guide On Student Registration Form In PHP Code With Validation :-

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $name = test_input($_POST["name"]);
  $email = test_input($_POST["email"]);
  $website = test_input($_POST["website"]);
  $comment = test_input($_POST["comment"]);
  $gender = test_input($_POST["gender"]);
}
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>
<h3>TalkersCode form validation Example</h3>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  Name: <input type="text" name="name">
  <br><br>
  E-mail: <input type="text" name="email">
  <br><br>
  Website: <input type="text" name="website">
  <br><br>
  Comment: <textarea name="comment" rows="5" cols="40"></textarea>
  <br><br>
  Gender:
  <input type="radio" name="gender" value="female">Female
  <input type="radio" name="gender" value="male">Male
  <input type="radio" name="gender" value="other">Other
  <br><br>
  <input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>Your name</h2>";
echo $username;
echo "<br>";
echo $emailid;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $Gender;
?>
</body>
</html>
  1. In our first step, we write <HTML>, which tells the browser what HTML version we're using. Tags are the first part of HTML documents.
  2. Using the <head> tag, we will explain the project's heading.
  3. To define the body of the webpage, the <body> tag is used. On our website, all the content is written here.
  4. The short tags start with "<?" and end with "?>".
  5. For the validation, we have to verify that the data submitted from the registration form is available in the $_POST variable array and is not empty. As we don't want to allow empty values when all form input fields are required for the user registration process.
  6. These validations will check if the fields are not empty or null. For the Email, upon submitting the registration form the system will check if the value entered in the Email field is a valid email.
  7. For the Password, the system checks that it contains at least 8 characters. Lastly, the Confirm Password will check if the entered value in Password Field is identical equals to the value of this field.
  8. Next, view the source code of the output for the string "String with special characters." and convert it to "String with special characters."
  9. Each field in the registration form has its own validation which is the First Name and Last Name has a null or empty validation.
  10. then we use input type for creating a text box, password, email, and username.
  11. The echo () function is not actually a function, so you are not required to use parentheses with it.
  12. After that </body></html> and code should be executed after closed all tags.

Conclusion :-

For the validation, we have to verify that the data submitted from the registration form is available in the $_POSTvariable array and is not empty.

As we don't want to allow empty values when all form input fields are required for the user registration process.

We are going to use the empty () PHP function. The function allows checking that the key exists in the array and verifying that the value is present and is not empty.

I hope this article on student registration form in PHP code with validation helps you and the steps and method mentioned above are easy to follow and implement.