|
|
|
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.
Firstly you may not want the ugly and possibly abusable variable you're passing being shown in plain, you don't really want http://www.myserver.dom/somescript.php?user=fred&pass=pass displayed in the address bar after all.
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.
Usage Example
|
<form name="aform" action="somewhere.php" method="post">
<input type="hidden" name="firstName" value="<?php echo $firstName; ?>">
<input type="hidden" name="lastName" value="<?php echo $lastName; ?>">
<input type="hidden" name="section" value="0">
</form> | |
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...
| | <a href="" onclick="return clickpost(<?php echo $param; ?>);">Some text</a> | |
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. |
|
| Pull Down Surfing - Surf on Change Categories : Java Script, MySQL, HTML and PHP, PHP, Databases | | | Remote Scripting: send form POST data to a script and insert the results into a page without refreshing the page. Categories : PHP, AJAX, HTML and PHP, Java Script | | | Newbie Notes #1 - Making a form return to itself Categories : PHP, Beginner Guides, HTML and PHP | | | How to preset a text string in a textarea input field Categories : HTML, HTML and PHP, PHP, Beginner Guides | | | Easy alert box pop-up function Categories : PHP, Java Script, Beginner Guides | | | Form Submission Using Array's Categories : PHP, HTML and PHP, Beginner Guides, Arrays | | | Simple PHP Form Field Generator Categories : PHP, Beginner Guides, Form Processing, HTML and PHP | | | Dynamic generation of textboxes, select items etc in a table for use with databases applications, matrimonials and for job sites Categories : PHP, HTML and PHP, Java Script | | | Kewl Date Example Categories : PHP, HTML and PHP, Date Time, CSS, Beginner Guides | | | Tree Menu Dynamic (+Static) with Loading in Progress.. Categories : PHP, Java Script, HTML and PHP | | | OverEasy - PHP generated JavaScript to do mouseovers on
your pages. Modify one file and one function does it all
for you! Categories : PHP, Java Script, HTML and PHP, MySQL | | | mySQL/PHP/search with multientry
form and table output with colored rows Categories : PHP, Beginner Guides, MySQL, HTML and PHP, Databases | | | Multiple Select box, Select multiple Items from Menu.List box Categories : PHP, HTML and PHP, Beginner Guides | | | Local Time clock and Server time usign PHP and JavaScript Categories : PHP, Java Script, Date Time, Beginner Guides | | | This PHP function creates dropdown select lists for time and date that you can change, outputs a 14 char MySQL timestamp in a text field Categories : PHP, MySQL, Java Script, HTML and PHP | |
| | | | Shiraz Esat wrote :1087
Be careful with some browsers (e.g. IE5.0):
<a href="" onclick="return clickpost(<?php echo $param; ?>);">Some text</a>
might not work, because the href="" `overrides` the onclick.
I use:
<a style="cursor:hand" onclick="return clickpost(<?php echo $param; ?>);">Some text</a>
So that we still get the rollover effect on the <a> tag and we make sure the onclick works.
| |
|
|
|