Note that there are some explanatory texts on larger screens.

plurals
  1. POVariable being unset during new page load
    text
    copied!<p>I have a registration form on a website, the registration calls a function which accepts the $_POST values as arguments. While going through the usual checks, if an error is found it uses a function in the error message class which sets the error, and also increments the errorcounter variable. This function accepts two arguments: the field and the error message (stored in an array).</p> <pre><code> $errmsg-&gt;setError("firstname", "You must enter your name"); </code></pre> <p>setError() sets the error:</p> <pre><code> function setError($field, $errMsg){ $this-&gt;errors[$field] = $errMsg; } </code></pre> <p>The register function checks to see if the error counter has been increased, if not it registers the account, if yes then it redirects back to the register page. </p> <p>The register page uses a function in the errmsg class to show any errors:</p> <pre><code> $errmsg-&gt;showError("firstname"); // Argument being the field name. </code></pre> <p>This function should display any error messages that have been set:</p> <pre><code> function showError($field) { if (isset($this-&gt;errors[$field])) { return $this-&gt;errors[$field]; } else { return ""; } } </code></pre> <p>So, you can see if the array has the index defined it should display the value, if not, display nothing.</p> <p>The problem I'm having is that nothing is being displayed even when there is an error.</p> <p>I've tried echoing out $errmsg->errors['firstname']; in the register function if the error counter is > 0, and it displays the correct message. So I know the array key is being set, also if I change the else statement to return a string if the array key doesn't exist it also works fine. So for some reason the value is being set, then lost again once the page reloads. </p> <p>I also tried creating a function in the error message class to return the array which can be called by the register function when errors have been found:</p> <pre><code> function getErrorArray(){ return $this-&gt;errors; } </code></pre> <p>This also did nothing. To be 100% certain the array key exists at the time of running the register function I tried this:</p> <pre><code> if ($errors &gt; 0) { if(isset($errmsg-&gt;errors['firstname'])) { header("Location: ".$session-&gt;referrer); } else { header("Location: http://www.google.com"); } </code></pre> <p>As expected, if there is an error (the error I'm working on here is an empty string) it redirects back and should show the message and when the field contained a value it redirected to Google.</p> <p>I've also tried using:</p> <pre><code> if(array_key_exists($field,$this-&gt;errors)) </code></pre> <p>instead of:</p> <pre><code> if(isset($errmsg-&gt;errors['firstname'])) </code></pre> <p>I really don't understand why this isn't working, is it a scope issue? Should I maybe be storing this in $_SESSION? Any pointers would be greatly appreciated. Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload