www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] Form PHP doesn't return false if a required field is empty

  1. #1
    Join Date
    Jul 2008
    Posts
    36

    resolved [RESOLVED] Form PHP doesn't return false if a required field is empty

    I'm not too familiar with forms and php so I grabbed a template from an online source. My form works as in it submits the info to the correct email but my required fields are supposed to give an error if they are not filled in or incorrect and they aren't doing that. I'm not sure how to fix this because it is a foreign language to me. Here is the url I need help with. Please help me!

    http://creativcat.com/projects/rapidweb/catalog.html

    Here is the php I am using:

    <?php

    $to="email";

    $header="From: ".$_POST['email'] . "\r\n";
    $header .= 'MIME-Version: 1.0' . "\r\n";
    $header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    $subject="Rapid Labels Catalog Order Form";

    $body='<html><body>
    '.$_POST['fname'].'
    '.$_POST['lname'].' <br>
    '.$_POST['email'].' <br>
    '.$_POST['address'].' <br>
    '.$_POST['city'].' <br>
    '.$_POST['state'].' <br>
    '.$_POST['zip'].' <br>
    '.$_POST['phone'].' <br><br>
    '.$_POST['comments'].'
    </body></html>';

    mail($to,$subject,$body,$header);

    ?>

  2. #2
    Join Date
    Aug 2006
    Location
    Michigan
    Posts
    1,046
    foreach $_POST element you shold validate if it is empty and conforms to what you want

    PHP Code:
    // Could use for first and last name
    function validate_name($name)
    {
       if(empty(
    $name))
       {
          return 
    false;
       }
       else if(
    preg_match("^/[a-zA-Z/+$"$name)
       {
          return 
    true;
       }
       else
       {
          return 
    false;
       }
    }

    if(!
    validate_name($_POST['fname'])
    {
       echo 
    "Please fill in your first name<br />";

    haven't testing this but something to get you started

  3. #3
    Join Date
    Jul 2008
    Posts
    36
    Thanks Phill but I am still confused. This is what I was told to add to the script to get the fields to return false so maybe this is what I need to be fixing.

    function isEmpty(elem, helperMsg)
    {
    if(elem.value.length == 0)
    {
    alert(helperMsg);
    elem.focus();
    return false;
    }
    else
    {return true;}
    }

    function validate_email(field,alerttxt)
    {
    with (field)
    {
    atposition=value.indexOf("@")
    dotposition=value.lastIndexOf(".")

    if (atposition<1||dotposition-atposition<2)
    {alert(alerttxt);return false}
    else {return true}
    }
    }

    function validate_form(thisform)
    {
    with (thisform)
    {
    if (isEmpty(document.getElementById('fname'), 'Please enter a first
    name')==false)
    {fname.focus();return false}

    if (isEmpty(document.getElementById('lname'), 'Please enter a last
    name')==false)
    {lname.focus();return false}

    if (isEmpty(document.getElementById('email'), 'Please enter an email
    address')==false)
    {email.focus();return false}

    if (isEmpty(document.getElementById('address'), 'Please enter a street
    address')==false)
    {address.focus();return false}

    if (isEmpty(document.getElementById('city'), 'Please enter a city
    ')==false)
    {city.focus();return false}

    if (isEmpty(document.getElementById('state'), 'Please enter a state
    ')==false)
    {state.focus();return false}

    if (isEmpty(document.getElementById('zip'), 'Please enter a zip code
    ')==false)
    {zip.focus();return false}

    if (isEmpty(document.getElementById('comments'), 'Please enter a
    comment or question')==false)
    {comments.focus();return false}

    if (validate_email(email,"Please enter a valid email
    address")==false)
    {email.focus();return false}
    }
    }

  4. #4
    Join Date
    Aug 2006
    Location
    Michigan
    Posts
    1,046
    this looks like JavaScript

    Code:
    function isEmpty(elem, helperMsg)
    {
    if(elem.value.length == 0)
    {
    alert(helperMsg);
    elem.focus();
    return false;
    }
    else
    {return true;}
    }
    
    function validate_email(field,alerttxt)
    {
    with (field)
    {
    atposition=value.indexOf("@")
    dotposition=value.lastIndexOf(".")
    
    if (atposition<1||dotposition-atposition<2)
    {alert(alerttxt);return false}
    else {return true}
    }
    }
    
    function validate_form(thisform)
    {
    with (thisform)
    {
    if (isEmpty(document.getElementById('fname'), 'Please enter a first
    name')==false)
    {fname.focus();return false}
    
    if (isEmpty(document.getElementById('lname'), 'Please enter a last
    name')==false)
    {lname.focus();return false}
    
    if (isEmpty(document.getElementById('email'), 'Please enter an email
    address')==false)
    {email.focus();return false}
    
    if (isEmpty(document.getElementById('address'), 'Please enter a street
    address')==false)
    {address.focus();return false}
    
    if (isEmpty(document.getElementById('city'), 'Please enter a city
    ')==false)
    {city.focus();return false}
    
    if (isEmpty(document.getElementById('state'), 'Please enter a state
    ')==false)
    {state.focus();return false}
    
    if (isEmpty(document.getElementById('zip'), 'Please enter a zip code
    ')==false)
    {zip.focus();return false}
    
    if (isEmpty(document.getElementById('comments'), 'Please enter a
    comment or question')==false)
    {comments.focus();return false}
    
    if (validate_email(email,"Please enter a valid email
    address")==false)
    {email.focus();return false}
    }
    }
    I like PHP server side validation instead. if you want JavaScript help you should post in their forum.

    Also when do you call the JavaScript validation, on submit? in your PHP code I only see a mail function.

  5. #5
    Join Date
    Jul 2008
    Posts
    36
    I have no idea.. like I said I just copied this off an online source. I think I got in over my head with all this php and javascript talk. Thanks for you help though.

    If you have a better reference, let me know.

  6. #6
    Join Date
    Aug 2006
    Location
    Michigan
    Posts
    1,046
    Ok it looks like what you are trying to do is add some code to validate your input.

    On the page with the form there should be a submit button. in the code where the submit button is there should be a onclick method, something like onclick="validate_form(this.form)"

    if not add it and see what it does

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles