Tamim Ad Dari Tamim Ad Dari - 6 months ago 22
HTML Question

How to validate form using another php file?

I am new to web designing. Now, I have created a form, and if the user input doesn't meet the requirements I display error message, and if it does I do some mysql commands to enter the info to the database. Now one way to do this is to code the php file into the html and use this command,

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> like described [here][1]

But I don't want to put the script in the same file. How do I do that in another php file such that if user input is invalid, it will return to the homepage with the error message updated?

Here is my code

<!DOCTYPE html>

<link rel="stylesheet" type="text/css" href="register.css">
<h1>Register as A new user</h1>

<div id="signup">
<form id="registration_form" action="registration.php" method="post">
<input type="text" name="name"/>
<span class="errorMessage"></span>
<input type="text" name="email"/>
<span class="errorMessage"></span>
<input type="password" name="passwd"/>
<span class="errorMessage"></span>
<label>Repeat Password</label>
<input type="password" name="repasswd"/>
<span class="errorMessage"></span>
<input type="submit" class="button" value="sign up"/>


What should be in the registration.php? Like the link, I do everything, I set a flag to the error, Now if the flag is true I return the user to the homepage with the error messages, and if false, I show a message saying registration successful. How do I do the part,"return to homepage with the appended error message"?


All your validation and bulletproofing should be in the registration.php

stuff like this:

    //both parameters are required, so make sure they were passed-in
if(!isset($_GET['name'])) {
    die('Must pass \'name\');

    //both parameters are required, so make sure they were passed-in
if(!isset($_GET['email'])) {
    die('Must pass \'email\');
if(!isset($_GET['passwd'])) {
    die('Must pass \'password\');
} else {
  //do cool stuff here

Don't forget your JS validation as well for the front end. I really hope this helps and gives you a bit of direction.