I want to know what is wrong with the form in my code.
<?php if(isset($_POST['submit'])) { $arrayName = array("TeacherA", "TeacherB", "TeacherC" , "TeacherD", "TeacherE"); $minimum = 5; $maximum = 10; $name = $_POST['yourName']; $email = $_POST['yourEmail']; if(strlen($name) < $minimum) { echo "Your name should be longer than 5 characters"; } if(strlen($name) > $maximum) { echo "Your name should not be longer than 10 characters"; } if(!in_array($name,$arrayName)){ echo "Please do register with us before you can login"; } else { echo "Welcome!"; } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Blank</title> </head> <body> <form action="form.php" method="post"> <label>Your Name: </label> <input type="text" name="yourName"> <br> <label for="">Your E mail:</label> <input type="email" name="yourEmail" id=""><br> <!-- <textarea name="yourMessage" id="" cols="30" rows="10"></textarea><br> --> <input type="submit" name="submit" value="submit"> </form> </body> </html>
I ran the code locally, but couldn't get the results I wanted.
The result I want is that if I do not enter a name in the "Your Name" field, the result should be displayed:
Please register before logging in
If the "Your Name" field is empty, its
strlen($name)
should be0
, the first if statement is true, andYour name should be longer than 5 characters
You can try this:i used
empty()
to test if the field empty and i used if-else statements because i want the script to stop if it finds a 'true' condition in your script you can usereturn;
but that will exit the rest of your scripthave a nice code :)