PHP: Registration Help with AJAX

Shadow121

Member
Messages
901
Reaction score
0
Points
16
Alright, I have an AJAX Username checker which goes to check.php with ?user=NAME and I want it so when it returns something with 'Not Available' in it, it will disable the form field.
This is what I have:
Register.php
Code:
<?php
if($settings->reg_stats == 0){
    $Functions->openblock("Registrations Disabled","par");
    print "Site registrations have been disabled";
    $Functions->closeblock("par");
}else{
    if(!$_POST['register']){
        $Functions->openblock("Register","frm", "/register/", null, 'reg_frm');
        print "<strong>Username</strong>:<br />
        <input type=\"text\" name=\"username\" onchange=\"CheckItems(this.value, 'user');\" />
        </p>
        <div id=\"user_results\"></div>
        <p>
        <strong>Password</strong>:<br />
        <input type=\"password\" name=\"pass\" onchange=\"CheckItems(this.value, 'pass');\" />
        </p>
        <div id=\"pass_results\"></div>
        <p>
        <strong>Verify Password</strong>:<br />
        <input type=\"password\" name=\"vpwd\" onchange=\"CheckItems(this.value, 'vpass');\" />
        </p>
        <div id=\"vpass_results\"></div>
        <p>
        <strong>E-mail</strong>:<br />
        <input type=\"text\" name=\"mail\" /><br />
        <strong>Verify E-mail</strong>:<br />
        <input type=\"text\" name=\"vmail\" /><br />
        <input type=\"submit\" name=\"register\" disabled=\"false\" />";
        $Functions->closeblock("frm");
    }else{
        
    }
}
?>

check.js
Code:
var xmlHttp

function GetXmlHttpObject(){

var objXMLHttp=null 

if (window.XMLHttpRequest){

objXMLHttp=new XMLHttpRequest()

}else if (window.ActiveXObject){

objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")

}

return objXMLHttp

} 



function CheckItems(text, type){

xmlHttp=GetXmlHttpObject()

if (xmlHttp==null){

alert ("Browser does not support HTTP Request")

return

}



var url="http://SITE/check.php?text=" + text + "&type=" + type

xmlHttp.open("GET",url,true)

xmlHttp.onreadystatechange = function () {

if (xmlHttp.readyState == 4) {

document.getElementById(type + "_results").innerHTML = xmlHttp.responseText; 

if (xmlHttp.responseText.contains == "Not Available"){

    document.reg_frm.register.disabled = true;

}else{

    document.reg_frm.register.disabled = false;

}

}

};

xmlHttp.send(null);

}

check.php
Code:
<?php
require ('FileLibrary/configuration.db.php');
switch ($_GET['type']) {
    default:
        print "No Type Defined!";
        break;
    case 'user':
        $user = stripslashes(strip_tags(htmlspecialchars($_GET['text'], ENT_QUOTES)));
        $check = $db->dbCount($db->dbQuery("SELECT * FROM `" . DB_PREFIX . "users` WHERE `username` = '" . $user .
            "'"));
        if ($check == 0) {
            echo '' . $user . ' is <span style="color:#009933">Available!</span>';
        } elseif ($check > 0) {
            echo '' . $user . ' is <span style="color:#CC0000">Not Available!</span>';
        }
        break;
    case 'pass':
    case 'vpass':
        $password = $_GET['text'];
        $strength = 0;
        if (preg_match("/([a-z]+)/", $password)) {
            $strength++;
        }
        // letters (uppercase)
        if (preg_match("/([A-Z]+)/", $password)) {
            $strength++;
        }
        // numbers
        if (preg_match("/([0-9]+)/", $password)) {
            $strength++;
        }
        // non word characters
        if (preg_match("/(W+)/", $password)) {
            $strength++;
        }
        if ($_GET['type'] == 'pass') {
            $def_str = '<strong>Password Strength</strong>: ';
        } else {
            $def_str = '<strong>Verification Password Strength</strong>: ';
        }
        switch ($strength) {
            default:
                echo $def_str . "<span style=\"color:red; text-decoration:underline;\">None Given!</span>";
                break;
            case "1": //red
                echo $def_str . "<span style=\"color:red;\">Very Weak</span>";
                break;
            case "2": //yellow
                echo $def_str . "<span style=\"color:yellow;\">Weak</span>";
                break;
            case "3": //orange
                echo $def_str . "<span style=\"color:orange;\">Strong</span>";
                break;
            case "4": //green
                echo $def_str . "<span style=\"color:green;\">Very Strong</span>";
                break;
        }
        break;
}
?>

Any help? u.u
 

xmakina

New Member
Messages
264
Reaction score
0
Points
0
And what happens when it runs? Where do you suspect the error is?
 

Shadow121

Member
Messages
901
Reaction score
0
Points
16
I suspect the AJAX because after I enter say NAME in the username field the login gets enabled, then when i use a used name it doesnt disable.
 

xmakina

New Member
Messages
264
Reaction score
0
Points
0
change:
echo '' . $user . ' is <span style="color:#CC0000">Not Available!</span>';

to

echo "$user is <span style=\"color:#CC0000\">Not Available!</span>";

and see if it works now.
 

Shadow121

Member
Messages
901
Reaction score
0
Points
16
THat shouldn't have anything to do with it.. All that does is change the style of the output.
 

xmakina

New Member
Messages
264
Reaction score
0
Points
0
Er... it should. I suggested the name change as I suspect your check page is not, in fact, returning "Not Available" due to the "'s in the span tag and so your script isn't behaving as it should.
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
I suspect your check page is not, in fact, returning "Not Available" due to the "'s in the span tag and so your script isn't behaving as it should.
No. The check page is essentially doing '...."not available"...';
Either of the two ways will work, although personally I would use a third way (shown at bottom of post), but that isn't the problem here.

Shadow, you already have a variable in the PHP that is a certain value when 'Not Available' is displayed.
NA will only show when $check is more than 0.

So pass the value of $check to the javascript if that's how you want to disable the form, and then simply check in the JS if the form should be displayed or not.


And here's how I would do that that erroneous echo (only difference to xmakina's being the single quotes for HTML rather than escaping):
PHP:
echo "$user is <span style='color:#CC0000'>Not Available!</span>";
 
Last edited:

natsuki

New Member
Messages
112
Reaction score
0
Points
0
Code:
if (xmlHttp.responseText.contains == "Not Available"){

    document.reg_frm.register.disabled = true;

}else{

    document.reg_frm.register.disabled = false;

}
so this is basically not working because of one way or another

i know you know what you're doing because your code is structured and and well managed.

What does it show then the username is not available?
what does this contain when called if name not available: document.getElementById(type + "_results").innerHTML

OT: I recommend always using single quotes in PHP for strings unless you need to output values of variables. It saves the php interpreter time to parse the strings (which doesn't contain anything to parse in the first place). Double quotes used when there are a lot of variables that are to be parsed inside the string. It's not really necessary to do this though, but it's not just a matter of style but of efficiency. Just some fyi
 
Last edited:
Top