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)
[CODE=html]
<div class="row">
<span class="label" id="usernamelab el">
<span class="warningl abel">Username: </span>
</span>
<span class="formw">
<input id="username" type="text" size="47" onchange="valid ateItem('userna melabel',this.v alue,'Username: ',4,false,5,tru e);" title="Username , for use on the Forum" />
(6-18 alpha numeric characters)
</span>
</div>
[/CODE]
Javascript (3 functions involved)
[CODE=javascript]
function stateChanged()
{
if (goXMLHTTP.read yState==4 || goXMLHTTP.ready State=="complet e")
{
document.getEle mentById(gcItem ID).innerHTML=g oXMLHTTP.respon seText;
}
}
function GetXmlHttpObjec t()
{
if (window.XMLHttp Request)
{
goXMLHTTP=new XMLHttpRequest( )
}
else if (window.ActiveX Object)
{
goXMLHTTP=new ActiveXObject(" Microsoft.XMLHT TP")
}
if (goXMLHTTP==nul l)
{
alert ("Browser does not support HTTP Request")
return
}
}
function validateItem(pc ID,pcItem,pcDis play,pnType,plP opulateExtra,pn NumberOfItems,p lCheckDatabase)
{
var lcRegExp, llIsValid, llUseRegExp
llIsValid = false
llUseRegExp = false
switch (pnType)
{
case 1: // UK postcode
pcItem = pcItem.toUpperC ase()
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(l cRegExp))
{
if(plCheckDatab ase)
{
GetXmlHttpObjec t() // sets the global variable
gcItemID = pcID
gcUrl = "../lib/datacheck.php?c heck=1&tocheck= '" + pcItem + "'"
goXMLHTTP.onrea dystatechange=s tateChanged
goXMLHTTP.open( "GET",gcUrl,tru e)
goXMLHTTP.send( null)
}
else
{
llIsValid = true
}
}
else
{
llIsValid = false
}
}
else
{
if (pcItem == null||pcItem == ""||pcItem.leng th < 2)
{
llIsValid = false
}
else
{
llIsValid = true
}
}
if (!plCheckDataba se)
{
if (llIsValid)
{
document.getEle mentById(pcID). innerHTML = pcDisplay
if (gnValidationCo unt >= 1)
{
gnValidationCou nt = gnValidationCou nt - 1
}
}
else
{
document.getEle mentById(pcID). innerHTML = "<span class='warningl abel'>" + pcDisplay + "</span>"
if (gnValidationCo unt <= pnNumberOfItems )
{
gnValidationCou nt = gnValidationCou nt + 1
}
}
}
if (gnValidationCo unt == 0)
{
hideOrShowInput ("complete",fal se)
}
else
{
hideOrShowInput ("complete",tru e)
}
if (plPopulateExtr a)
{
populateExtra(l cSource, lcDestination, true, true)
}
}
[/CODE]
PHP (the page referenced in validateItem())
[CODE=php]
<?php
// establish what is being checked via the $_REQUEST variable
$lnCheckType = $_REQUEST['check'];
$lcItemToCheck = $_REQUEST['tocheck'];
switch($lnCheck Type)
{
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($laResu lt as $lcTest)
{
$llAvailable = $lcTest['test'];
if($llAvailable > 0)
{
echo "<span class='warningl abel'>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;
}
?>
[/CODE]
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
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)
[CODE=html]
<div class="row">
<span class="label" id="usernamelab el">
<span class="warningl abel">Username: </span>
</span>
<span class="formw">
<input id="username" type="text" size="47" onchange="valid ateItem('userna melabel',this.v alue,'Username: ',4,false,5,tru e);" title="Username , for use on the Forum" />
(6-18 alpha numeric characters)
</span>
</div>
[/CODE]
Javascript (3 functions involved)
[CODE=javascript]
function stateChanged()
{
if (goXMLHTTP.read yState==4 || goXMLHTTP.ready State=="complet e")
{
document.getEle mentById(gcItem ID).innerHTML=g oXMLHTTP.respon seText;
}
}
function GetXmlHttpObjec t()
{
if (window.XMLHttp Request)
{
goXMLHTTP=new XMLHttpRequest( )
}
else if (window.ActiveX Object)
{
goXMLHTTP=new ActiveXObject(" Microsoft.XMLHT TP")
}
if (goXMLHTTP==nul l)
{
alert ("Browser does not support HTTP Request")
return
}
}
function validateItem(pc ID,pcItem,pcDis play,pnType,plP opulateExtra,pn NumberOfItems,p lCheckDatabase)
{
var lcRegExp, llIsValid, llUseRegExp
llIsValid = false
llUseRegExp = false
switch (pnType)
{
case 1: // UK postcode
pcItem = pcItem.toUpperC ase()
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(l cRegExp))
{
if(plCheckDatab ase)
{
GetXmlHttpObjec t() // sets the global variable
gcItemID = pcID
gcUrl = "../lib/datacheck.php?c heck=1&tocheck= '" + pcItem + "'"
goXMLHTTP.onrea dystatechange=s tateChanged
goXMLHTTP.open( "GET",gcUrl,tru e)
goXMLHTTP.send( null)
}
else
{
llIsValid = true
}
}
else
{
llIsValid = false
}
}
else
{
if (pcItem == null||pcItem == ""||pcItem.leng th < 2)
{
llIsValid = false
}
else
{
llIsValid = true
}
}
if (!plCheckDataba se)
{
if (llIsValid)
{
document.getEle mentById(pcID). innerHTML = pcDisplay
if (gnValidationCo unt >= 1)
{
gnValidationCou nt = gnValidationCou nt - 1
}
}
else
{
document.getEle mentById(pcID). innerHTML = "<span class='warningl abel'>" + pcDisplay + "</span>"
if (gnValidationCo unt <= pnNumberOfItems )
{
gnValidationCou nt = gnValidationCou nt + 1
}
}
}
if (gnValidationCo unt == 0)
{
hideOrShowInput ("complete",fal se)
}
else
{
hideOrShowInput ("complete",tru e)
}
if (plPopulateExtr a)
{
populateExtra(l cSource, lcDestination, true, true)
}
}
[/CODE]
PHP (the page referenced in validateItem())
[CODE=php]
<?php
// establish what is being checked via the $_REQUEST variable
$lnCheckType = $_REQUEST['check'];
$lcItemToCheck = $_REQUEST['tocheck'];
switch($lnCheck Type)
{
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($laResu lt as $lcTest)
{
$llAvailable = $lcTest['test'];
if($llAvailable > 0)
{
echo "<span class='warningl abel'>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;
}
?>
[/CODE]
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
Comment