Displaying error messages based on form data in php
<p>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.  </p>

<p>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.</p>

<ol>
<li><p>From what I've read, the way to redirect users on success is to use header("Location: <a href="http://foo.com" rel="nofollow">http://foo.com</a>").  Is that correct, or is there a more proper way to do it?</p></li>
<li><p>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?</p></li>
<li><p>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?</p></li>
</ol>

<p>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)</p>

<p>The page that the form is rendered on (called index.html)</p>

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


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

<p>
</p>

<p>The script</p>

<pre><code>    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
}
</code></pre>
