Jump to content

PHP required field validation


rEhSi_123

Recommended Posts

Hello Everbody  8)

 

I am have php website in which I have decided to add Testimonial section where visitors can leave feedbacks...etc....

 

Everything seems to be working fine where I am able to enter the data into the MySQL database.

 

Anyways, basically I am trying to add a required field validator in order to prevent an empty form to be submitted into the database i.e. blank fields.

 

I googled the above topic where I have come across Javascript validators ...etc but I want to stay away from it because of very less experience and since there is a possibility of browers with no javascript support.

 

I have also tried the below:

 

if ($_POST['name']=="") {
Print("Please fill in all fields!<br>");
}
elseif ($_POST['email']=="")
{
Print("Please fill in all fields!!<br>");
}
elseif ($_POST['comment']=="")
{
Print("Please fill in all fields!!!<br>");
}
else
{
echo "Successful";
echo "<BR>";
echo "<a href='viewguestbook.php'>View guestbook</a>"; 
}

 

Where it displays a error message if the field is empty but blank data is still entered into the database :confused:

 

Any suggestions please advice!

Link to comment
https://forums.phpfreaks.com/topic/174616-php-required-field-validation/
Share on other sites

you have to exit() after one of the validation ifs or the script will just spit out the error message, but keep running.

for example

if ($_POST['name']=="") {
Print("Please fill in all fields!<br>");
exit();
}

 

you can also have the query execute in the else statement if you dont want to use exit

if a field is empty you need to make the script to die(); or exit;

you have to exit() after one of the validation ifs or the script will just spit out the error message, but keep running.

for example

if ($_POST['name']=="") {
Print("Please fill in all fields!<br>");
exit();
}

 

 

Thanks rille95 and mikesta707 for the correction as I should have know this..... ::)

 

Also mikesta707 what do you mean by this 'you can also have the query execute in the else statement if you dont want to use exit' sorry  :-[

 

if ($_POST['name']=="") {
Print("Please fill in all fields!<br>");
}
elseif ($_POST['email']=="")
{
Print("Please fill in all fields!!<br>");
}
elseif ($_POST['comment']=="")
{
Print("Please fill in all fields!!!<br>");
}
else
{
//do the query here
//then output the stuff below
echo "Successful";
echo "<BR>";
echo "<a href='viewguestbook.php'>View guestbook</a>";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.