Hasitha
Hasitha

Reputation: 67

Unable to call the function I declared

Iam tryin to call function chkEmailInput() with a parameter of $email. but I get the error "Fatal error: Call to undefined function chkEmailInput()"

if('registerUser' == $type = filter_input(INPUT_POST, 'type')){
//echo $type."<br>"; 

    $email = $_POST['email'];
     echo $email."<br>";
    //Gets external variable, optionally filters
     $email = filter_input(INPUT_POST, 'email');

chkEmailInput(); 

function chkEmailInput($email){
  if($email_val = filter_input(INPUT_POST, $email,FILTER_SANITIZE_EMAIL)){
      echo $email_val."<br>";
       if($email = filter_var($email_val, FILTER_VALIDATE_EMAIL)){
            echo 'EMAIL OK'."<br>";
            echo $email."<br>"; 
       }
       else{
            echo 'VALIDATE Fail'."<br>";
            echo $email."<br>";   
       }
    }
    else{
        echo"SANITIZE Fail";
        echo $email."<br>"; 
    }
}

Upvotes: 0

Views: 87

Answers (2)

Meloman
Meloman

Reputation: 3712

In your chkEmailInput definition you specify $email as argument. So when you need to call it with that arguemnt :

chkEmailInput($email);

and it is better if you declare the function outside the if statement.

if('registerUser' == $type = filter_input(INPUT_POST, 'type')){
    //echo $type."<br>"; 

    $email = $_POST['email'];
    echo $email."<br>";

    //Gets external variable, optionally filters
    $email = filter_input(INPUT_POST, 'email');

    chkEmailInput($email); 

}

function chkEmailInput($email){

    if($email_val = filter_input(INPUT_POST, $email,FILTER_SANITIZE_EMAIL)) {
        echo $email_val."<br>";

        if($email = filter_var($email_val, FILTER_VALIDATE_EMAIL)) {
            echo 'EMAIL OK'."<br>";
            echo $email."<br>"; 
        }

        else {
            echo 'VALIDATE Fail'."<br>";
            echo $email."<br>";   
        }
    }

    else {
        echo"SANITIZE Fail";
        echo $email."<br>"; 
    }

}

Upvotes: 2

Hasitha
Hasitha

Reputation: 67

Function definition should appear prior to function calling. In here function has been called before function declaring. So function is unable to find the function definition.

Upvotes: 0

Related Questions