sphinx Posted September 24, 2011 Share Posted September 24, 2011 Hello, I'm using the following code to echo what fields have been left blank: if(isset($_POST['submit'])) { $emailconfirm = $_POST['emailconfirm']; $name = trim($_POST['name']); $visitor_email = trim($_POST['email']); $user_message = trim($_POST['message']); if(empty($name)){ $name1 .= "<br>Name is empty."; } if(empty($visitor_email)){ $email1 .= "<br>Email is empty."; } if(empty($user_message)){ $message1 .= "<br>User Message is empty."; } But what seems to happen, is once you submit the form with blank fields, the echo does show, however, when you click submit again, the form submits even though the fields were blank. Is their a way of blocking 'post' if the fields are blank? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/247802-only-submit-form-if-fields-have-content-in/ Share on other sites More sharing options...
Nethox Posted September 24, 2011 Share Posted September 24, 2011 $name1 .= "<br>Name is empty."; $email1 .= "<br>Email is empty."; $message1 .= "<br>User Message is empty."; if(isset($_POST['submit']) && trim($_POST['email']) != $email1 && trim($_POST['name']) != $name1 && trim($_POST['message']) != $message1) { $emailconfirm = $_POST['emailconfirm']; $name = trim($_POST['name']); $visitor_email = trim($_POST['email']); $user_message = trim($_POST['message']); if(empty($name)){ $name1 .= "<br>Name is empty."; } if(empty($visitor_email)){ $email1 .= "<br>Email is empty."; } if(empty($user_message)){ $message1 .= "<br>User Message is empty."; } I think something like this would do it. EDIT: Well actually you'd need to change the name of the variables at the top so they don't appear in the form lol... Quote Link to comment https://forums.phpfreaks.com/topic/247802-only-submit-form-if-fields-have-content-in/#findComment-1272485 Share on other sites More sharing options...
sphinx Posted September 25, 2011 Author Share Posted September 25, 2011 Parse error: syntax error, unexpected $end Quote Link to comment https://forums.phpfreaks.com/topic/247802-only-submit-form-if-fields-have-content-in/#findComment-1272553 Share on other sites More sharing options...
Nethox Posted September 25, 2011 Share Posted September 25, 2011 it's missing a } at the end Quote Link to comment https://forums.phpfreaks.com/topic/247802-only-submit-form-if-fields-have-content-in/#findComment-1272577 Share on other sites More sharing options...
AyKay47 Posted September 25, 2011 Share Posted September 25, 2011 i don't see in your code where you are actually disabling the form from being submitted.. if the criteria is not met, you will want to use "return false" so the form does not submit.. also, where do you initially set your values of $name1 $email1 $message1... from the code you posted, you are concatenating where it is not needed.. you will have to set each variable to an empty string before concatenating, if you havn't done so already.. Quote Link to comment https://forums.phpfreaks.com/topic/247802-only-submit-form-if-fields-have-content-in/#findComment-1272582 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.