SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Apr 2003
    Location
    Lubbock, Texas
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking PHP Form validation help

    I am currently learning how to validate forms with PHP. The form I have built requires more complex validation than the JavaScript methods I have looked at will allow. Besides, I want to learn more PHP anyway.

    I have posted the form here.

    Here is a snippet of the validation code on the text fields:
    PHP Code:
    if (strlen($_POST['check_in_date']) > 0) {
        
    $check_in_date stripslashes($_POST['check_in_date']);
    } else {
        
    $check_in_date NULL;
        echo 
    '<p><b>Please enter your Check-In Date.</b></p>';

    How do I validate the radio buttons since there are four in each set. I am also trying to figure out how to make it so that IF the user fills in names on rooms 2-5, it will validate the radio buttons for those sets as well.
    Last edited by CreativeResident; Jan 5, 2005 at 13:44. Reason: typo

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    Edmonton, the upper part of AB.
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think what you want is something like this:

    PHP Code:
    if( $_POST['r2p1'] ){
     
     if( ! 
    $_POST['r2_size'] ){
     
      
    //no radio button selected
      
    echo '<p><b>Please select a room size.</b></p>'

     }else{
      
      
    //continue  

     
    }

    And you would do that for each of the other rooms.
    You don't need test the string length, if a variable has something in it, it will return true when tested, otherwise will return false if it is empty.
    Think of radio buttons as text boxes, the post variable is either empty or has a string in it. The only difference is that radio buttons can only be one of a few preset values, in this case, either the value 'Single', 'Triple', 'Double', 'Quad', or empty.
    I hope that helps.

  3. #3
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    umm. correct me if i am wrong.
    but, when you do
    if($_POST['r2p1'])

    won't you get error in case $_POST['r2p1'] doees not exist. and the error would be 'Undefined index'
    so i would suggest you do
    PHP Code:
    if( isset($_POST['r2p1']) ){

    if( ! isset(
    $_POST['r2_size']) ){

      
    //no radio button selected
      
    echo '<p><b>Please select a room size.</b></p>';

    }else{
      
      
    //continue  

    }


  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    Edmonton, the upper part of AB.
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've never had that problem. In my experience, if you use an if statement to test a variable that does not exist, it simply returns false. However, I don't see anything wrong with using isset(). I guess one could go either way.

  5. #5
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's true if you only check for true or false, BUT what if the variable doesn't exists, then it gonna complaint. so, i think its better to use isset() just to avoid those warnings.


Bookmarks

Posting Permissions

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