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 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2008
    Posts
    32
    Thanks
    3
    Thanked 0 Times in 0 Posts

    php email form checks required fields but still emails if they are empty

    I have searched all over for the solution to my problem!

    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    ini_set('display_errors'TRUE); 


    if(isset(
    $_POST['Submit'])){

    $myemail  "myemail@email.com";
    $subject "Update Resident Information";


    $name check_input($_POST['name'], "Enter your name");
    $address  check_input($_POST['address'], "Enter your address");
    $city    check_input($_POST['city'], "Enter your city");
    $state    check_input($_POST['state'], "Enter your state");
    $zip    check_input($_POST['zip'], "Enter your zip");
    $home    check_input($_POST['home'], "Enter your home phone number");
    $work    check_input($_POST['work'], "Enter your work phone number");
    $mobile    check_input($_POST['mobile'], "Enter your mobile phone number");
    $email    check_input($_POST['email'], "Enter your email address");

    $emergname check_input($_POST['emergname']);
    $emergaddress  check_input($_POST['emergaddress']);
    $emergcity    check_input($_POST['emergcity']);
    $emergstate    check_input($_POST['emergstate']);
    $emergzip    check_input($_POST['emergzip']);
    $emerghome    check_input($_POST['emerghome']);
    $emergwork    check_input($_POST['emergwork']);
    $emergmobile    check_input($_POST['emergmobile']);
    $emergemail    check_input($_POST['emergemail']);


    /* If e-mail is not valid show error message */
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/"$email))
    {
        
    show_error("E-mail address not valid");
    }



    $message "Hello!

    An update to resident information has been received from:

    Your Information
    Name: $name
    Address: $address
    City: $city
    State: $state
    Zip: $zip
    Home Phone Number: $home
    Work Phone Number: $work
    Mobile Phone Number: $mobile
    Email Address: $email

    Emergency Contact Information
    Name: $emergname
    Address: $emergaddress
    City: $emergcity
    State: $emergstate
    Zip: $emergzip
    Home Phone Number: $emerghome
    Work Phone Number: $emergwork
    Mobile Phone Number: $emergmobile
    Email Address: $emergemail
    "
    ;

    mail($myemail$subject$message);
    }


    function 
    check_input($data$problem='')
    {
        
    $data trim($data);
        
    $data stripslashes($data);
        
    $data htmlspecialchars($data);
        if (
    $problem && strlen($data) == 0)
        {
            
    show_error($problem);
        }
        return 
    $data;
    }

    function 
    show_error($myError
    {
    ?>
    Then I echo $myError in my content. It returns the error that is listed, ie Enter your name, yet it still sends an email with the required fields as blank. How can I make this not send the email until all required fields are filled out?
    "Loo loo loo, I've got some apples, loo loo loo, you've got some, too!" - Butters

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    One option:

    1) set a boolean that denotes whether any of the user inputs is invalid or not.

    PHP Code:
    $isDataValid true
    2) then as you validate each input, if any input is invalid set $isDataValid = false;

    3) then when you want to send the email

    PHP Code:
    if($isDataValid) {
        
    mail(.............);


  • #3
    New Coder
    Join Date
    Feb 2008
    Posts
    32
    Thanks
    3
    Thanked 0 Times in 0 Posts
    like this? i thought that's what the check_input is for. i'm confused....

    PHP Code:
    if ($name == TRUE)
    { echo 
    "thank you";}
    else
    {echo 
    "complete the field";} 
    "Loo loo loo, I've got some apples, loo loo loo, you've got some, too!" - Butters

  • #4
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    I was just describing the general concept based on what I normally do.

    Also, it looks like you are just checking if the input has zero length or not which is not really sufficient. For example, if I entered qwerty as a phone number your check_input() would accept it as valid. You really need to check if each input has any invalid characters in it. But that is beside the point.

    What you could do, is set $isDataValid = true; before any validation.

    Then if your check_input() detects any invalid input data, the function then sets $isDataValid = false; and display any error message. ( Normally I store error messages in an array and then display them at the end of the validation.)

    Then when you want to send the email, you first check if $isDataValid is true or false.
    Last edited by bullant; 06-01-2011 at 05:59 AM.

  • #5
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Or something like this pseudo code maybe? Normally I have a php class to do the validation, but using procedural coding for simplicity -

    PHP Code:
    <?php
    $name 
    trim($_POST['txtName']);
    $phone trim($_POST['txtPhone']);

    $errors = array();

    if(!
    chkName($name)) {
        
    $errors[] = 'Invalid name';
    }

    if(!
    chkPhone($phone)) {
        
    $errors[] = 'Invalid phone no.';
    }

    if(
    count($errors) > 0){ //errors occurred
        
    foreach($errors as $str) {
            echo 
    $str.'<br />';
        }
    } else { 
    //all input data is valid
        
    mail(....); //send email
    }

    function 
    chkName($name){
        
    //returns tru or false
    }
    function 
    chkPhone($phone){
        
    //returns tru or false
    }
    ?>

  • #6
    New Coder
    Join Date
    Feb 2008
    Posts
    32
    Thanks
    3
    Thanked 0 Times in 0 Posts
    This method is instead of check_input? it does make more sense...

    Quote Originally Posted by bullant View Post
    Or something like this pseudo code maybe? Normally I have a php class to do the validation, but using procedural coding for simplicity -

    PHP Code:
    <?php
    $name 
    trim($_POST['txtName']);
    $phone trim($_POST['txtPhone']);

    $errors = array();

    if(!
    chkName($name)) {
        
    $errors[] = 'Invalid name';
    }

    if(!
    chkPhone($phone)) {
        
    $errors[] = 'Invalid phone no.';
    }

    if(
    count($errors) > 0){ //errors occurred
        
    foreach($errors as $str) {
            echo 
    $str.'<br />';
        }
    } else { 
    //all input data is valid
        
    mail(....); //send email
    }

    function 
    chkName($name){
        
    //returns tru or false
    }
    function 
    chkPhone($phone){
        
    //returns tru or false
    }
    ?>
    "Loo loo loo, I've got some apples, loo loo loo, you've got some, too!" - Butters


  •  

    Posting Permissions

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