0

I'm trying to create a pretty standard user creation form for a website I'm working on, but my PHP amateur status is preventing me from accomplishing my goal.

Essentially what I want to do is have the user fill out a registration form, and then have a server-side script attempt to register that information. If the registration succeeds, then the user will be redirected to a page telling them to look for a verification email. If that registration fails (the email address is already in the system, not matching passwords, whatever), I would like to reload the page, but display the error message to the user above the form. Currently the form I have is being echoed out of a php file which is separate from the HTML from where the page is stored. My three questions are this.

  1. From what I've read, the way to redirect users on success is to use header("Location: http://foo.com"). Is that correct, or is there a more proper way to do it?

  2. How can I get access to the error(s) that caused the first user to fail? I've read that setting a session variable is a poor idea for a number of different reasons, but without that how can I keep track of the errors thrown by the form validation? Should I just create a global $errors variable that I clear every time I echo the registration form?

  3. For this case, should the page that the registration form is on have a .html extension or a .php extension, or does it not matter? More generally, is there any rule for when a page is .html vs .php/.asp/something else?

Edit: added code below. Note, the form is rendered from a file called in ../views/register_form.php (I assume this is how I'd work on getting something MVC-like in PHP)

The page that the form is rendered on (called index.html)

<body> 
<script type="text/javascript"> <!-- put the jquery load stuff into here -->
$(function(){
  $("#registration_form").load("views/register_form.php");});
</script>


<div class="topbar"><!-- Create the top bar layout-->
  <div class="fill">
    <div class="container">
     <a class="brand" href="#">Website!</a>
     <ul class="nav">
       <li class="active"><a href="#">Home</a></li>
       <li> <a href="#about">About</a></li>
     </ul>
     <form action="scripts/login_user.php" method="post" class="pull-right">
       <input class="input-medium" id="login-email" type="text" name="email_addr"     placeholder="email" />
   <input class="input-medium" id="login-password" type="password" name="password" placeholder="password" />
   <button class="btn" type="submit">Login now!</button>
     </form>
    </div>
  </div>
</div>
<div class="container">
  <div class="content">
    <div class="page header">
     Page header!
    </div>
    <div id="registration_form">
</div>

The script

    if(//some of the data in the form isn't set)
         {
           //Set error conditions based on missing data
         }

    $confirm_password=$_REQUEST['confirm_password'];
    $password=$_REQUEST['password'];
    if($confirm_password != $password){
       //report passwords don't match}


    $email_addr=$_REQUEST['email_addr'];
    $first_name=$_REQUEST['first_name'];
    $last_name=$_REQUEST['last_name'];


try
{
    $successful_creation=create_user_and_send_verification_email($email_addr, $password, $first_name, $last_name);
}
catch(Exception $e)
{
    /*SQL errors are caught here*/
}

if($successful_creation==FALSE)
{
    //If it couldn't be inserted for a non exception generating reason.  If I get here, should I echo a page that has those errors printed, but is otherwise identical to the previous one?
}
else
{
    //redirect to a "wait for a verification email" page
}
1
  • 3
    Thanks for the interesting question, but can we see some code now? Commented Dec 13, 2011 at 5:35

2 Answers 2

1
  1. Yes that is the correct way to do it, but all headers must be sent before you try to render anything to the page. Also, you don't necessarily need to redirect, you can generate the correct output with PHP depending on the form data.
  2. The script that receives the submitted form should check the submitted data for errors and can then output those errors to the page that is generated by that script.
  3. If the page has php code in it then usually it needs a php extension unless your server is set up to process html pages as php. Otherwise, .html is fine.

I hope that helps point you in the right direction, although a more focused question with the code you have up to now would generate a better answer.

There are lots of tutorials on this on the web, you should follow one of those and experiment.

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

3 Comments

I added some code, although that definitely does get me off in the right direction. Can I just call a function to render the next page, and then pass in any errors to that function?
Yes, that is the way to do it. You mention MVC in your edit and that is the best way to go. Check out some frameworks such as Zend Framework rather than re-inventing the MVC wheel.
I intend to look into frameworks once I get a couple of projects under my belt. I don't want to jump into the language with frameworks right away and then be lost when they're pulled out from under me. Thanks for the help!
0

why dont you write the server side code on the same page and for every form input use a flag for eg: if email is not valid $email=1.

den after submission if any flag is initialized,check for the flag and if $email=1 echo error else redirect.

2 Comments

If I were to do it this way, would I just reload the same page as before, but have it be something like <html>
yes you can reload the same page,and in case you want to to use html page use htaccess rewrite rule to show php page as html

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.