...

View Full Version : Basic isset() Question



jessjenn
01-23-2004, 04:21 AM
Whenever I process variables from forms, I always use isset($_POST['whatever']) for checking the existence of it. This is just in case the user decides to go to that page directly without submitting the form. On top of that, if I want to check if the data is numeric, then I do

if(isset($_POST['whatever']) && is_numeric($_POST['whatever']))

My problem is that when I'm dealing with a bunch of form variables this checking can get quite long because I check for the datatype (is_numeric) and its existence (isset).

My question is, if I even have to use isset() when checking for datatypes. Would using functions like is_numeric or any "is_" function be enough? Is adding isset() in my validation just a waste if I'm combining it with "is_" functions?

Any help is appreciated. Thanks!

Spookster
01-23-2004, 04:30 AM
If you just want to check to see if the form has been submitted you can just check for the existance of the submit button variable.



<input type="submit" name="submit" value="Submit" />

<?php
if(isset($_POST['submit'])){
// Code for when form has been submitted goes here
}
else{
// Code for when form hasn't been submitted here
}



As for validating the form elements if you have many of them and do pretty much the same validation you could write a function that loops through the $_POST array and checks each one. That way you wouldn't have to write out the code to check each and every variable.

firepages
01-23-2004, 04:04 PM
well

is_numeric( $_POST['var'] ){

will throw a notice if indeed $_POST['var'] is not set.

In situations where it does not matter whether the variable is set or not just if it is set it must be numeric, I cheat and...

is_numeric( @$_POST['var'] ){

else yes you are stuck with the more long-winded approach , don't worry about processing time here, its immeasurable , try perhaps to find ways to loop though all your posted vars and apply validation logic that way instead of typing each out individually.

jessjenn
01-24-2004, 07:36 AM
Thanks for the suggestions. I guess I'm stuck using isset()! =) Looping through $_POST sounds like a good idea.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum