0

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

4
  • Can you post your code here ? Commented Nov 22, 2013 at 5:45
  • it's pretty standard to just post to action="do_stuff_action.php"... a different page..like poster said above.. please post your code so we can help you further. Commented Nov 22, 2013 at 5:46
  • All your validation and bulletproofing should be in the registration.php Commented Nov 22, 2013 at 5:56
  • 2
    IF any error found then save the errors in a session and redirect the user to form_page.Fetch errors from session , display it , and delete the errors from the session. Commented Nov 22, 2013 at 5:56

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

using die() isn't such a good idea.. :( I will use javascript before submitting the form.
well for testing purposes is what i suppose he's doing on an intranet, he can manually just add everything into the $_GET. I understand what you're saying though. Feel free to edit my answer and add a bit of your theory. Thank you.
1

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");

1 Comment

I see its easier to validate the form using java script. I think I should use javascript instead.
0

You can learn form validation here : http://allitstuff.com/registration-form-in-php-with-validation/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.