rEhSi_123 Posted September 17, 2009 Share Posted September 17, 2009 Hello Everbody 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 Any suggestions please advice! Quote Link to comment https://forums.phpfreaks.com/topic/174616-php-required-field-validation/ Share on other sites More sharing options...
rille95 Posted September 17, 2009 Share Posted September 17, 2009 if a field is empty you need to make the script to die(); or exit; Quote Link to comment https://forums.phpfreaks.com/topic/174616-php-required-field-validation/#findComment-920240 Share on other sites More sharing options...
mikesta707 Posted September 17, 2009 Share Posted September 17, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/174616-php-required-field-validation/#findComment-920243 Share on other sites More sharing options...
rEhSi_123 Posted September 17, 2009 Author Share Posted September 17, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/174616-php-required-field-validation/#findComment-920254 Share on other sites More sharing options...
mikesta707 Posted September 17, 2009 Share Posted September 17, 2009 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/174616-php-required-field-validation/#findComment-920256 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.