0

trying to do validation for email using filter_validate_email. and use meth post. however, I get an error that

Notice: Undefined index: email in H:\XAMPP\htdocs\PHP-Walkthrough-Completed\register.php on line 68

E-mail is not valid

<input type="text" class="form-control" id="email" name="email" required placeholder="Type your email"><span id="emailError"></span>
<?php
  if(!filter_var($email, FILTER_VALIDATE_EMAIL))
  {
     echo("E-mail is not valid");
  }
  else
  {
     echo("E-mail is valid");
  }     
?>     
3
  • 1
    How you're getting $email it is obviously not defined anywhere. Commented Mar 29, 2019 at 5:10
  • 1
    Why not use type="email" in the input? That way you get a first filter of the input Commented Mar 29, 2019 at 5:40
  • assign value or define $email like $email = $_POST["email"]; befor using filter_var($email, FILTER_VALIDATE_EMAIL) Commented Mar 29, 2019 at 6:04

1 Answer 1

2

On same page you are posting data & checking posting data validation.To achieve this you have to ad post condition :

<?php
if(isset($_POST['email']))
{
  $email = $_POST['email'];
  if(!filter_var($email, FILTER_VALIDATE_EMAIL))
  {
     echo("E-mail is not valid");
  }
  else
  {
     echo("E-mail is valid");
  }     
}
?>    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.