Transferring Link Page URL to Web Page Form
Do you have a "recommend this page" link on your web pages?
Getting the URL (and/or the page title) of the recommended
page automatically into the central recommend form is
the purpose of this article.
Our example is a recommend form that will recommend
the web page the person was at when they clicked the
"recommend this page" link. The technique can be used with
other forms, as well, such as feedback/contact forms, where
it may help to know on which page the user found the
"contact" link.
The technique can be accomplished with either JavaScript or
PHP. You'll find information for both.
JavaScript can be used on any page, but requires a
JavaScript-enabled browser.
PHP can be used only on pages that the server recognizes
may contain PHP code. Usually, these will be web pages with
.php file name extensions, but other extensions might also
be recognized. There is no special browser requirement when
PHP is used.
A primary difference between JavaScript and PHP is that
JavaScript runs in the browser, after the web page has been
retrieved from the server, and PHP runs on the server before
the web page is sent to the browser.
JavaScript can run on any browser that is
JavaScript-enabled. PHP can run on any server
that has PHP available.
As an aside, something to consider when creating a new web site or new sections of a web site is whether or not to create the web pages with .php file name extensions. This "Considering PHP?" blog post talks about some pros and cons.
How This Technique Works
One or more web pages have a link that can be clicked to
load the recommend form.
The URL of the link that can be clicked has parameters that
contain the URL of the current web page and/or the title of
the current web page. Creating this link is step 1.
When the link is clicked, the parameters are carried over
to the web page with the form.
At the web page with the form, the parameter information
is inserted into existing form fields for that purpose.
Inserting this information is step 2.
The recommend form can now be used to recommend the
specific page where the "recommend this page" link was
clicked.
Step 1 Creating the Link To the Form
Step 1 is creating the link and URL to the "recommend"
form. The link will be put on one or more web pages.
The Web Page With the Link JavaScript Method
The following JavaScript will create a link with the
correct parameters
The first document.write() line is where the HTML A tag and
href attribute are started. The href attribute value is the
URL of the form the link is to click to, with a "?" appended
to the URL. Change the href URL in the JavaScript to the URL
of the web page with your form keep the question mark
character at the end of the URL.
The rest of the JavaScript can be copied and pasted just
like it is presented above unless you don't want both
URL and page title to be carried over to the form.
The second document.write() line is where the parameter for
the current URL is appended to the form link URL. This line
is optional.
The third document.write() line puts an "&" character
between the URL and title of the web page. This line is to
be used only if both the URL and title of the web page will
be part of the link URL.
The fourth document.write() line is where the parameter for
the current page's title is appended to the form link URL
(the title being whatever is specified with the HTML TITLE
tag). This line is optional.
It is assumed that at least the URL or the page title will
be specified, else why use this JavaScript in the first
place. When both are specified, the "&" must separate the
two.
The fifth document.write() line completes the href
attribute and opening A tag.
The sixth document.write() line is where the link text is
specified. If preferred, it may be an image tag instead of,
or in addition to, text.
The seventh and last document.write() line is for the
closing A tag.
It may be prudent to add a NOSCRIPT tag to the web page for
browsers without JavaScript.
With the NOSCRIPT tag, browsers without JavaScript have a
link to click even though the recommend form will not
receive the current page information. It is assumed the
default values in the form will recommend the web site
rather than an individual web page.
The Web Page With the Link PHP Method
PHP is not as straight-forward if the page title is also to
be part of the link. Unfortunately, PHP code does not know
the title of the web page, as PHP code runs before the web
page is loaded into the browser.
PHP can, however, automatically determine the URL of the
current web page.
Following are 2 PHP code sections to create the link. The
first has only the current web page URL in the parameter
and the second also has the current web page title.
Here is the first PHP code to create the link:
The first echo() line is where the A tag and href attribute
are started. The href attribute value is the URL of the
form the link is to click to, with a "?" appended to the
URL. Change the href URL in the PHP code to the URL of the
web page with your form keep the question mark character
at the end of the URL.
The second echo() line is where the parameter for the
current URL is appended to the form link URL.
The third echo() line ends the href attribute and opening
A tag, specifies the link text (which may be image URL
instead, or in addition to), and prints the closing A tag.
Here is the second PHP code to create the link:
This PHP code is identical to the previous except for the
addition of a line, the second from last echo() line. That
is the line where the web page title parameter is appended
to the URL.
The title will need to be specified at every page the link
is used. (Notice that apostrophes, sometimes called "single
quotes," within the specified title must be escaped with a
backslash character.) I am aware of no PHP code to
automatically insert the web page title.
Step 2 Inserting the Information Into the Form
Step 2 is inserting the information carried over with
the link to the form.
Although the technique of transferring link page URL and
title to a web page form can be integrated with most forms,
this article refers specifically to a form that submits to
Master
Recommend Pro V4. For other forms, simply change form
and field names to reflect the form you're using.
Inserting the Information JavaScript Method
Paste this JavaScript somewhere below the recommend form.
The above is a simple copy 'n paste unless you're
implementing this technique with a form other than
Master
Recommend Pro V4, in which case the 3 marked variables
for form and field names might need to be changed.
The JavaScript will replace URL or title only if the
information was carried over from the link. If either
or both URL and title information is not available,
the respective form field values are not changed.
Inserting the Information PHP Method
Updating the form field values with PHP is more than
just copy 'n paste.
First, find the hidden field name="siteURL". It looks
something like this (except all one line):
<input
type="hidden"
name="siteURL"
value="http://example.com/">
Replace the above with:
Replace "http://example.com/" in
the above PHP code with the default value for the form field.
Second, find the hidden field name="sitename". It looks
something like this (except all one line):
<input
type="hidden"
name="sitename"
value="My Site!">
Replace the above with:
Replace "My Site!" in
the above PHP code with the default value for the form field.
In both of the above instances, the PHP code will use the
information if it was carried over from the link. Otherwise,
the value on the "else..." line is used.
Testing
Before employing either of these methods site-wide, test it
on one or a few pages. This way, the code for the links can
be corrected as needed before pasting them on every web page
of the site.
Question:
Did you find this article interesting and understandable? How can it be improved?
Your response is anonymous.
When done typing, click anywhere outside the box. [more info]
Will Bontrager
©2006 Bontrager Connection, LLC
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.