NewbieNotes is a little series of tips for people who are new to PHP to give them a few handy tips that the more experienced of us use often
Quite often you'll want to use text links, possibly with some nice CSS effects, to link to another page rather than using a form's button or image submit way of doing things.
There are several reasons why you might not want to use a standard <a href="somewhere.php">Some text</a> invocation in your webpage.
The two most obvious reasons for not doing it this way.
Secondly if you're trying to send over 255 characters many web servers will get very upset, they don't like an address over 255 characters so sending a load of data in the URL just won't work.
An elegant and simple solution to this problem is to use a little JavaScript to fake a submit button being pressed when the user clicks on the link.
All you need is a form at the end of your HTML with as many hidden inputs as you desire replicating the contents of the information you want passed.
Note that it's important that you use a name tag with the hidden form!
The user will never see this form, and you can put as much information as you want in the hidden inputs of the form. Also note that there's no way of submitting the form (yet)
Going back to our original hyperlink we now modify it slightly so it now looks like this...
The 'param' bit in the example above is just to illustrate what comes next and is completely optional, if you need them add then, if not, don't. If you're displaying a list of sections or something then param might be the section number or whatever. We'll use param to change the value of the 'section' hidden input in the hidden form.
Simply add a little JavaScript somewhere in the <head> ... </head> part of your page and we're done. The JavaScript would look something like this...
function clickpost(param)
{
document.forms.aform.elements.section.value = param;
document.forms.aform.submit;
return true;
}
That's it. Your hyperlinks will now use the hidden form to do stuff and it'll use post rather than get so you have a nicer looking application without passing information in plain sight.
It should be noted that this is really handy when building up things like A-Z indexes and suchlike where the next page wants a record ID, you just use the $param trick and the page almost builds itself.