eugeniu Posted June 7, 2009 Share Posted June 7, 2009 I have a script that processes errors from a form and makes an array out of the errors. I have a line of code that submits what kind of error it is and what the error text is to a function, which then puts it in an array. This is the line of code: $form->setError($field, $badEmail); $field is "email" and $badEmail is the error text, defined on another included file. Now this my function: function setError($field, $errmsg) { $this->errors[$field] = $errmsg; $this->num_errors = count($this->errors); } Problem is, it outputs nothing. It seems to enter nothing into the array. The only way I can get it to work is by doing this: $form->setError($field, "Error goes here as a non-variable."); But I really don't want to have it done that way because I often have to place the same error in multiple places, which would make it a lot more helpful if I just had variables. Thanks for your time. Quote Link to comment https://forums.phpfreaks.com/topic/161270-solved-function-problem/ Share on other sites More sharing options...
jxrd Posted June 7, 2009 Share Posted June 7, 2009 What happens when you print_r() yourClass::$errors? Quote Link to comment https://forums.phpfreaks.com/topic/161270-solved-function-problem/#findComment-850971 Share on other sites More sharing options...
wildteen88 Posted June 7, 2009 Share Posted June 7, 2009 Where are you calling your setError method and how are the variables $field and $badEmail being defined? Quote Link to comment https://forums.phpfreaks.com/topic/161270-solved-function-problem/#findComment-850972 Share on other sites More sharing options...
nrg_alpha Posted June 7, 2009 Share Posted June 7, 2009 It's not outputting anything because all you have done is assign variables. You need to echo the error message (be it by a simple echo statement or some kind of error displaying class method). Quote Link to comment https://forums.phpfreaks.com/topic/161270-solved-function-problem/#findComment-850973 Share on other sites More sharing options...
eugeniu Posted June 7, 2009 Author Share Posted June 7, 2009 @jxrd: I'm sorry, I'm not following you. What would the exact code for that be? @wildteen88: Here's the full code for generating errors: $field = "email"; // Use field name for email if(!$subemail || strlen($subemail = trim($subemail)) == 0){ $form->setError($field, $noEmail); } else { /* Check if valid email address */ $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*" ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*" ."\.([a-z]{2,}){1}$"; if (!eregi($regex,$subemail)) { $form->setError($field, $badEmail); } $subemail = stripslashes($subemail); } The function is called on line 3 and 10. $noEmail and $badEmail are defined on a separate and included file called constants.php. This is what I have for those two: $ebefore = " <p class=\"error\">* "; $eafter = "</p>"; $noEmail = $ebefore . "No email has been entered." . $eafter; $badEmail = $ebefore . "You have entered an invalid email." . $eafter; @nrg_alpha: I'm echoing the errors with codes like this: <? echo $form->error("email"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/161270-solved-function-problem/#findComment-850976 Share on other sites More sharing options...
wildteen88 Posted June 7, 2009 Share Posted June 7, 2009 Is this code $field = "email"; // Use field name for email if(!$subemail || strlen($subemail = trim($subemail)) == 0){ $form->setError($field, $noEmail); } else { /* Check if valid email address */ $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*" ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*" ."\.([a-z]{2,}){1}$"; if (!eregi($regex,$subemail)) { $form->setError($field, $badEmail); } $subemail = stripslashes($subemail); } Wrapped in a function? If it is then you'll have to define the variables $noEmail and $badEmail as global within your function. Otherwise your variables wont be available to your function and this is why your script outputs no errors. You should read up on variable scope Quote Link to comment https://forums.phpfreaks.com/topic/161270-solved-function-problem/#findComment-850978 Share on other sites More sharing options...
eugeniu Posted June 7, 2009 Author Share Posted June 7, 2009 Thanks! Looks like that's exactly what I was doing wrong. Now I have one last question. Is is possible to shorten things up by having the error variables in some sort of array and just make the array global in the function? Quote Link to comment https://forums.phpfreaks.com/topic/161270-solved-function-problem/#findComment-850979 Share on other sites More sharing options...
nrg_alpha Posted June 7, 2009 Share Posted June 7, 2009 @nrg_alpha: I'm echoing the errors with codes like this: <? echo $form->error("email"); ?> I figured that you expected that code in your initial post to do the outputting (but now understand what you meant - I misread). In your newest posted code snippet, I would scrap ereg, as this will not be included within the core of PHP as of version 6. I would instead use preg (part of Perl Compatible regular expressions). Future proof your code while you can. Some online resources: regular expressions weblogtoolscollection php freaks regex resources php freaks regex tutorial Quote Link to comment https://forums.phpfreaks.com/topic/161270-solved-function-problem/#findComment-850989 Share on other sites More sharing options...
eugeniu Posted June 7, 2009 Author Share Posted June 7, 2009 Good idea. Why are they removing ereg? Quote Link to comment https://forums.phpfreaks.com/topic/161270-solved-function-problem/#findComment-850992 Share on other sites More sharing options...
nrg_alpha Posted June 7, 2009 Share Posted June 7, 2009 Good idea. Why are they removing ereg? ereg is part of POSIX (Portable Operating System Interface), and is generally slower than PCRE (granted, I never benchmarked this to find out personally.. but I read that it is). As well, PCRE offers additional functionality / flexibility that POSIX doesn't support. You can read the very top tip on this page that explains this. I also just noticed that note regarding POSIX in that same link: Note: As of PHP 5.3.0 this extension is deprecated, calling any function provided by this extension will issue an E_DEPRECATED notice. If that's not a sign, I don't know what is Quote Link to comment https://forums.phpfreaks.com/topic/161270-solved-function-problem/#findComment-851016 Share on other sites More sharing options...
eugeniu Posted June 7, 2009 Author Share Posted June 7, 2009 Ok, I see. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/161270-solved-function-problem/#findComment-851022 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.