PHP newb here. What is the best way to go about checking that all the required fields of a form are filled out and give an error if they aren't? I've been working on the following simple form and the way I have it, it gives the "Not all fields are filled out" error the moment the page is loaded instead of only on form submission.
Question
jordan.
Hi guys.
PHP newb here. What is the best way to go about checking that all the required fields of a form are filled out and give an error if they aren't? I've been working on the following simple form and the way I have it, it gives the "Not all fields are filled out" error the moment the page is loaded instead of only on form submission.
<?php if(!empty($_POST['name']) && !empty($_POST['review'])) { $name = stripslashes($_POST['name']); $br = nl2br($_POST['review']); $review = stripslashes($br); $msg = "<div class='review'><strong>" . $name . "</strong> said:<br />\n" . $review . "</div>\n\n"; $fp = fopen ("reviews.php", "a"); if ($fp) { fwrite ($fp, $msg); fclose ($fp); $status = "<span class='success'>Success</span>"; } } else { $status = "<span class='error'>You haven't filled in all the fields you twit.</span>"; } include("reviews.php"); echo $status; ?>I know it must be something super simple that I am missing, my brain just isn't firing on all cylinders tonight.
Also, any further advice on how to improve the rest of the script is much appreciated.
Cheers,
Jordan
Link to comment
Share on other sites
11 answers to this question
Recommended Posts