SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Simple Form Validation

    Im trying to validate this form.... for some reason, when i dont fill out any fields, yet i have the password field right, $ok == "true" when it should be false if no fields are filled out... Can anyone find the problem?



    PHP Code:
    <?
    include("header.php"); 
    $reply "Please fill out the form below to add a new movie to the site.<br>";

    if(isset(
    $_POST['submit'])){
        
    $pass $_POST['pass'];
        
    $access "123";
        
    $ok "true";
        
        if(
    $pass != $access){
            
    $ok "false";
            
    $reply $reply."<br> - The password you entered is incorrect. Please enter the correct password to continue<br>";
        } else {
        
            
    $title $_POST['title'];
            
    $title str_replace(" ""_"$title);
            
    $year $_POST['year'];
            
    $syn $_POST['syn'];
            
    $blank "";
            
            function 
    validate($item){
                if(
    $item == $blank){
                    
    $ok "false";
                    
    $reply $reply."<br> - You need to fill out all of the required fields.<br>";
                }
            }
            
            
    validate("$title");
            
    validate("$year");
            
    validate("$syn");
        }
        
        if(
    $ok == "true"){
            
    $reply "<b>Your movie information has been added successfully!</b>";
        } else if (
    $ok != "true"){
            
    $reply $reply."<br> - Your movie info couldn't be added. Make sure you fill out all the required fields, and that the formats are right.<br>";
        }
    }
    ?>



    then it echo's the reply:

    <div id="reply">
    <p><strong>Form Guide<br />
    </strong><? echo $reply; ?></p>
    </div>

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Here is something for you to read: http://www.php.net/manual/en/languag...bles.scope.php

    Nevertheless, you might get away with empty here:

    Code php:
    $ok = !empty($title) && !empty($year) && !empty($syn);
     
    if(!$ok){
      $reply.="<br> - You need to fill out all of the required fields.<br>";
     
    }

    On a side note, don't quote true/false, you're not dealing with booleans anymore there.
    Saul

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Set your flag for error to False(0) at the start of validation, at any time if your field has an error set the flag to True(1), Do not set True or False with each field, the only way you will get an error = True is if the very last field is set to True....
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you need to validate email address you can use next code

    PHP Code:
    function valid_email($address)
    {
     
      if (
    ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$'$address))
        return 
    true;
      else 
        return 
    false;

    www.mangiaphoto.com - daily photo news
    www.serveradminblog.com - Open source tips and tricks


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
  •