Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryManaging Website Forms

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Putting the Form URL Into a Form Field

Sometimes, it is important to know the location of a form that someone used on your website.

If your website has only one form, then no problem. But if there are two or more forms, it could be an issue. Perhaps one form is for sales questions and the other form for requesting a shipping consideration. Or, the location of the form might be a clue to what the person was looking at that precipitated their comment. As live examples, there is a feedback form by the end of every article at Willmaster.com — it is important that we know from where the form was submitted.

Way back when the internet was small and new, form processing software could use the browser's referral information to know where the form was located. The referrer, in simple terms, is the URL of where the browser just came from.

The browser's referral data is no longer reliable. The data, if the browser has it available, generally is fine. But browser privacy settings can cause the browser to refuse to provide the information. Further, some virus scanners block browser referral information (used to; I haven't heard of late).

Thus, for forms that must provide their URL, something else needs to be done.

And here it is:

A hidden form field holds the URL of the web page where the form is located.

This article provides two ways to get the URL into the hidden form field. One method is with PHP and the other is with JavaScript.

Comparing the Methods

The PHP method populates the form field's value as the form field loads. The JavaScript method populates the value after the form field loads.

The timing difference is negligible.

The PHP method works only on PHP web pages. The JavaScript works only on browsers that have JavaScript enabled without limitations.

Implementing the Methods

To implement, we'll use this hidden field:

<input 
   type="hidden"
   value=""
   id="my-hidden-url-field" 
   name="form-URL">

The value="" attribute will be updated with either PHP or JavaScript.

The PHP method will insert the value directly.

The JavaScript method will use id="my-hidden-url-field" to determine where to insert the URL value.

The hidden field is good for either method. Apply the one you want.

The PHP Method —

For the PHP method, insert the relevant PHP code into the hidden field's value. This is how it will look:

<input 
   type="hidden"
   value="<?php echo(((isset($_SERVER['HTTPS']) and preg_match('/1|on/i',$_SERVER['HTTPS']))?'https://':'http://').$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']) ?>"
   id="my-hidden-url-field" 
   name="form-URL">

The PHP code will insert the URL of the current web page, complete with http:// or https://, the domain name, and the web page location on the server.

To illustrate that it does indeed work, here is that field with type="text" instead of type="hidden".

The JavaScript Method —

For the JavaScript method, put this JavaScript somewhere below the form.

<script type="text/javascript">
document.getElementById("my-hidden-url-field").value=document.URL;
</script>

Notice that the JavaScript looks for the id value of the hidden field. If the hidden field's id value changes, then the JavaScript needs to be changed accordingly.

Here is that field with its value filled in using JavaScript. The type="hidden" attribute was changed to type="text".

Form Submission

When the form submits, the hidden field with the form's web page URL will submit along with the other fields of the form.

With either of those techniques, PHP or JavaScript, the form's URL is with the rest of the submitted form information, at hand for whatever you need it for.

(This content first appeared in Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC