Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP back button

Posted on 2010-09-18
11
Medium Priority
?
786 Views
Last Modified: 2013-12-12
Hi Experts,

I'd like to use PHP instead of Java to script a 'back' button in a page.  The previous page is a form so I'd like any of the fields to still have the data in them when the user clicks 'back'.

Any idea how to do this?

Thanks,

HNM
0
Comment
Question by:HelpNearMe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
11 Comments
 

Accepted Solution

by:
Terry174 earned 336 total points
ID: 33709867
if (isset($HTTP_REFERER)) {
echo "<a href='$HTTP_REFERER'>back</a>";
} else {
echo "<a href='javascript:history.back()'>back</a>";
}
0
 
LVL 28

Assisted Solution

by:gamebits
gamebits earned 332 total points
ID: 33709872
Use session to remember the data entered, it is done in two fold first on your form page you need to check if the session are set if not you show the blank fields, when the user submit the form you set the session data so if the user goes back than the fields are already populated.
0
 

Expert Comment

by:Terry174
ID: 33709873
This should probably work better.

<?php
  $url = htmlspecialchars($_SERVER['HTTP_REFERER']);
  echo "<a href='$url'>back</a>";
?>

Although they should both work.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Assisted Solution

by:WilliamStam
WilliamStam earned 332 total points
ID: 33711361
create a loop of all the fields submitted with hidden fields..

foreach in post thing


then your back button basicaly submits the hidden fields back to your form.. your form inputs

if(isset($_POST['fld'])){
$v = $_POST['fld'];
} else $v = "";

<input type="text" value="<?php echo $v; ?>
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 332 total points
ID: 33711532
That's an interesting question.  Usually developers are more interested in disabling the back button to prevent accidental re-submission of the data.

But if you want to prepopulate the form input fields, you can usually do it without too much trouble.  Often the information comes out of a data base, but it could come from $_SESSION, too.

Install this and run it to see the moving parts.

Regards, ~Ray
<?php // RAY_prepopulate_forms.php

// SET ERROR REPORTING SO WE CAN IGNORE UNDEFINED VARIABLE NOTICES
error_reporting(E_ALL ^ E_NOTICE);

// ALWAYS START THE SESSION UNCONDITIONALLY AT THE TOP OF THE PAGE
session_start();

// IF ANYTHING HAS BEEN POSTED
if (!empty($_POST))
{
    // COPY THE POSTED INFORMATION TO THE SESSION
    $_SESSION['POST'] = $_POST;
    
    // DISPLAY THE CONTENTS OF THE ARRAYS TO SIMULATE PROCESSING THE DATA
    echo "<pre>" . PHP_EOL;
    echo "POST: ";
    var_dump($_POST);
    echo "SESSION: ";
    var_dump($_SESSION);
    
    // ALL DONE
    die("HIT THE BACK KEY NOW");
}

// IF NOTHING HAS BEEN POSTED, DROP INTO HTML AND PUT UP THE FORM
?>
<form method="post">
YOUR NAME: <input name="name" value="<?php echo $_SESSION["POST"]["name"]; ?>" />
<input type="submit" />
</form>

Open in new window

0
 
LVL 1

Assisted Solution

by:phpmycoder
phpmycoder earned 668 total points
ID: 33712031
This may considered semi-vulnerable, but if your form allows for fields to be filled through GET variables (say, form.php?name=Me fills in the name field when the form displays) you could always iterate through all of the given form information and create invisible fields for each. Then the back button simply will submit all this information back to the form. Try something like this:

 
<?php //PhpMyCoder - formprocess.php
$keys = array('name', 'email', 'zip');  //Array of post fields to send back
?>
<form action="form.php" method="get">
<?php foreach($keys as $key): ?>
<input type="hidden" name="<?= $key ?>" value="<?= $_POST[$key] ?>" />
<?php endforeach; ?>
<input type="submit" name="s" value="Back" />
</form>

<?php //PhpMyCoder - form.php ?>
<form action="formprocess.php" action="post">
<input type="text" name="name" value="<?= htmlentities($_GET['name']) ?>" />
<input type="text" name="email" value="<?= htmlentities($_GET['email']) ?>" />
<input type="text" name="zip" value="<?= htmlentities($_GET['zip']) ?>" />
</form>

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 33712472
"This may considered semi-vulnerable,..."

It is all vulnerable if it depends on unfiltered external data, and it is safe if the script follows the standard, "Accept only known good values."

Every month or so, I like to read all the links on the first two pages here:
http://lmgtfy.com?q=PHP+Security

Best to all, ~Ray
0
 
LVL 1

Assisted Solution

by:phpmycoder
phpmycoder earned 668 total points
ID: 33712570
Hence the htmlentities(), but then again there's lots of other hacks that could apply here and circumvent the htmlentities() function.
My strategy when it comes to vulnerabilities is: learn the attack, practice it on a local machine, and then find a way to prevent it. Usually, though, the last step is really unnecessary since a simple Googling can find a solution.

I can't agree with you more on that quote. Newer programmers never consider the evils of the internet and the dangers of using user generated content.

Also Ray, I can't help but noticing: you really like "let me Google that for you" don't you?
0
 

Author Comment

by:HelpNearMe
ID: 33760880
I'll have some feed back shortly, haven't touched this yet.

Thanks,

HNM
0
 

Author Comment

by:HelpNearMe
ID: 33863386
May be another two days before I can update this question thanks for waiting.

HNM
0
 

Author Closing Comment

by:HelpNearMe
ID: 34026229
I won't be able to test the solutions so I'm just sharing the points and thank you for the effort.

HNM
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question