ppowell777 Posted Sunday at 03:13 PM Share Posted Sunday at 03:13 PM I have no idea why I am getting this error, but I am constantly getting this error when I am trying to do a default set onto $elem based on whether or not $_POST['firstName'] exists or not. Error: Quote PHP Fatal error: Uncaught Error: Value of type null is not callable in C:\inetpub\wwwroot\pages\feedback\includes\validation.php:32 Stack trace: #0 C:\inetpub\wwwroot\pages\feedback\includes\validation.php(8): isValidByCase(true, 0) #1 C:\inetpub\wwwroot\pages\feedback_process.php(31): isValidFeedback() #2 {main} thrown in C:\inetpub\wwwroot\pages\feedback\includes\validation.php on line 32 Code: $elem = (array_key_exists('firstName', $_POST) && isset($_POST['firstName']) && !is_null($_POST['firstName'])) ? $_POST['firstName'] : ''; if (!is_null($elem) || !$isValidName($elem)) { $isValid = false; array_push($erroredIDArray, 'firstName'); array_push($erroredIDIndexArray, $i); } Entire function: function isValidByCase($isValid, $i) { global $erroredIDArray; global $erroredIDIndexArray; $elem = ''; switch ($i) { case 0: // FIRST NAME $elem = (array_key_exists('firstName', $_POST) && isset($_POST['firstName']) && !is_null($_POST['firstName'])) ? $_POST['firstName'] : ''; if (!is_null($elem) || !$isValidName($elem)) { $isValid = false; array_push($erroredIDArray, 'firstName'); array_push($erroredIDIndexArray, $i); } break; case 1: // LAST NAME $elem = (array_key_exists('lastName', $_POST) && isset($_POST['lastName']) && !is_null($_POST['lastName'])) ? $_POST['lastName'] : ''; if (is_null($elem) || !$isValidName($elem)) { $isValid = false; array_push($erroredIDArray, 'lastName'); array_push($erroredIDIndexArray, $i); } break; case 2: // EMAIL $elem = (array_key_exists('email', $_POST) && isset($_POST['email']) && !is_null($_POST['email'])) ? $_POST['email'] : ''; if (!is_null($elem) || strlen(trim($elem) === 0 || strlen(trim($elem)) > EMAIL_MAX_LENGTH || !$isValidEmail($elem))) { $isValid = false; array_push($erroredIDArray, 'email'); array_push($erroredIDIndexArray, $i); } break; case 3: // SUBJECT $elem = (array_key_exists('subject', $_POST) && isset($_POST['subject']) && !is_null($_POST['subject'])) ? $_POST['subject'] : ''; if (!is_null($elem) || !is_numeric($elem) || !isValidSubject($elem)) { $isValid = false; array_push($erroredIDArray, 'subject'); array_push($erroredIDIndexArray, $i); } break; case 4: // QUESTION $elem = (array_key_exists('question', $_POST) && isset($_POST['question']) && !is_null($_POST['question'])) ? $_POST['question'] : ''; if (!is_null($elem) || !isValidQuestion($elem)) { $isValid = false; array_push($erroredIDArray, 'question'); array_push($erroredIDIndexArray, $i); } break; default: break; } return $isValid; } I am checking for everything I can think of: 1) Is the key in $_POST? 2) Is $_POST[key] set? 3) Is $elem null? 4) Does $elem pass the sniff test in every single individual function which nominally checks for null + emptiness? I don't know what else to do, and the error is persistent. Please help Thanks Quote Link to comment https://forums.phpfreaks.com/topic/330150-uncaught-error-value-of-type-null-is-not-callable/ Share on other sites More sharing options...
Solution Barand Posted Sunday at 03:58 PM Solution Share Posted Sunday at 03:58 PM if (!is_null($elem) || !$isValidName($elem)) { ^ ??? Quote Link to comment https://forums.phpfreaks.com/topic/330150-uncaught-error-value-of-type-null-is-not-callable/#findComment-1658112 Share on other sites More sharing options...
ppowell777 Posted Sunday at 04:04 PM Author Share Posted Sunday at 04:04 PM Sigh. Way too easy and I completely missed it Quote Link to comment https://forums.phpfreaks.com/topic/330150-uncaught-error-value-of-type-null-is-not-callable/#findComment-1658113 Share on other sites More sharing options...
mac_gyver Posted Sunday at 04:43 PM Share Posted Sunday at 04:43 PM 1 hour ago, ppowell777 said: I am checking for everything I can think of this is unnecessary and is hiding simple typo mistakes. except for unchecked checkbox/radio fields, all other fields will be set/will exist after a form has been submitted. after you have detected that a post method form has been submitted, these 'always set' fields will be set, regardless of what they contain. you only need check if a field is set for checkbox/radio fields. your post method form processing code should - detect if a post method form was submitted - if($_SERVER['REQUEST_METHOD'] === 'POST'). detect if there is $_POST (or $_FILES) data. there may not be if the total size of the submitted form data is greater than the post_max_size setting. keep the form data as a set in a php array variable, then operate on elements in this array variable throughout the rest of the code. trim all the input data, mainly so that you can detect if all white-space characters were entered. validate all inputs separately, storing user/validation errors in an array using the field name as the array index. after the end of the validation logic, if there are no errors (the array holding the user/validation errors is empty), use the submitted form data. after using the form data, if there are no errors, redirect to the exact same URL of the current page to cause a get request for that page. this will prevent the browser from trying to resubmit the form data should that page get browsed back to or reloaded. if you want to dynamically validate and process form data, and dynamically produce the corresponding form, create an array with the expected form fields, using the field name as the array index, with an array for each field with a label element, field data type, validation rules, and processing rules. you can then loop over this defining array and call general-purpose code to handle each field. Quote Link to comment https://forums.phpfreaks.com/topic/330150-uncaught-error-value-of-type-null-is-not-callable/#findComment-1658117 Share on other sites More sharing options...
gizmola Posted 2 hours ago Share Posted 2 hours ago Looking at your code, you should consider the application of DRY (Don't repeat yourself). The entire switch statement, is literally the same code in every case, other than the field name. global $erroredIDArray; global $erroredIDIndexArray; $elem = ''; switch ($i) { case 0: // FIRST NAME $elem = (array_key_exists('firstName', $_POST) && isset($_POST['firstName']) && !is_null($_POST['firstName'])) ? $_POST['firstName'] : ''; if (!is_null($elem) || !$isValidName($elem)) { $isValid = false; array_push($erroredIDArray, 'firstName'); array_push($erroredIDIndexArray, $i); } break; You're also using globals, which you should avoid here. I don't understand what the purpose of the numeric case #'s is, but it's also quite likely you don't need that at all if you create a function to handle this code. I left it out, as it seems to be duplicative. Just mechanically breaking your code out into a function would get you this: function isValid($key, array &$erroredIDArray) { $elem = ''; $elem = (array_key_exists($key, $_POST) && isset($_POST[$key]) && !is_null($_POST[$key])) ? $_POST[$key] : ''; if (!is_null($elem) || !$isValidName($elem)) { array_push($erroredIDArray, $key); // array_push($erroredIDIndexArray, $i); return false; } return true; } Quote Link to comment https://forums.phpfreaks.com/topic/330150-uncaught-error-value-of-type-null-is-not-callable/#findComment-1658171 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.