For context: I am busy using javascript for checking fields in a form. The problem is to work around being unable to retrieve data from the database. All the checks except for making sure its a unique(not used in db) username are done. I looked around quite a while on the web, but the only answer that came close was this: "until you start needing the php page in question to return a value of some sort. in that case, just use the YUI connection manager or something of that nature". Since php is server side, when the button is clicked, can the php then be called, as there is no point in processing the php until there is text in the username field.
Here is what I have done. When pressing the submit button, a js function is called. The checking works.
<script type="text/javascript" src="regform_checks.js"></script>
<h1>Welcome to Registration</h1>
<form name="register" action="add_user.php" onsubmit="return validateForm()" method="post">
<table>
<tr>
<td>Choose a username*</td>
<td><input type = "text" name="profile_id"/></td>
</tr>
This above is in my registration.php The below is my .js file:
function validateForm()
{
var username=document.forms["register"]["profile_id"].value;
var pw1=document.forms["register"]["profile_password"].value;
var pw2=document.forms["register"]["profile_password2"].value;
var invalid = " "; // Invalid character is a space
var minLength2 = 3; // Minimum username length
var minLength = 6; // Minimum password length
var x=document.forms["register"]["profile_email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
//check for a value in username field
if (username==null || username=="")
{
alert("Please enter a username");
return false;
}
// check for username minimum length
if (document.register.profile_id.value.length < minLength2)
{
alert('Your username must be at least ' + minLength2 + ' characters long.');
return false;
}
// check for a value in both fields.
if (pw1 == "" || pw2 == "")
{
alert("Please enter your password in the Password and Re-enter password areas.");
return false;
}
// check for password minimum length
if (document.register.profile_password.value.length < minLength)
{
alert('Your password must be at least ' + minLength + ' characters long. Try again.');
return false;
}
// check for password consistency
if (pw1 != pw2)
{
alert ("The passwords you have entered are not the same. Please re-enter your password.");
return false;
}
//check for valid email address
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a valid e-mail address");
return false;
}
// check for spaces
if (document.register.profile_id.value.indexOf(invalid) > -1)
{
alert("Sorry, spaces are not allowed in your username.");
return false;
}
else if (document.register.profile_password.value.indexOf(invalid) > -1)
{
alert("Sorry, spaces are not allowed in your password.");
return false;
}
else if (document.register.profile_email.value.indexOf(invalid) > -1)
{
alert("Sorry, spaces are not allowed in your email.");
return false;
}
else
{
return true;
}
}
What I have been unable to do: set a js variable as follows: var username_check=profile_id_check.php; This file looks like this:
$query = "select profile_id from profile";
$result = mssql_query($query);
$names = array();
for ($count=0; $row=@mssql_fetch_array($result); $count++)
$names[$count]=$row;
$idtest= True;
foreach ($names as $name)
{
if($name[profile_id]==$profile_id)
return false;
print "$name[profile_id]";
}
?>
The php code is still in progress of making, depending on what it needs done I realize I dont need the $idtest, as the if statement returns a boolean value. That last print statement just prints all the profile names(so I can be sure the code works, and it does). And its the profile name entered into the form that needs to be compared with all the profile names in the database to ensure it is unique. The endresult the way I see it: If username is unique, give no error or warning, if it isnt, the js must then... which I know how to code. Im open to suggestions that turns away from this method, though my knowledge is restricted to php, some js. (and obviously html) Thanks in advance.