|
|
|
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. |
|
| Using PHP im HTML image tags Categories : PHP, HTML and PHP, Graphics, Beginner Guides | | | Pull Down Surfing - Surf on Change Categories : Java Script, MySQL, HTML and PHP, PHP, Databases | | | Real simple example of removing HTML tags from text then changing \n (new line) to <br>. Could be used in a forum for instance. Categories : HTML, PHP, HTML and PHP, Beginner Guides | | | PHP3: Formmail. Just a cgi formmail, but than in PHP. It is easy to use! Categories : HTML and PHP, Email, PHP, Perl, HTML and PHP | | | Validator 98 - a PHP-script to generate form-validation-code in JavaScript. Categories : Complete Programs, Java Script, PHP, HTML and PHP | | | PHP and javascript mouseover, mouseout, and mousedown events Categories : PHP, Java Script, Form Processing, Beginner Guides | | | Newbie Notes #1 - Making a form return to itself Categories : PHP, Beginner Guides, HTML and PHP | | | 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 | | | 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 | | | Multiple Select box, Select multiple Items from Menu.List box Categories : PHP, HTML and PHP, Beginner Guides | | | Form Submission Using Array's Categories : PHP, HTML and PHP, Beginner Guides, Arrays | | | PHP4 session helper HTML file.
Categories : PHP, Java Script, HTML and PHP, Sessions | | | Local-to-user date and time display regardless of time zone or where the website's server is located Categories : PHP, Date Time, HTML and PHP, Java Script | |
| | | | 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.
| |
|
|