Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Dec 2003
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Basic isset() Question

    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!

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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.

    PHP Code:
    <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.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,051
    Thanks
    10
    Thanked 94 Times in 92 Posts
    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.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #4
    New Coder
    Join Date
    Dec 2003
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the suggestions. I guess I'm stuck using isset()! =) Looping through $_POST sounds like a good idea.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •