|
|
|
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
Tip #1
The global variable $PHP_SELF holds the name of the script that's currently running.
You'll often define a form as something like...
|
<form action="index.php" method="post"> | |
This is usually because you want to check that what the user just entered was valid, to allow for repeated operations until you opt out etc. You'll end up doing it quite a bit.
All very good and proper, but what happens if you rename the file? It's quite easy to forget to remember to update the internal links to your own page?
If you re-write the above as...
| | <form action="<?php echo $GLOBALS["PHP_SELF"]; ?>" method="post"> | |
PHP will replace the $GLOBALS["PHP_SELF"] with the page you're on. Rename the page and it still works.
|
|
| Using PHP im HTML image tags Categories : PHP, HTML and PHP, Graphics, Beginner Guides | | | How to preset a text string in a textarea input field Categories : HTML, HTML and PHP, 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 | | | 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 | | | 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 | | | 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 | | | How to Create a Shoutbox Using PHP & MySQL Categories : PHP, MySQL, Web Applications, Beginner Guides, HTML and PHP | | | Newbie Notes #9 - Hyperlinking a post Categories : PHP, Java Script, HTML and PHP, Beginner Guides | | | Kewl Date Example Categories : PHP, HTML and PHP, Date Time, CSS, Beginner Guides | | | PHP based HTML rabbing Tools Categories : PHP, HTML and PHP, Tag Extractors, Regexps, Beginner Guides | | | TreeView - Finally a working tree view function to be used as you want. Simple create the Table using the code provided and you will be able to have a tree view in your project. Download the zip to get the images. Categories : PHP, HTML and PHP, Navigation | | | This script allows people to add their favorite quotes to your website. This
could easily be modified to be a guestbook script or comment page script. Categories : PHP, Complete Programs, HTML and PHP, Misc | | | Produces browser-safe strings while preserving HTML tags. Categories : Strings, HTTP, PHP, HTML and PHP | |
| | | | Shiraz Esat wrote : 1075
It is also possible to have <form action="" ...>
to post back to the page you`re on (no PHP required!).
I`m not sure, though, which browsers support this shortcut.
| | | | Ade Morgan wrote : 1076
I don`t think you even need the `action` attribute to post to the same page, all you need is:
<form method="post">
| | | | Boaz Yahav wrote : 1077
The question is : will it work for all clients?
| | | | Simon Booth wrote : 1078
Actually the point is that you _should_ use attributes even if they`re optional. It`s good style and if there are any quirks with a browser you`re guaranteed your code will work.
For example you don`t _need_ a closing </option> tag in a select list but it`s better to have one than not to have one.
The other thing is that you can`t guarantee how JavaScript will work if an attribute is missing and it`s often desirable to alter the value of a given attribute.
Omitting an attribute just cos it works is just lazy.
| | | | ezone ezone wrote : 1079
Don`t forget to produce well formated code... it pays in the future :-)
| | | | Simon Booth wrote :1080
The above comment is extremely true
Indentation of things like forms etc is also a very good idea - it makes it much easier to find the buit you wanna change if you ever need to do so
The other thing, although we all hate it, is comments. You come back to a bit of code a year from now and suddenly realise you haven`t got the foggiest idea what it did, why id did it, what the parameters are meant to be or what it was meant to return
Never assume you won`t have to modify something later
It`s very useful to build up a little library of functions you use a lot - we get to this later in the series (when Boaz lets you see them all <g>)
If anyone has things they thing I should include in Newbie Notes drop me a line - I`m covering quite a wide range of things with the first 10 to see what people wanna read.
| |
|
|