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 8 of 8
  1. #1
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts

    need form validation

    How do I validate a form?

    is it like this?
    PHP Code:
    <?php
    $from 
    $_POST['sender'];

    if (!isset(
    $from)){
    echo 
    "You did not specify a sender!";
    }
    else
    {
    include(
    'sucess.php');
    }
    ?>
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Did you try it?

  • #3
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No, I didn't I like to see if it is right before I try it...
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, I think nightfire was implying that you should try it out. You don't need to put it on your actual website, so what harm can be done by it? If it doesn't work as expected, then you know that it's not working correctly.

    Tip: isset() test if a variable exists. All form values appear initially as string values or arrays in the PHP script.
    De gustibus non est disputandum.

  • #5
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, I tried this
    PHP Code:
    <?php
    $name 
    $_POST['search'];

    if(!isset(
    $name)){
    echo 
    "You must enter a search query";
    }
    else
    {
    include(
    'sucess.php');
    }

    ?>
    It partially works. If I enter something, it includes sucess.php. If I do not enter something, it still includes sucess.php!!

    Now can someone help?
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #6
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    if(trim($name) == ""){


    That'll check if anything's been added.

    You're better off using regex though, as someone could just enter a . or something stupid

  • #7
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks

    How could I add more than one to that?

    Like

    if(trim($senderemail,$message) == ""){
    echo "Must specify your email and message";
    }
    Last edited by SDP2006; 10-03-2003 at 11:02 PM.
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #8
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well, if all fields were required, you could do a foreach loop:

    PHP Code:

    $valid 
    true;

    foreach(
    $_POST as $postvar){

          if(
    trim($postvar) == ''){

                
    $valid false;

          }

    }

    if(
    $valid == true){

          include(
    'success.php');

    } else {

          echo 
    'Your form did not validate, please retry';


    if you are only wanting to validate a few fields...

    PHP Code:

    $validate 
    = array('sender''message''subject');

    $valid true;

    for(
    $i=0;$i<count($validate);$i++){

          if(
    trim($_POST[$validate[$i]]) == ''){

                
    $valid false;

          }
    }

    if(
    $valid == true){

          include(
    'success.php');

    } else {

          echo 
    'Your form did not validate, please retry';


    I think that would be your best bet


  •  

    Posting Permissions

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