Setting up registration error messages with Javascript redirect

22 Aug

OK, got the WORKIT website to output error messages. Both for duplicate entries, as well as incomplete forms. This is how it was done.

1. Set up a PHP function in the header or someplace that outputs this Javascript redirect code upon error.

function redirRegister($str) {
?>
   <script language='Javascript'>window.location='http://www.wirelesskit.net/?page=register&errmsg=<? echo urlencode($str) ?>';</script>
<?
}
&#91;/sourcecode&#93;

2. Put this error message in the registration form page

&#91;sourcecode language="php"&#93;
if (isset($_GET&#91;'errmsg'&#93;) {
  $errmsg = $_GET&#91;'errmsg'&#93;;
  echo '
<b>ERROR</b>:' .  $errmsg . '
';
}

3. In the registration processing page, check for errors. When you come across an error, redirect to the registration form page which outputs the error:

if ($count > 0) {
  redirRegister("Username exists! Please select another username!");
} else {
  $result = query(); // Query to insert users; deleted for safety
  if ($result) {
    // Register the user
  } else {
    redirRegister("Error creating user! Please make sure to fill out all the fields.");
  }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: