Jump to content

Help Needed! PHP Based Step-by-Step Process Form


Saves

Recommended Posts

Hello, I am fairly new to PHP though I have worked with it for a couple months now. I am working on a new project and I have the design concept (HTML and CSS) all complete. However, I am currently clueless on how to approach it PHP-wise.  :( Luckily, I do know exactly what I want and how I would like it implemented, I hope that would make it easier for me to seek assistance.

 

I have provided a graphical image on what I want to accomplish, I feel like it would be the best way to deliver my question; this image can be found below:

http://img39.imageshack.us/img39/5725/pmlplan.jpg

 

Also, I will explain in more detail what I am looking for in text:

This website is so that people can upload songs for mastering, it consists of 5 simple steps:

1. Get Started (not a real step, I want to use this one as a home page)

2. Payment Plan (just 3 radios for 3 different options; eg: Option 1 = 1 Song, Option 2 = 3-5 Songs)

3. Information (I want to collect the name, email, and a "browse..." so that they can upload their song)

4. Payment (I want to use a simple paypal method, it doesnt have to stay on my site, but after payment I want it to send the user to the final step my site)

5. Complete (Final step, also not a real step, it just prompts the user that everything was successful)

 

I don't want the users to be able to skip steps. They must proceed in sub sequential order.

I want the users to be able to backtrack by clicking the steps as if they were breadcrumbs in order to make any changes.

Also, I don't want it to send or save any information to the server until the user has completed all 5 steps completely.

 

I'm not asking for someone to write me the code but if someone can give me some pointers or tips on where to begin or how to approach this, it would be greatly appreciated!

Thank You!

Link to comment
Share on other sites

You can use a session to hold the data between requests and a page number to verify if one can access said step.

 

session_start();

define('STEP', 3);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  // user posted data
  
  // data was posted from previous step. OK. update step. do not put any other code here.
  if ($_SESSION['step'] === (STEP - 1)) {
    $_SESSION['step'] = STEP;
  
  // user can not yet access this page. ERROR. redirect user or something.
  } else if ($_SESSION['step'] < STEP) {
    // code here
  }
  
  $_SESSION['formdata'] = array_merge($_SESSION['formdata'], $_POST);
}

 

Step 1 only initializes the session data and does not validate steps like the subsequent pages.

 

session_start();

if (!isset($_SESSION['step'])) {
  $_SESSION['step'] = 1;
}

if (!isset($_SESSION['formdata'])) {
  $_SESSION['formdata'] = array();
}

Link to comment
Share on other sites

You can use a session to hold the data between requests and a page number to verify if one can access said step.

 

session_start();

define('STEP', 3);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  // user posted data
  
  // data was posted from previous step. OK. update step. do not put any other code here.
  if ($_SESSION['step'] === (STEP - 1)) {
    $_SESSION['step'] = STEP;
  
  // user can not yet access this page. ERROR. redirect user or something.
  } else if ($_SESSION['step'] < STEP) {
    // code here
  }
  
  $_SESSION['formdata'] = array_merge($_SESSION['formdata'], $_POST);
}

 

Step 1 only initializes the session data and does not validate steps like the subsequent pages.

 

session_start();

if (!isset($_SESSION['step'])) {
  $_SESSION['step'] = 1;
}

if (!isset($_SESSION['formdata'])) {
  $_SESSION['formdata'] = array();
}

 

Wow ! Thank you for such an amazing answer. I really appreciate you going out of your way to help me. :)

I'll go ahead and implement the codes and post back here with updates on my progress.

 

Thanks again!

Link to comment
Share on other sites

I'd do it slightly differently, in that I'd do the validation for the form on the current step. Instead of doing the validation on the next step.

That'll keep all the relevant code on the same "page" (file or function), and make it a bit easier to maintain.

 

In short my steps would be like this:

Step #:
    - If not submit
        - Print form & quit
    - If input not validated
        - Repopulate form with user-data
        - Print form & quit
    - Save input
    - Increase step counter
    - Redirect to next step (GET).

Link to comment
Share on other sites

  • 4 weeks later...

Wow, it's been a month! Sorry for the extended time period. I have been struggling to code this by studying and through trial and error. I finally got the website to a sustainable point, whereas the basic functionality is coded out.

 

What I am trying to do now is add PayPal on to Step 4 to where it will read all the values it collected from the end-user.

Here are all the variables:

$_SESSION['step']

$_SESSION['plan']----** Can be plan1, plan2, plan3, or plan 4; based off what the end-user selected in STEP 2. **

$_SESSION['fullname']

$_SESSION['email']

$_SESSION['phone']

 

Here are the first 4 steps in an image for visual reference (each step is on a different page):

http://i78.photobucket.com/albums/j89/Savesx/test.png

 

Here is the coding for each page (STEPS 1-4):

 

STEP 1:

<?php session_start();

if (!isset($_SESSION['step'])) {
  $_SESSION['step'] = 1;
}

if (!isset($_SESSION['formdata'])) {
  $_SESSION['formdata'] = array();
} ?>

 

STEP 2:

<?php session_start();

if ((!isset($_SESSION['step'])) || ($_SESSION['step'] < 1)) {
  	header("location:x.php");
exit(); 
} 

$_SESSION['step'] = 2;

define('STEP', 3);

$step = '';
$step = $_SESSION['step'];

if (isset($_SESSION['plan'])) {
unset($_SESSION["plan"]);
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  // user posted data
  
  // data was posted from previous step. OK. update step. do not put any other code here.
  if ($_SESSION['step'] === (STEP - 1)) {
    $_SESSION['step'] = STEP;
  
  // user can not yet access this page. ERROR. redirect user or something.
  } else if ($_SESSION['step'] < STEP) {
    // code here
  	header("location:x.php");
exit();
  }
  
  $_SESSION['formdata'] = array_merge($_SESSION['formdata'], $_POST);
} ?>

 

STEP 3:

<?php session_start();

if ((!isset($_SESSION['step'])) || ($_SESSION['step'] < 2)) {
  	header("location:x.php");
exit(); 
} 

$_SESSION['step'] = 3;

define('STEP', 3);

$step = '';
$step = $_SESSION['step'];

if ((!isset($_SESSION['fullname'])) || ($_SESSION['email']) || ($_SESSION['phone'])) {

} else {
$_SESSION['fullname'];
$_SESSION['email'];
$_SESSION['phone'];	
}

if (!isset($_SESSION['plan'])) {
$_SESSION['plan'] = $_GET['plan']; 
} else {
$_SESSION['plan'];
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  // user posted data
  
  // data was posted from previous step. OK. update step. do not put any other code here.
  if ($_SESSION['step'] === (STEP - 1)) {
    $_SESSION['step'] = STEP;
  
  // user can not yet access this page. ERROR. redirect user or something.
  } else if ($_SESSION['step'] < STEP) {
    // code here
  	header("location:x.php");
exit();
  }
  
  $_SESSION['formdata'] = array_merge($_SESSION['formdata'], $_POST);
} ?>

 

STEP 4:

<?php session_start();

if ((!isset($_SESSION['step'])) || ($_SESSION['step'] < 3)) {
  	header("location:x.php");
exit(); 
}

$_SESSION['step'] = 4;

define('STEP', 3);

$step = '';
$step = $_SESSION['step'];
$_SESSION['plan'];

if ((!isset($_SESSION['fullname'])) || ($_SESSION['email']) || ($_SESSION['phone'])) {
$_SESSION['fullname'] = $_GET['fullname'];
$_SESSION['email'] = $_GET['email'];
$_SESSION['phone'] = $_GET['phone'];
} else {
$_SESSION['fullname'];
$_SESSION['email'];
$_SESSION['phone'];
}


if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  // user posted data
  
  // data was posted from previous step. OK. update step. do not put any other code here.
  if ($_SESSION['step'] === (STEP - 1)) {
    $_SESSION['step'] = STEP;
  
  // user can not yet access this page. ERROR. redirect user or something.
  } else if ($_SESSION['step'] < STEP) {
    // code here
  	header("location:x.php");
exit();
  }
  
  $_SESSION['formdata'] = array_merge($_SESSION['formdata'], $_POST);
} ?>

 

If anyone can help me out with this that would be amazing! I have done a lot of research but I can't seem to tackle this one my own...

Link to comment
Share on other sites

Not quite the way I'd done it, and as I laid out in my previous post. Using your second step as a baseline, this is (approximately) how I'd do things:

<?php

session_start ();

// Make sure we got permission to reach _this_ step, not if we've visited the previous.
if ((!isset ($_SESSION['step'])) || ($_SESSION['step'] < 2)) {
header ("location:x.php");
exit ();
}

// If user didn't post anything, just show the form
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
// TODO: Show the form.
return;
}

// Not sure what this does, so leaving it as is.
if (isset ($_SESSION['plan'])) {
unset ($_SESSION["plan"]);
}

// Assume all validation succeeds.
$validated = true;

// TODO: User posted data, validate it all. Set $validated to false if anything fails.

if (!$validated) {
// TODO: Something failed, pre-fill form with the posted data and show it again.
return;
}

// Save the results from this step.
$_SESSION['formdata'] = array_merge ($_SESSION['formdata'], $_POST);

// Give permission to reach next step.
$_SESSION['step'] = 3;

// Send user to the next step.
header ("location:x.php");
exit ();

?>

Doing it this way we know that the only way that the user can get from one step to the next, is if everything has been validated and saved into the session.

 

As for how to integrate Paypal into this, then you'll have to look at the Paypal API. In either case, that's the topic for another thread, not to mention a different section, I'm afraid.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.