![]() |
|
#1
|
|||
|
|||
PHP form needs validationi am very new to php and am working on a form that is complete and working yet has no validation on the form....
the .php files all link to a template and get the content out of a excel spreadsheet, a .csv file. all the pages work fine. the php code that is in the form page is this: PHP Code:
even if the form is blank it submits it i need some form of validation for it, how do i implement it?? it also links to another file emailtemplate.php in here are all the variables: PHP Code:
help on this would be very grate full, just i don't even know where to start putting the validation code nevermind what it is! thank you. Last edited by JdS : 24-Sep-2004 at 06:26.
Reason: Please insert your example PHP codes between [php] and [/php] tags
|
|
#2
|
||||
|
||||
|
If your concern is only that some data supplied by the user is not blank, then the 'screening/validation' snippet can look a bit like this:
PHP Code:
That's just the general idea. You can create a custom PHP function that you can re-use for validating common data types. Just to whet your appetite, here's how my little custom function works: PHP Code:
To accomplish creating a custom function like this for yourself, you'll need to learn about passing variables by reference, bitwise operators and constants. __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#3
|
||||
|
||||
|
J de Silva, I can just imagine what the internal code of that function looks like. I assume that you do a loop on the array, and nest a series of IF/THEN statments that check if a BIT is turned on (or set) which corresponds to your constants of GID_TRIM,GID_STRIP_TAGS, and GID_PUT_IN_DB, and then perform the associated operation, calling a function perhaps for each one.
That's pretty coooooool! |
|
#4
|
|||
|
|||
|
Quote:
If you want to test whether a variable is empty, use PHP's built-in "empty()" construct. Personally, I prefer the "belt and suspenders" method. Use Javascript validation for speed (avoids an extra trip to the server if the user left a required field blank), and server-side validation in the PHP script. If the user has Javascript enabled in their browser, then there is no penalty by performing the redundant PHP validation. If they don't have Javascript enabled, then the server-side checking will catch it. Bullet-proof! |
|
#5
|
||||
|
||||
|
Quote:
__________________
Cow: You're a lawyer too? Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase! |
|
#6
|
|||
|
|||
|
Quote:
There is nothing wrong with John's answer. It's just that I think empty() is more generally correct, given that the question was posed by a self-described newbie. |
|
#7
|
||||
|
||||
|
One does NOT usually set a $_POST variable/array in one's script.
__________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#8
|
||||
|
||||
|
I don't think its even possible to set a $_POST variable, with your own code - its set by the server. If it is possible, I think it would be bad programming style to set a $_POST variable.
Nonetheless, Javascript validation works too. Too bad we don't have an example handy of how to do this. I think I'll have to get one posted soon. |
|
#9
|
|||
|
|||
|
I think cs2 does have a point. If a form is left blank & submitted, the form values are set, but blank. Recall how a form submitted using the GET method will have a query string like
form.php?name=&id= The variable $_GET['name'] is set, but it's blank. empty() is a better choice to make sure all is okay, I think. (Actually I'm lazier, I do a if isset() and if $var == '' check) POST is similar, just the query string is send in the header request instead of the page URL. GF |
|
#10
|
||||
|
||||
|
How do you suggest empty() works with multiple SUBMIT buttons (on a form) and how will your script know which one was clicked on? The only way that would work is isset(), definitely.
__________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
Recent GIDBlog
First week of IA training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| uisng php to display php | dopee | MySQL / PHP Forum | 6 | 14-May-2004 18:40 |
| php software | dopee | MySQL / PHP Forum | 0 | 04-May-2004 11:26 |
| help with form | orbitel | MySQL / PHP Forum | 11 | 21-Jan-2004 15:29 |
| Automate a data change php form | mjfmn | MySQL / PHP Forum | 4 | 20-Oct-2003 09:37 |
| All the big PHP script collections that matter | jrobbio | MySQL / PHP Forum | 5 | 06-Jun-2003 16:14 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The