I didn’t test or even complete it as you can see by some comments I added, but this is how I would do it just change a few things and put the two in one file. the form will load every time and when you submit you’re just reloading the page with the new _POST array now available for processing. What’s more you could even completely remove the javascript and validate the form with php and if the user doesn’t meet the criteria you can repopulate the fields, thats up to your preference however as it just mean more writing as you’ve already created the scripts for it.
Anyway hope this helps, if you have anymore questions ask away!
[php] <?php
session_start();
if ($_POST[‘adduserForm’]) {
if(!isset($_SESSION['SESS_LOGIN']) || $_SESSION['SESS_TYPE'] !='admin')// if session variable "login" does not exist.
{
echo '<script language="javascript">';
echo 'alert("Please login as ADMINISTRATOR to add a user");';
echo ' window.location.replace("index.html");';
echo '</script>';
//header(“location:login-form.php”); // Re-direct to login-form.php
} else {
include("config.php");
$login = mysql_real_escape_string($_POST['login']);
$password = mysql_real_escape_string($_POST['password']);
$type = mysql_real_escape_string($_POST['type']);
// For security purposes you don't want to store passwords in the database in plain text.
// Whats more by adding "salt" to the beginning and end of the password you'll get a boost
// to the security, when verifying password you would simple request the string from the
// database parse out the 'salt' then call back the md5() function. Depending on how secure
// you need it you can create your own algorithm for the 'salt' to make it harder to break
// especially as a common reaccuring string found will become obvious or not use it at all
// but should always at least encrypt it.
$password = ("INSERT RANDOM ALPHANUMERIC CHARACTERS" . md5($password) . "INSERT RANDOM ALPHANUMERIC CHARACTERS");
$checkformembers = mysql_query("SELECT * FROM members WHERE login='$login'");
if(mysql_num_rows($checkformembers) != 0)
{
echo '<script language="javascript">';
echo 'alert("Username already in use. Please try again.!" );';
//echo ' window.location.replace("adduser.php");';
echo '</script>';
} else {
$qry_add = " INSERT INTO members
(login, password,type )
VALUES ('$login', '$password', '$type') ";
$count = mysql_query("SELECT COUNT(login) FROM members WHERE login='$login'");
if($count==1)
{
echo "<font color=red> Duplicate Entry. Please Verify login</font>";
} else {
if($result=mysql_query($qry_add))
{
// echo '<script language="javascript">';
//echo 'alert("you have successfully added one user !" );';
// echo 'window.setTimeout("window.location.replace('adduser.php');",20);';
// echo 'window.setTimeout("window.location.replace('adduser.php'),20");';
echo "<br><font color=green size=+1 >you have successfully added one user ! <br>[ username = $login ] </font>" ;
//echo ' window.location.reload("adduser.php");';
// echo '/script>';
} else {
echo "<br><font color=red size=+1 >Problem in Adding !</font>" ;
echo "ERROR - unable to save new username and password!<br>";
$SQLError = "SQL ERROR: ".mysql_errno().". ".mysql_error()."<BR><BR>";
echo "$SQLError";
mysql_close();
}
}
}
}
echo "<BR><BR>"; // just to create a little space between anything sent prior to the form
}
?>
Add user
username |
|
password |
|
Select user type : |
Admin
Lab Assistant
Store Keeper
|
<tr>
<td></td>
<td><input type="submit" name="button" id="button" value="save"/></td>
</tr>
</form>
[/php]