We help IT Professionals succeed at work.

PHP Array to Pass Data Between Pages

brendan-amex
brendan-amex used Ask the Experts™
on
I have a form on my website and I want to save that data into an array so that it passes through to more than only the first page. I know how to send data from a form but I want it to be retained so that they only enter the data in once, but it stays with them on several pages. How can I easily do this? Thank you much
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
IT Manager
Commented:
Create a session cookie...you can google how to create one. I would give you the code , but I'm remote now and without my computer...
Dave BaldwinFixer of Problems
Most Valuable Expert 2014
Commented:
The three primary ways of passing the data are hidden (or visible) fields in a form on each page, a database, and Session variables.  Cookies can also be used with javascript to read them and fill in any fields on each page.

I don't know about easy though.  In each case, you have to 'receive' the data on the PHP page and re-enter it in the HTML where it is needed.
Marco GasiFreelancer
Top Expert 2010
Commented:
I post here to code snippets you find in a book titled "PHP Programming solutions" by Vikram Vaswani. Examples are fully working: the first one uses hidden fields to send data from a page to another one; the second example uses php sessions.

//USE HIDDEN FIELDS
<html>
<head></head>
<body>
<?php
// function to rewrite submitted values
// as hidden form fields
function importPrevPageData() {
    $hiddenDataStr = "<!-- input carried forward begins -->\n";
    foreach ($_POST as $key => $value) {
        $hiddenDataStr .= "<input type=\"hidden\" name=\"" . htmlentities($key) . "\" value=\"" . htmlentities(stripslashes($value)) . "\">\n";
    }
    $hiddenDataStr .= "<!-- input carried forward ends -->\n";
    echo $hiddenDataStr;
}

// look for the page number
// if not available, assume page 1
$pageID = isset($_POST['pageID']) ? $_POST['pageID'] : 1;

// display pages by number
switch ($pageID) {
    case 1:
?>
        <!-- page 1 -->
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        Name: 
        <br />
        <input type="text" name="name" size="30">
        <br />
        E-mail address:
        <br />
        <input type="text" name="email">
        <br />
        Telephone:
        <br />
        <input type="text" name="tel" size="10">
        <br />
        <input type="hidden" name="pageID" value=<?php echo ($pageID+1); ?>>
        <input type="submit" name="submit" value="Go to Page 2">
        </form>
<?php
        break;
        
    case 2:
        // perform validation of page 1 data
        // if errors, display error list
        // if no errors, display page 2
?>
        <!-- page 2 -->
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        Card type: 
        <br />
        <select name="cctype">
            <option value="V">Visa</option>
            <option value="M">MasterCard</option>
            <option value="A">AmEx</option>
        </select>
        <br />
        Name on credit card:
        <br />
        <input type="ccname" name="ccname" size="30">
        <br />
        Credit card expiration date (MM/YYYY):
        <br />
        <input type="text" name="ccexp" size="7">
        <br />
        Email invoice:
        <br />
        <input type="radio" name="email_invoice" value="Y">Yes
        <input type="radio" name="email_invoice" value="N">No
        <br />
        <input type="hidden" name="pageID" value=<?php echo ($pageID+1); ?>>
<?php 
        // remove unnecessary form elements
        unset($_POST['submit']);
        unset($_POST['pageID']);
        
        // import previous form submission
            importPrevPageData(); 
?>
        <input type="submit" name="submit" value="Place Order">
        </form>
<?php
        break;
        
    case 3:
        // perform validation of page 2 data
        // if errors, display error list
        // if no errors, display page 3

        // remove unnecessary form elements
        unset($_POST['submit']);
        unset($_POST['pageID']);

        // get all the submitted data as an array        
        $data = $_POST;

        // process the data
        // for example, save to a database or file
            print_r($data);
    
        // print success message
        echo "Thank you for your order";
        break;
}
?>
</body>
</html>

Open in new window



//USE SESSION
<?php
// function to save submitted values
// as session variables
function importPrevPageData() {
    foreach ($_POST as $key => $value) {
        $_SESSION['form'][$key] = htmlentities(stripslashes($value));
    }
}
// look for the page number
// if not available, assume page 1
$pageID = isset($_POST['pageID']) ? $_POST['pageID'] : 1;

// import previous session
session_start();
?>
<html>
<head></head>
<body>
<?php
// display pages by number
switch ($pageID) {
    case 1:
?>
        <!-- page 1 -->
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        Name: 
        <br />
        <input type="text" name="name" size="30">
        <br />
        E-mail address:
        <br />
        <input type="text" name="email">
        <br />
        Telephone:
        <br />
        <input type="text" name="tel" size="10">
        <br />
        <input type="hidden" name="pageID" value=<?php echo ($pageID+1); ?>>
        <input type="submit" name="submit" value="Go to Page 2">
        </form>
<?php
        break;
        
    case 2:
        // perform validation of page 1 data
        // if errors, display error list
        // if no errors, display page 2
?>
        <!-- page 2 -->
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        Card type: 
        <br />
        <select name="cctype">
            <option value="V">Visa</option>
            <option value="M">MasterCard</option>
            <option value="A">AmEx</option>
        </select>
        <br />
        Name on credit card:
        <br />
        <input type="ccname" name="ccname" size="30">
        <br />
        Credit card expiration date (MM/YYYY):
        <br />
        <input type="text" name="ccexp" size="7">
        <br />
        Email invoice:
        <br />
        <input type="radio" name="email_invoice" value="Y">Yes
        <input type="radio" name="email_invoice" value="N">No
        <br />
        <input type="hidden" name="pageID" value=<?php echo ($pageID+1); ?>>
<?php 
        // remove unnecessary form elements
        unset($_POST['submit']);
        unset($_POST['pageID']);

        // add previous submission to session
        importPrevPageData(); 
?>
        <input type="submit" name="submit" value="Place Order">
        </form>
<?php
        break;
        
    case 3:
        // perform validation of page 2 data
        // if errors, display error list
        // if no errors, display page 3

        // remove unnecessary form elements
        unset($_POST['submit']);
        unset($_POST['pageID']);

        // add previous submission to session
        importPrevPageData(); 

        // get all the submitted data from the session        
        $data = $_SESSION['form'];

        // process the data
        // for example, save to a database or file
        print_r($data);
    
        // print success message
        echo "Thank you for your order";
        break;
}
?>
</body>
</html>

Open in new window


Cheers

Author

Commented:
All of you guys gave fantastic solutions, thank you. I'm going to look into all of them and see what best will apply to what I'm doing.