简体   繁体   中英

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>

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

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

        </form>
    </div>

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"?

put your validation codes in "validate.php" or any file name you like then change the action to validate.php to

then in validate.php if validation matches the requirements. header("Location: registration.php");

if not match

header("Location: back to the httml with form.php");

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.

您可以在此处学习表单验证: http://allitstuff.com/registration-form-in-php-with-validation/http://allitstuff.com/registration-form-in-php-with-validation/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM