How to pass a value from a link to a form field?


I have a web form in, say ‘page2’ of my website.
To get to the web form, I have a link on ‘page1’ of the site.

ie: <a href=“page2” title=“Click for contact form”>link to form</a>

There are going to be many links on page 1 to the same form.

How can I pass or insert a value into one of the form fields (on page 2) so that when the form loads, one of the fields is already complete with a value determined by the previous page?

ie: the ‘reference’ field in my form on page 2, would already by complete when you click through to it. The value of the reference field is determined by which link you click through on, from page 1.

Im hoping there is something I can add to the a href on page 1, to pass this data across?

Many thanks for any help you can give…

You can add query parameters to the link, but you’ll need a server-side script or program to generate the form for you. It’s not something you can do with pure HTML, which is a markup language, not a programming language.

<a href="page2?field=value">contact form</a>

Thanks for your reply - where can I find out more about this script thing I need then? If i get one will that make the link you posted work? (and does ‘page2’ not need the /html on the end?

Sorry im a bit of a newbie

A much used server side scripting language is PHP.
Try googling for ‘php form tutorial’ and you’ll get lots of tutorials and examples. All you have to do now is study… :wink:

What sort of script am I looking for?

It may help my googling for tutorials and info, if I knew what the term or name was for doing what im wanting to do - assuming there is one? :-S


Did you try googling what I said? The first result is (I quote)

PHP form introduction. A complete article how to use forms in PHP.

I guess that’s what you need, isn’t it. Learning how to use forms in PHP. Just give it a shot. And if you get stuck at some point, don’t hesitate to come back and ask your questions.

Ive read a lot about using PHP in forms, but that type of tutorial or info is too broad. I want to research about specifically what im wanting to do. Not a begginers guide to PHP, you know?

Is what im wanting to do known as “passing a value from a link into a field” or… i dont even know, but there must be some ‘term’ or name for this function or whatever

Hope im making sense?

Ok, if you already know PHP, then this should be clear enough:

I dont know PHP inside out or anything, very little in fact, but have read a lot about it. Thanks for that link tho!

Am i on the right lines with something like:

HTML Page 1 Link:
<a href="“1234”>Click to go to form</a>

…and then on the Form field…

HTML Page 2 Form Input code:
<input type=“textbox” value=“<?php htmlspecialchars($_GET[“ref”]) . ‘!’; ?>” name=“ref” id=“ref” />

Yes, almost :slight_smile:

<input type="textbox" value="<?php echo htmlspecialchars($_GET['ref']); ?>" name="ref" id="ref" />

Almost. It would be something like this:

<a href="/page2.php?ref=1234">Click to go to form</a>

Yes, essentially.

awesome - thanks guys, ill have a play with this and test it out and post back with results!

Just wanted to say this works perfectly!
Kudos to guido2004 & AutisticCuckoo Thank you.

I have one final question relating to this though…

The link which takes you to the form page, at present, launches the form inside a Lightbox (Thickbox to be precise).

The link code for doing that is as follows:

<a rel=“lightbox”
class=“btn_link thickbox”
>Go to form page</a>

How can I ‘add’ the new line of code (which grabs the ‘ref’: href=“index.php?ref=1234”) on-to the launch Thickbox code href above?