Setting up registration error messages with Javascript redirect
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>
<?
}
2. Put this error message in the registration form page
if (isset($_GET['errmsg']) {
$errmsg = $_GET['errmsg'];
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.");
}
Categories: php
javascript
