Ok I am fairly fairly new in PHP and I am trying to make a contact form. There are some fields which I need to be of certain characters.
if (!empty($_POST['title'])) {
$title = $_POST['title'];
}
elseif (strlen($title)>=1||strlen($title)<6)||strlen($title)>50 {
$error .= "Length of your Subject must be more than 6 characters. <br />";
}
else {
$error .= "Please enter the Subject. <br />";
}
$error stores all the errors that came while filling the form. At the end, the form checks and if no error is found, message is successfully sent.
What I want? I want to display the error stated in the elseif statement if the string length is less than 3 but greater or equal to 1. Error should also display if the length is greater than 50 characters.
If the field is empty then it should display the last error provided in else statement.
What's happening? Whenever I keep the field empty it shows the first error instead of second. And if I enter say just 1 character in the field, no error is shown.
I am bagging my head, I did a lot of trial and errors but nothing seems to work. It will be a huge help if someone can forward. This is my first project in PHP just because some of my readers want it and I have not learnt the language completely.
look you have jumbled the if else statements..... try it this way ,
if (!empty($_POST['title']))
{
$title = $_POST['title'];
if (strlen($title)>=1 && strlen($title)<6)||strlen($title)>50)
{
$error .= "Length of your Subject must be more than 6 characters. <br />";
}
}
else
{
$error .= "Please enter the Subject. <br />";
}