user1585549
user1585549

Reputation: 3

PHP session back button?

Ok. So I am using Sessions to store data because I am making a multi page form. The thing is, I need a back button with it. I have a submit button that will take the user to the next page and store the data into sessions but I need a back button next to the submit button in case they messed up for whatever reason. Is there anyway to make a back button with php that will take them back to the previous page while showing the data they entered? heres my code of one page. Also, I have tried using the history.go but that only works for one page.

<?php

//let's start the session
session_start();

//now, let's register our session variables
$_SESSION['opinion1'] = 'opinion1';
$_SESSION['choice1'] = 'choice1';

//finally, let's store our posted values in the session variables
$_SESSION['opinion1'] = $_POST['opinion1'];
$_SESSION['choice1'] = $_POST['choice1'];

?>

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href='http://fonts.googleapis.com/css?family=Playball' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="bootstrap.css">
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" type="text/css" href="engine1/style.css" />
<script type="text/javascript" src="engine1/jquery.js"></script>
<script src="jquery.cycle2.js"></script>
<title>Untitled Document</title>
</head>

<body>

<div class="nav">
    <div class="container">
        <h1 class="pull-left">Securing the Future of Village Walk</h1>
        <div class="pull-right">
        <ul class="nav nav-pills">
            <li class="active"><a href="#">home</a></li>
            <li><a href="#">about</a></li>
            <li><a href="#">contact</a></li>
        </ul>
        </div>
    </div>
</div>

<div class="ammendment">
    <div class="container">
        <form method="post" action="page4_form.php">
            <textarea id="opinion2" name="opinion2" value=""></textarea>
            <label for="Yes">Yes</label>
            <input type="radio" name="choice2" value="Yes">
            <label for="No">No</label>
            <input type="radio" name="choice2" value="No">
            <label for="Neither">Neither</label>
            <input type="radio" name="choice2" value="Neither">
            **I NEED BACK BUTTON HERE**
            <input type="submit" value="Next">
        </form>
    </div>
</div>


</body>
</html>

Upvotes: 0

Views: 4091

Answers (2)

Chris Stryczynski
Chris Stryczynski

Reputation: 33871

You could just add an additional form like so:

<form action="pageX.php" method="post> <input name="submitBackButton" type="submit" value="Back"> </form>

This can be detected as a normal GET / POST field within PHP:

<?php
if (isset($_POST['submitBackButton']))
{
    // Do required actions here
}

Upvotes: 0

Alejandro Arbiza
Alejandro Arbiza

Reputation: 766

I would first organize session data in a way that it's easy for you to correlate data entered with the page it was entered.

One possibility could be to organize session data in an array:

$_SESSION['formData'] = array(
    array(... data for step 1 ),
    array(... data for step 2 ),
    array(... data for step 3 ),
);

So $formData[0] would hold data entered in step 1, etc.

As for the button, another submit would be enough:

<input type="submit" value="Back" />

Then, you would have to determine which page you are going back to; which you can achieve by sending a hidden field with the current page number.

<form method="post" action="page_form.php">
    <input type="hidden" name="page" value="X" />

One thing to note here is that the server side process would not longer be one-per-page; instead there would be only one page_form.php where you'll have to determine the action and the page to move to (although you could use one per page and set the right action in the , there's always several solutions to any problem):

<?php
... 

$page = $_POST['page'];
if ( isset($_POST['Back']) ) // Going back
{
    $page -= 1; // Previous page requested 

    // Retrieve data from session
    $data = $_SESSION['formData'][$page-1]; // $page-1 because of zero based array indexes. 

    ...

    // Dynamically load the proper page processor. 
    // each pageX_form.php will know how to deal with the variable $data. 
    include  "page{$page}_form.php" 
}
else
{
    $page += 1; // Next page requested
    $data = $_POST; // In this case, the data is whatever was entered
}

When building the form for each page, you'd have to remember to add the hidden field with the page number:

<form method="post" action="page_form.php">
    <input type="hidden" name="page" value="<?php echo $page; ?>" />

If you wanted to use one server-side process per page, then you could do something like this instead:

<form method="post" action="page<?php echo $page; ?>_form.php">

The server-side process would look different, since you would not have to ask for the page number; it would be implicit.

Upvotes: 1

Related Questions