Forum Moderators: coopster
Here's the scenario: Client wants to know what page prior to the contact page that the customer was on and wants that included in the information when they receive the submitted contact form. I have done this previously with the javascipt below:
<script type="text/javascript">
function loadRefURL (){
document.forms[0].theAddr.value = document.referrer;
}
</script>
<body onload="loadRefURL()">
In the form I have this:
<input type="hidden" name="theAddr" />
In the php code I have this:
$theAddr = $_POST['theAddr'] ;
And so on. The thing is - I'm switching the contact form to another PHP script which includes the form and PHP on one page (no separate PHP file). For some reason when I add the javascript to this contact page, I get an error when trying to send the form.
Is there something similar with PHP that serves this type of function?
Thanks,
Hope
but if it is just the referring page you are after you could try the $_SERVER index HTTP_REFERER to see if it has been sent by the browser (it's not always reliable, of course)
Thanks, coopster. But doesn't the HTTP_REFERER only reference the page the visitor is at? For instance, if the visitor is filling out the contact form on contact.php, wouldn't contact.php then be the one listed as the HTTP_REFERER? What my client wants to know is what page on their site was visited right before they went to the contact page. Is that possible with PHP or does it have to include sessions and such?
Thanks,
Hope
page1.php
<html><head><title>Page1</title></head>
<body><p><a href="page2.php">Page2</a></p></body></html>
<html><head><title>Page2</title></head>
<body><pre>
<?php print_r($_SERVER);?>
</pre></body></html>
if (isset($_REQUEST['submit'])) {
$refering_page = $_SERVER['HTTP_REFERER'];
and later:
$refering_page!== $script_location) {
which is the contact.php page.
How would I incorporate your method? Wouldn't that take a lot of complicated PHP code?
Thanks,
Hope
When you hit your landing page (page2.php in my example, contact.php in your real application), you will want to grab the HTTP_REFERER variable and place it in your own variable before the form is displayed and/or submitted for the first time. How do you do that? Check to see if you have stored it already, perhaps in a hidden form field. Make sense? Something like the following ...
page2.php
<html><head><title>Page2</title></head>
<body><form method="post" action="page2.php">
<?php
if (isset [php.net]($_POST['referer'])) {
$referer = htmlentities [php.net]($_POST['referer']);
} else if (isset($_SERVER['HTTP_REFERER'])) {
$referer = htmlentities [php.net]($_SERVER['HTTP_REFERER']);
} else {
$referer = 'unknown';
}
?>
<input name="referer" type="hidden" value="<?php print $referer;?>" />
</form></body></html>