PDA

View Full Version : Inserting URL from previous page into text box



super-trevor
12-09-2009, 03:17 PM
Hi All

Im trying to make a form so customers can order by telephone if they dont want to order online.

The idea is they click a link ("Order this product by telephone") which takes them to a page with a simple form where they can enter their name and phone number. On this same form I would like a field which is automatically filled out containg the address on the previous page they were on (ie the page where they clicked "order this product by telephone")

An alternative solution would be for the customer to fill out their name and phone number and send the form with the title of the email (when it arrives in my inbox) containing the URL of the page where they clicked "Order this product by telephone".

This is the closest code Ive found, ie it spits out the address of the previous page but its in Outlook and I want it to do it in a simple form

[CODE]<SCRIPT language="javascript">
function mailpage()
{
mail_str = "mailto:?subject= Fill in your subject line here";
mail_str += "&body= Put a message in the email body here";
mail_str += " " + parent.location.href;
location.href = mail_str;
}
</SCRIPT>
<A HREF = "javascript:mailpage()">Email this page to a friend</A></p>[CODE]

its based on a tell a friend link.

I cant put together code to do what I want no matter what I try. If you can help it would be really appreciated.

jscheuer1
12-09-2009, 04:51 PM
For so many reasons, you really need server side code for this. Is it (PHP, or asp, etc.) available to you on your server?

djr33
12-09-2009, 07:24 PM
The ways to do this are somewhat awkward.
There are two options:
1. Using a serverside language you can get the "referrer", that is the page from which the current page was loaded. If the user was on a.php and clicked a link to b.php, the value of it will be a.php. It is unreliable for a few reasons: 1. The format can vary (relative address, absolute, etc), 2. it can be faked by users. It may not be, but it isn't entirely reliable (so it's never a good idea for a secure system, etc., but it can be helpful if needed in a nonsecure system).
2. If you need more than one page back, as you often do for anything involving a submitted form (for example, a log on, a shopping cart + confirm order, etc.), then you will also need server side code that stores a url.
This is typically done like this (let's assume the login example):
<a href="login.php?lastpage=a/b/c.php">
Then when you get to the login page, that "lastpage" value is stored on the server (typically with sessions, maybe with cookies, or even in a database), and then once they are logged in they are redirected to that url.
This is also unreliable (the user can fake it fairly easily), so the best method overall is again to assume it won't be reliable and just helpful, so default to the homepage, etc., if the url doesn't make sense.

But, yes, you will need serverside code.

In theory you could attempt method (b) with javascript using cookies, but there are many reasons this is a bad idea, the primary one just being that not all users will have javascript enabled, so they won't have any value there. Then again, I guess those users can be redirect to the home page, so that might work.

super-trevor
12-10-2009, 10:40 AM
I have access to server side PHP scripts but I think I will be out of my depth with it!

The code I showed orginally does the main job. ie when you click on the link it opens Outlook and in the body of the Email it shows the URL of the previous page. I assume using parent.location.href;

I'm probably over simplyfying but to get this URL inserted into a form Textbox or form title is a big increase in complication?

jscheuer1
12-10-2009, 11:09 AM
Well, in javascript it's document.referrer. But that's even more unreliable than on the server side. So you could use something like (the referrer should probably be tested to see if it is one that is expected, this only tests if it exists):


mail_str += " " + (document.referrer || "Please Enter Product Name Here");

in place of:


mail_str += " " + parent.location.href;

But don't expect things to work out all the time. If folks have javascript disabled, or they edit out the link before sending, or they have no email client . . .

super-trevor
12-11-2009, 05:53 PM
Good evening jscheuer1, djr33

This is the page where the form is

http://www.streetandcircuit.biz/forms/en/call-back-form-en.html

In the big text box at the bottom I want the URL of the previous page to be inserted automatically. At the moment most people either put in the URL of the contact page/home page etc or dont put a URL at all.

Even if it only works (putting in the URL of the previous page) some of the time it will be a step forward from where it is at the mo

Basically I need help with regards to what code I need put in and where

djr33
12-11-2009, 07:29 PM
jscheuer1 gave the javascript code.
In PHP it's very simple: <?php echo $_SERVER['HTTP_REFERER']; ?>

The only problem is that both are very unreliable. There is no reason not to use the PHP because relying on the Javascript means relying on every user to have Javascript enabled, and that won't be the case. PHP will work in all situations. (At least in the sense that it will always output the value, whether or not the user sent a valid value during the request... most will.)