Hi,
I'm working on a registration form and one of the checks I need to perform as the form is used is on the username. I need to ensure that it is not already in use. I am getting a little stuck. Here's the code involved (sorry there's quite a bit) and then I'll explain the problem.
HTML (relevant sample)
-
<div class="row">
-
<span class="label" id="usernamelabel">
-
<span class="warninglabel">Username:</span>
-
</span>
-
<span class="formw">
-
<input id="username" type="text" size="47" onchange="validateItem('usernamelabel',this.value,'Username:',4,false,5,true);" title="Username, for use on the Forum" />
-
(6-18 alpha numeric characters)
-
</span>
-
</div>
-
Javascript (3 functions involved)
-
function stateChanged()
-
{
-
if (goXMLHTTP.readyState==4 || goXMLHTTP.readyState=="complete")
-
{
-
document.getElementById(gcItemID).innerHTML=goXMLHTTP.responseText;
-
}
-
}
-
-
function GetXmlHttpObject()
-
{
-
if (window.XMLHttpRequest)
-
{
-
goXMLHTTP=new XMLHttpRequest()
-
}
-
else if (window.ActiveXObject)
-
{
-
goXMLHTTP=new ActiveXObject("Microsoft.XMLHTTP")
-
}
-
if (goXMLHTTP==null)
-
{
-
alert ("Browser does not support HTTP Request")
-
return
-
}
-
}
-
-
-
function validateItem(pcID,pcItem,pcDisplay,pnType,plPopulateExtra,pnNumberOfItems,plCheckDatabase)
-
{
-
var lcRegExp, llIsValid, llUseRegExp
-
llIsValid = false
-
llUseRegExp = false
-
switch (pnType)
-
{
-
case 1: // UK postcode
-
pcItem = pcItem.toUpperCase()
-
lcRegExp = '^[A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA$'
-
llUseRegExp = true
-
break;
-
case 2: // email address
-
lcRegExp = '^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$'
-
llUseRegExp = true
-
break;
-
case 3: // number of set length - basic telephone number check
-
lcRegExp = '^[0-9]{6,11}$'
-
llUseRegExp = true
-
break;
-
case 4: // username and password validation - alpha numeric characters 6-18 characters long
-
lcRegExp = '^[A-Za-z0-9]{6,18}$'
-
llUseRegExp = true
-
break;
-
}
-
-
if (llUseRegExp)
-
{
-
if (pcItem.match(lcRegExp))
-
{
-
if(plCheckDatabase)
-
{
-
GetXmlHttpObject() // sets the global variable
-
gcItemID = pcID
-
gcUrl = "../lib/datacheck.php?check=1&tocheck='" + pcItem + "'"
-
goXMLHTTP.onreadystatechange=stateChanged
-
goXMLHTTP.open("GET",gcUrl,true)
-
goXMLHTTP.send(null)
-
-
}
-
else
-
{
-
llIsValid = true
-
}
-
}
-
else
-
{
-
llIsValid = false
-
}
-
}
-
else
-
{
-
if (pcItem == null||pcItem == ""||pcItem.length < 2)
-
{
-
llIsValid = false
-
}
-
else
-
{
-
llIsValid = true
-
}
-
}
-
-
if (!plCheckDatabase)
-
{
-
if (llIsValid)
-
{
-
document.getElementById(pcID).innerHTML = pcDisplay
-
if (gnValidationCount >= 1)
-
{
-
gnValidationCount = gnValidationCount - 1
-
}
-
}
-
else
-
{
-
document.getElementById(pcID).innerHTML = "<span class='warninglabel'>" + pcDisplay + "</span>"
-
if (gnValidationCount <= pnNumberOfItems)
-
{
-
gnValidationCount = gnValidationCount + 1
-
}
-
}
-
}
-
-
if (gnValidationCount == 0)
-
{
-
hideOrShowInput("complete",false)
-
}
-
else
-
{
-
hideOrShowInput("complete",true)
-
}
-
if (plPopulateExtra)
-
{
-
populateExtra(lcSource, lcDestination, true, true)
-
}
-
}
-
PHP (the page referenced in validateItem())
-
<?php
-
-
// establish what is being checked via the $_REQUEST variable
-
$lnCheckType = $_REQUEST['check'];
-
$lcItemToCheck = $_REQUEST['tocheck'];
-
switch($lnCheckType)
-
{
-
case 1: // check for presence of username, will only ever be one match
-
$lcCheckSQL =
-
"SELECT 1 as test
-
FROM credential
-
WHERE username like '$lcItemToCheck'" ;
-
-
$laResult = $loDB->queryGetData($lcCheckSQL);
-
-
foreach($laResult as $lcTest)
-
{
-
$llAvailable = $lcTest['test'];
-
if($llAvailable > 0)
-
{
-
echo "<span class='warninglabel'>Username in use:</span>";
-
}
-
else
-
{
-
echo "Username:";
-
}
-
}
-
break;
-
-
case 2:// just to show that we can expand this system should we ever need to
-
echo "case = 2";
-
break;
-
}
-
?>
-
The Problem
The php will echo the tow variables from $_REQUEST, and the $lcCheckSQL is defined correctly. However, there appears to be a problem with the way the SQL is executing as all that happens is the label that is supposed to change is blanked.
I believe the connection to the database is available, this is set at the top of the page that has the form on it and so should be available. Even if I add a connection to this page the behaviour is the same.
This is really frustrating now and I'd love some help with this.
Thanks for reading (I know it was long)
nathj