Reputation: 1938
I have a simple form for a back button that is supposed to take the PHP $_SERVER['HTTP_REFERER']
and use that as the action
for the button.
<?php
echo "<form class='BackButton' action='" . $_SERVER['HTTP_REFERER'] . "'>";
echo "<input type='submit' class='button' value='Back' />";
echo "</form>";
?>
When I navigate to the page with this button and hover over the button it has the correct action='http://engsys.corp.ftr.com//MaterialTracking_Filtered_State.php?search=in'
Then when I click the button it drops the ?search=in
and so the page that comes up isn't correct.
Why does it do this? I thought since it's in the action
that it wouild all be sent?
Upvotes: 0
Views: 57
Reputation: 943579
When you submit a for with method="GET"
(which is the default) the existing query string will be replaced.
To preserve it: generate hidden inputs to copy the values across.
For example (and this can't handle array values in the query string):
<?php
$referer = "http://engsys.corp.ftr.com//MaterialTracking_Filtered_State.php?search=in";
$url = parse_url($referer);
parse_str($url['query'], $query);
?>
<form action="<?= htmlspecialchars($url['scheme']); ?>://<?= htmlspecialchars($url['host']); ?><?= htmlspecialchars($url['path']); ?>">
<?php foreach ($query as $key => $value) { ?>
<input type="hidden" name="<?= htmlspecialchars($key); ?>" value="<?= htmlspecialchars($value); ?>">
<?php } ?>
<input type='submit' class='button' value='Back' />
</form>
That said, and having reread the question: You aren't collecting any user data, so you shouldn't be using a form.
Essentially you are generating a link, so use a link:
<a href="<?=htmlspecialchars($_SERVER['HTTP_REFERER']);?>">Back</a>
… but even that isn't a great idea. The Referer header is optional, and the button or link won't take them back. It will take them forward to a previous URL (with all the implications you might expect for the browser history list).
Browsers come with a built-in back button that works the same way everywhere. You can simply let them use that.
Upvotes: 5