DLR Posted December 17, 2009 Share Posted December 17, 2009 I have created a function to validate input. function validate_dimension($value,$name) { global $errors; $errors = array(); ......validation actions here - this all works fine - if I get an error it is captured like this . . . $errors[] = "Dimension value missing"; This is all fine and the $error is returned correctly. So that I can keep the error, I save it as a Session variable $_SESSION['errors'] = $errors; HOWEVER the next time I run the function (with the next dimension to be checked) , $_SESSION['errors'] is overwritten and the first error is lost. I have solved the problem by creating a new array - $all_errors - and adding $errors to $all_errors each time the function is run - but this seems clumsy - I do not need the function as I could just as easily wite a separate line of code for each dimension to be checked. Can anyone suggest a better way of doing this? Please? Quote Link to comment https://forums.phpfreaks.com/topic/185430-storing-results-of-a-function-previous-result-overwritten-with-new-result/ Share on other sites More sharing options...
.josh Posted December 17, 2009 Share Posted December 17, 2009 Okay so first off,, $_SESSION is already global scope, so there's no need to be creating this extra global $errors array (and you really shouldn't be side-stepping scope like that to begin with). Also, you mentioned returning $errors... well the point of returning something is to keep within scope, so I fear you may be again doing something iffy... And 3rd, you are saying that you are using a session variable to "keep" the errors...are you trying to pass the errors to another page? If this is all within one script page being executed, then there's no need for even that. What is the overall goal here? Are you simply looping through data, validating and returning all relevant errors? Your code should more or less look like this (just using $_POST in a foreach as an example...): foreach($_POST as $name => $value) { $errors[] = validate_dimension($value,$name); } function validate_dimension($value,$name) { // validate info. If there is an error... return "error message"; } You will then have a full array of all relevant errors. If you are passing it to another page, then just use $_SESSION['errors'][] instead of $errors[] in the foreach Quote Link to comment https://forums.phpfreaks.com/topic/185430-storing-results-of-a-function-previous-result-overwritten-with-new-result/#findComment-978920 Share on other sites More sharing options...
DLR Posted December 17, 2009 Author Share Posted December 17, 2009 Thank you. I really appreciate your assistance. I have learnt a lot. Perhaps you could assist me further? Some of the $_POST variables are permitted to be nul. But the validate function will mark any nul input as an error. So the fixed error message becomes inappropriate. I have tried to add a "flag" to the variable so I can permit a nul input, but cannot see a way to do this. Is there perhaps another elegant way to solve this? Many thanks for your time. Quote Link to comment https://forums.phpfreaks.com/topic/185430-storing-results-of-a-function-previous-result-overwritten-with-new-result/#findComment-978986 Share on other sites More sharing options...
teamatomic Posted December 17, 2009 Share Posted December 17, 2009 Not exactly sure what you want but maybe this will work. if (_$POST['var'] == ''){$_POST['var']=0;} HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/185430-storing-results-of-a-function-previous-result-overwritten-with-new-result/#findComment-978993 Share on other sites More sharing options...
.josh Posted December 17, 2009 Share Posted December 17, 2009 Thank you. I really appreciate your assistance. I have learnt a lot. Perhaps you could assist me further? Some of the $_POST variables are permitted to be nul. But the validate function will mark any nul input as an error. So the fixed error message becomes inappropriate. I have tried to add a "flag" to the variable so I can permit a nul input, but cannot see a way to do this. Is there perhaps another elegant way to solve this? Many thanks for your time. well i have no idea what the validation part of your function looks like... Quote Link to comment https://forums.phpfreaks.com/topic/185430-storing-results-of-a-function-previous-result-overwritten-with-new-result/#findComment-979147 Share on other sites More sharing options...
DLR Posted December 17, 2009 Author Share Posted December 17, 2009 Thanks, herewith the basic structure. <input type="text" name="width"> // this variable MUST have a value <input type="text" name="depth"> // this variable may be null //now validate inputs foreach($_POST as $name => $value) { $errors[] = validate_dimension($value,$name); } function validate_dimension($value,$name) { if(! isset($_POST[$name]) OR $_POST[$name] == "" OR $_POST[$name] == 0) { return "<span style='color:red;'>Dimension for " .$name . " required</span>"; } else { $_SESSION[$name] = htmlentities($value); } Is there a way to "flag" or "switch" the validation so that some variables may have a nul value and not show an error message? Quote Link to comment https://forums.phpfreaks.com/topic/185430-storing-results-of-a-function-previous-result-overwritten-with-new-result/#findComment-979265 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.