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 2 of 2
  1. #1
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts

    Why Form Validation Isn't Working

    I can get through this and post blank comments at will; what's wrong with the validation?

    PHP Code:
    $comment1 $_POST['comment'];

      if (!isset(
    $_POST['comment'])) {
        
    header ("Location: http://www.site.com/error.php");
            exit();
      
        } elseif (empty(
    $comment1)) {
        
    header ("Location: http://www.site.com/error.php");
            exit();
      
        } else {

        
    $comment2 nl2br($comment1);
        
    $comment3 str_replace ('<a ','<a rel="nofollow" ',$comment2); 
            
    // then execute rest of formhandler...
           


  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Try this, check to see if submitted first then check to see if comment is empty.
    PHP Code:
    <?php 
    if(isset($_POST['submit']) && $_POST['submit'] == 'Submit'//if submitted 

        
    $comment1 stripslashes($_POST['comment']);
        if(empty(
    $comment1)) //if $comment1 is blank 
        

            
    header ("Location: http://www.site.com/error.php"); 
            exit(); 
        }
        else
        { 
            
    $comment2 nl2br($comment1); 
            
    $comment3 str_replace ('<a ','<a rel="nofollow" ',$comment2);  
            
    // then execute rest of formhandler... 
        
    }

    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    function isValid(form)
    {
        if(form.comment.value == '')
        {
            alert('You must enter a comment');
            form.comment.focus();
            return false;
        }
    }
    </script>
    </head>

    <body>
    <form action="<?php echo $_SERVER['SCRIPT_NAME']?>" method="post" onsubmit="return isValid(this)"> 
    Comment1 : <br /><textarea name="comment" cols="40" rows="10"></textarea><br />
    <input type="submit" name="submit" value="Submit" />
    </form> 
    </body>
    </html>
    I added in some JS validation because its a good idea to do both clientside and server side validation.
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  

    Posting Permissions

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