Multi-page registration form in PHP

I’m creating a multi-page registration form in PHP with the first page of the form containing name, dob etc, and the second page being for the user to create a username and password.

Note: If I set my action to step1.php (the name of the first page of the form), it saves to the database, but it does not redirect. If I change my action in my form to step2.php, then it redirects to my second form page, but the data is then not stored in the database.

I tried using the header() function to redirect to step2.php when $r submits the user’s data to the database, but the header() function is not working for me.

I’ve cut down on my entire code because it’s a rather large file, but here is the basis of it. I apologise if there are any typos from compacting the code.

<?php
    $page_title = 'Step1';
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        require('../mysql_connect.php'); // Connect to the database

        // Initializing an error array
        $errors = array();

        if (empty($_POST['first_name'])) {
            $errors[] = 'You forgot to enter your first name.';
        } else {
            $fn = mysqli_real_escape_string($dbc, trim($_POST['first_name']));
        }
        if (empty($_POST['last_name'])) {
            $errors[] = 'You forgot to enter your last name.';
        } else {
            $ln = mysqli_real_escape_string($dbc, trim($_POST['last_name']));
        }

             if (empty($errors)) {

                 $q = "INSERT INTO users (first_name, last_name)
                      VALUES ('$fn', '$ln')";
                 $r = @mysqli_query($dbc, $q);

                 if ($r){
                     header("Location:step2.php");
                 }

             else {

                   $errors();
              }
  }  else {
        echo 'Please try again';
    }

 ?>
     <form id="form"
          action="step2.php"  <!-- if I leave it as step1.php, the information is stored in the database, but does not redirect. If I switch to step2.php, it redirects to the second page(desired result), but the user's data is not stored -->
          method="post">

            <label>First Name:</label>
            <input type="text"
                   name="first_name"
                   value="<?php if(isset($_POST['first_name'])) echo $_POST['first_name']; ?>" />

            <label>Last Name:</label>
            <input type="text"
                   name="last_name"
                   value="<?php if(isset($_POST['last_name'])) echo $_POST['last_name']; ?>" />

     </form>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s