trg86 Posted June 29, 2017 Share Posted June 29, 2017 Hi there! I am having s very small portion of my form processing script that is not working and I can't quite put my finger on what is going on. All of the validation works fine, the only problem is to avoid processing a blank form, i.e. not completing any fields and clicking 'submit' This is the part in my script that checks if any or all of the fields are blank and if they are it throws the error I have setup, if they are not blank (completed fields) then it continues on in processing the rest of the script. // Check For Any Null Values, If Null Then Throw Error if( !($name=='') && !($email=='') && !($phone_number=='') && !($contact_method=='') && !($phone_consent=='') && !($best_time=='') && !($referrer=='') && !($message=='')) { $mailError = ""; } else { THIS IS WHERE MY SCRIPT CONTINUES ON IF ALL OF THE FORMS ARE COMPLETED AND HAVE PASSED VALIDATION IT PROCESSING THIS SECTION EVEN IF THE FIELDS ARE BLANK As I have mentioned, it is processing an all blank field anyway and I am not quite sure why....I am a little stumped. Thanks in advance for your help! Quote Link to comment Share on other sites More sharing options...
dalecosp Posted June 29, 2017 Share Posted June 29, 2017 (edited) EDIT: the whole thing is maybe a tad convoluted. You could type coerce: if ($name && $email && $phone_number && $contact_method && $phone_consent && $best_time && $referrer && $message) { $error_msg = ''; } else { handle_error_with_a_function_or_exception(); }You could check for empty using OR: if (empty($name) || empty($email) || empty($phone_number) || empty($contact_method)) { bad(); } else { good(); }Also, end-users of the world would love it if you do them a favor and verify the form on the client-side prior to submission. Edited June 29, 2017 by dalecosp Quote Link to comment Share on other sites More sharing options...
trg86 Posted June 29, 2017 Author Share Posted June 29, 2017 Thank you for quick reply. I just tested your suggestion, but unfortunately it is still processing a completely blank form. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted June 29, 2017 Share Posted June 29, 2017 (edited) Your logic is off. You're checking if all fields are empty, otherwise you accept the request. To check if any field is empty, you need the logical OR. But the code generally doesn't make a lot of sense. The phrasing is weird, you don't tell the user what's actually wrong, and you haven't thought of the case that the whole parameter is missing. A more useful approach: <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { // collect error messages $errors = []; // check if all required fields are present and not empty $required_params = ['name', 'email', 'phone_number', 'contact_method', /* ... */]; foreach ($required_params as $param) { if (!isset($_POST[$param])) { // the parameter doesn't exist at all; this could be a client or a server error $errors[] = 'Missing parameter: '.$param; trigger_error('Missing POST parameter: '.$param, E_USER_NOTICE); } elseif (trim($_POST[$param]) == '') { // the value is empty $errors[] = 'Empty parameter: '.$param; } } if ($errors) { // handle errors } else { // continue } } Of course you should consider a more user-friendly error handling like going back to the form and displaying the error messages right next to the fields. Edited June 29, 2017 by Jacques1 Quote Link to comment Share on other sites More sharing options...
trg86 Posted June 29, 2017 Author Share Posted June 29, 2017 Thank you DaleCosp! My issue is now resolved by way of your suggestion, thank you for the help! SOLVED! Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 29, 2017 Share Posted June 29, 2017 Why don't you try echoing out your fields when you get the error message so you can check what is wrong? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted June 30, 2017 Share Posted June 30, 2017 trg86: You must be fucking kidding. You posted this exact code last week, got a detailed answer from mac_gyver, completely ignored it, then asked the question again, got another detailed answer, again ignored it, and now you have the same garbage code you had before. How many more years do you want to spend running in circles? It looks like you've been doing that in the last five years. Quote Link to comment 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.