3

In Register.php, it contains a form.

<html>
<body>

<form action="Insert.php" method="post">
Email: <input type="text" name="email" /><br/>
Username: <input type="text" name="username" /><br/>
<input type="submit" name="Send Registration" value="Send Registration"/>
</form>

</body>
</html>

In Insert.php, it contain codes that do some validations.

<?php

if(!filter_var($_POST[email], FILTER_VALIDATE_EMAIL))
{
  exit("E-mail is not valid");
}
else
{
  if(!strlen($_POST[username])>=6)
  {
    exit("Username should be at least 6 characters");
  }
  else
  {
    // Continue other validations
  }
}

?>

When i enter an invalid email address a click on 'send registration', it will show the error message "E-mail is not valid". So, i tried to enter a valid email address and key in 5 or lesser characters in the username field and click on 'send registration'. But it doesn't show the error message.

May i know what is wrong with my codes?
Sorry i am a beginner in programming and php.
Thanks in advance!

Lloydworth
  • 743
  • 6
  • 20
  • 38

3 Answers3

3

Your logic is wrong:

if(!strlen($_POST[username])>=6)

Replace with: *also note the ' characters in the variable (The ! negator is replaced with the less than, double negative, this is easier to understand if you have to look through a lot of code, you may miss the "!"

if (strlen($_POST['username'])<6)

Hope this helps.

Also something you may want to consider: Get rid of "insert.php" and put the code from THAT into the same page as the form. You can do something like this:

Form: <form method="post" action="'.$self.'">

(put in the top of the page inside the PHP brackets)

$self   = $_SERVER['PHP_SELF'];
$username   = $_GET['username'];
$email     = $_GET['email'];

It depends on how you want it set up, use either POST or GET, I can't tell you which to use.

From here, you can manipulate your variables and whatever.

ionFish
  • 1,004
  • 1
  • 8
  • 20
2

If you need to check for 5 or less characters:


if(strlen($_POST["username"])<=5) {
  //show error
}


Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
2

Try this:

<?php

if(!filter_var($_POST[email], FILTER_VALIDATE_EMAIL))
{
  exit("E-mail is not valid");
}
else
{
  $len = strlen($_POST["username"]);
  if($len<=5) {
  {
    exit("Username should be at least 6 characters");
  }
  else
  {
    // Continue other validations
  }
}

?>
Mas User
  • 166
  • 1
  • 5
  • 17