mike12255 Posted October 7, 2011 Share Posted October 7, 2011 I have a variable $form in the code below what I cant figure out how do it is call it and get the information it has out so I can display the errors: function login($subuser, $subpass, $subremember){ global $database, $form; //The database and form object /* Username error checking */ $field = "user"; //Use field name for username if(!$subuser || strlen($subuser = trim($subuser)) == 0){ $form->setError($field, "* Username not entered"); } else{ /* Check if username is not alphanumeric */ if(!preg_match("^([0-9a-z])*$^i", $subuser)){ $form->setError($field, "* Username not alphanumeric"); } } if ($database->usernameBanned($subuser)) { $form->setError($field, "*Your Account has been banned"); } /* Password error checking */ $field = "pass"; //Use field name for password if(!$subpass){ $form->setError($field, "* Password not entered"); } /* Return if form errors exist */ if($form->num_errors > 0){ return false; } /* Checks that username is in database and password is correct */ $subuser = stripslashes($subuser); $result = $database->confirmUserPass($subuser, md5($subpass)); /* Check error codes */ if($result == 1){ $field = "user"; $form->setError($field, "* Username not found"); } else if($result == 2){ $field = "pass"; $form->setError($field, "* Invalid password"); } /* Return if form errors exist */ if($form->num_errors > 0){ return false; } Quote Link to comment https://forums.phpfreaks.com/topic/248637-how-to-retrieve-this-variable/ Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 You will need to look at your form class and see what setError() is doing with the error data that is passed to it. Quote Link to comment https://forums.phpfreaks.com/topic/248637-how-to-retrieve-this-variable/#findComment-1276860 Share on other sites More sharing options...
mike12255 Posted October 7, 2011 Author Share Posted October 7, 2011 Sorry Ment to attach it, here it is: <? /** * Form.php * * The Form class is meant to simplify the task of keeping * track of errors in user submitted forms and the form * field values that were entered correctly. * * Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC) * Last Updated: August 19, 2004 */ class Form { var $values = array(); //Holds submitted form field values var $errors = array(); //Holds submitted form error messages var $num_errors; //The number of errors in submitted form /* Class constructor */ function Form(){ /** * Get form value and error arrays, used when there * is an error with a user-submitted form. */ if(isset($_SESSION['value_array']) && isset($_SESSION['error_array'])){ $this->values = $_SESSION['value_array']; $this->errors = $_SESSION['error_array']; $this->num_errors = count($this->errors); unset($_SESSION['value_array']); unset($_SESSION['error_array']); } else{ $this->num_errors = 0; } } /** * setValue - Records the value typed into the given * form field by the user. */ function setValue($field, $value){ $this->values[$field] = $value; } /** * setError - Records new form error given the form * field name and the error message attached to it. */ function setError($field, $errmsg){ $this->errors[$field] = $errmsg; $this->num_errors = count($this->errors); } /** * value - Returns the value attached to the given * field, if none exists, the empty string is returned. */ function value($field){ if(array_key_exists($field,$this->values)){ return htmlspecialchars(stripslashes($this->values[$field])); }else{ return ""; } } /** * error - Returns the error message attached to the * given field, if none exists, the empty string is returned. */ function error($field){ if(array_key_exists($field,$this->errors)){ return "<font size=\"2\" color=\"#ff0000\">".$this->errors[$field]."</font>"; }else{ return ""; } } /* getErrorArray - Returns the array of error messages */ function getErrorArray(){ return $this->errors; } }; ?> I assumed it would be something like $errors = getErrorArray(); echo $errors[0] but I couldnt get it to work either lol gonna keep trying though Quote Link to comment https://forums.phpfreaks.com/topic/248637-how-to-retrieve-this-variable/#findComment-1276880 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 $form->errors echo '<pre>' , print_r($form->errors,true) , '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/248637-how-to-retrieve-this-variable/#findComment-1276889 Share on other sites More sharing options...
mike12255 Posted October 7, 2011 Author Share Posted October 7, 2011 yeah I tried the errors thing as well since it says its an array of the errors but I couldnt figure out how to access that array there is one element so I tried echo $form->errors[0] and echo $form->errors(0) btw your code output: Array ( [user] => *Your Account has been banned ) Quote Link to comment https://forums.phpfreaks.com/topic/248637-how-to-retrieve-this-variable/#findComment-1276899 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 The errors array is not stored with numeric keys, that is why it was failing. Quote Link to comment https://forums.phpfreaks.com/topic/248637-how-to-retrieve-this-variable/#findComment-1276906 Share on other sites More sharing options...
mike12255 Posted October 7, 2011 Author Share Posted October 7, 2011 oh ok so I access it via user i take it, thanks. Thanks a lot for guiding me through this. Quote Link to comment https://forums.phpfreaks.com/topic/248637-how-to-retrieve-this-variable/#findComment-1276909 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.