+ Reply to Thread
Results 1 to 9 of 9

Thread: PHP: Registration Help with AJAX

  1. #1
    Shadow121's Avatar
    Shadow121 is offline x10 Lieutenant Shadow121 is an unknown quantity at this point
    Join Date
    Jul 2006
    Location
    Centerville
    Posts
    455

    PHP: Registration Help with AJAX

    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

  2. #2
    xmakina's Avatar
    xmakina is offline x10 Lieutenant xmakina is an unknown quantity at this point
    Join Date
    May 2008
    Location
    England
    Posts
    265

    Re: PHP: Registration Help with AJAX

    And what happens when it runs? Where do you suspect the error is?
    IF($this->$post.content() == "SEE SIG"){
    w3Schools and Google
    }

  3. #3
    Shadow121's Avatar
    Shadow121 is offline x10 Lieutenant Shadow121 is an unknown quantity at this point
    Join Date
    Jul 2006
    Location
    Centerville
    Posts
    455

    Re: PHP: Registration Help with AJAX

    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.

  4. #4
    xmakina's Avatar
    xmakina is offline x10 Lieutenant xmakina is an unknown quantity at this point
    Join Date
    May 2008
    Location
    England
    Posts
    265

    Re: PHP: Registration Help with AJAX

    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.
    IF($this->$post.content() == "SEE SIG"){
    w3Schools and Google
    }

  5. #5
    Shadow121's Avatar
    Shadow121 is offline x10 Lieutenant Shadow121 is an unknown quantity at this point
    Join Date
    Jul 2006
    Location
    Centerville
    Posts
    455

    Re: PHP: Registration Help with AJAX

    THat shouldn't have anything to do with it.. All that does is change the style of the output.

  6. #6
    xmakina's Avatar
    xmakina is offline x10 Lieutenant xmakina is an unknown quantity at this point
    Join Date
    May 2008
    Location
    England
    Posts
    265

    Re: PHP: Registration Help with AJAX

    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.
    IF($this->$post.content() == "SEE SIG"){
    w3Schools and Google
    }

  7. #7
    Scoochi2's Avatar
    Scoochi2 is offline x10 Sophmore Scoochi2 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Southport!
    Posts
    185

    Re: PHP: Registration Help with AJAX

    Quote Originally Posted by xmakina View Post
    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 Code:
    echo "$user is <span style='color:#CC0000'>Not Available!</span>"
    Last edited by Scoochi2; 11-04-2008 at 11:05 AM.
    If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending... :nuts:

  8. #8
    natsuki's Avatar
    natsuki is offline x10 Sophmore natsuki is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    112

    Re: PHP: Registration Help with AJAX

    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 by natsuki; 11-04-2008 at 11:48 AM.

  9. #9
    Shadow121's Avatar
    Shadow121 is offline x10 Lieutenant Shadow121 is an unknown quantity at this point
    Join Date
    Jul 2006
    Location
    Centerville
    Posts
    455

    Re: PHP: Registration Help with AJAX

    Forgot to place a link P:
    http://cdev.hrwr.org/?x=register
    I'll be adding SEO at a later point in time ._.

+ Reply to Thread

Similar Threads

  1. Ever Been Suspended For Using PHP?
    By dragoneye_xp in forum Off Topic
    Replies: 26
    Last Post: 08-16-2009, 07:17 PM
  2. Nuevo en x10? Lee el Tema Principal
    By ZharkD in forum Noticias y Anuncios
    Replies: 7
    Last Post: 10-21-2008, 12:43 PM
  3. php errors galore
    By DMG Online in forum Scripts & 3rd Party Apps
    Replies: 9
    Last Post: 05-17-2008, 06:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers