Mike Mike - 1 year ago 54
HTML Question

HTML form button not sending full value

I have a simple form for a back button that is supposed to take the PHP

and use that as the
for the button.

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
Then when I click the button it drops the
and so the page that comes up isn't correct.

Why does it do this? I thought since it's in the
that it wouild all be sent?

Answer Source

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):

$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' />

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download