FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

AJAX problem.

 


Diablosblizz
Okay, as I posted before I needed a system that checked if a username has already been registered. I have found a code, but there is no go. For the AJAX part, I have this:

Code:
<script type="text/javascript">
function toggle_username(userid) {
    if (window.XMLHttpRequest) {
        http = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        http = new ActiveXObject("Microsoft.XMLHTTP");
    }
    handle = document.getElementById(userid);
    var url = 'ajax.php?';
    if(handle.value.length > 0) {
        var fullurl = url + 'do=check_username_exists&username=' + encodeURIComponent(handle.value);
        http.open("GET", fullurl, true);
        http.send(null);
        http.onreadystatechange = statechange_username;
    }else{
        document.getElementById('username_exists').innerHTML = '';
    }
}

function statechange_username() {
    if (http.readyState == 4) {
        var xmlObj = http.responseXML;
        var html = xmlObj.getElementsByTagName('result').item(0).firstChild.data;
        document.getElementById('username_exists').innerHTML = html;
    }
}
</script>
<input id="username" type="text" name="username" onchange="toggle_username('username')" /><br />
<div id="username_exists" style="font-size: 11px;font-weight: bold;color:#FF3300"> </div>


It's supposed to change the div, but it doesn't. I believe it gets the result from here, but I am not sure:

Code:
<?php
include("members/config.php");

$do = $_GET['do'];
switch($do) {
    case 'check_username_exists':
        if(get_magic_quotes_gpc()) {
            $username = $_GET['username'];
        }else{
            $username = addslashes($_GET['username']);
        }
        $count = mysql_num_rows(mysql_query("SELECT * FROM `members` WHERE `username`='".$username."'"));
        header('Content-Type: text/xml');
        header('Pragma: no-cache');
        echo '<?xml version="1.0" encoding="UTF-8"?>';
        echo '<result>';
        if($count > 0) {
            echo 'That username already exists, please select another one.';
        }else{
            echo 'That username is available.';
        }
        echo '</result>';
    break;
    default:
        echo 'Error, invalid action';
    break;
}
?>


That file is called AJAX.php. If I go to that page, with the ?do and stuff then I get a true result, but it's not showing up on the first bit of code. I don't really understand Javascript that much, so I am trying to learn. Any support will be helpful!

Thank you.
badai
use Ajax Agent to handle Ajax

http://www.hemmady.com/ajaxagent

after that post your code here (both html and php)
pollux1er
I can see that you are dong a synchronous ajax script. Which means that in the ajax part, i can advise you to permute two lines :

Code:

http.open("GET", fullurl, true);
http.onreadystatechange = statechange_username;
http.send(null);



try it. Because i think it is a mistake.
flatliner
I would recommend you use http://mootools.net/ it makes using ajax a lot less hassle and code. Just download the framework and add it to your page. Now mootools was developed to be used by calling id's instead of using onclick="ajax();" tho I tend to still do this, I probly using it the wrong way, but it does the job. Check the below code

Code:

<script type="text/javascript" src="mootools.js"></script>

<form id="myform">
<input id="username" type="text" name="username"
onchange="Javascript: new Ajax('php-page-that-checks-if-user.php', {method: 'get',data:$('myform'),update:$('username_exists')}).request();" />
</form>
<br />
<div id="username_exists" style="font-size: 11px;font-weight: bold;color:#FF3300"> </div>




That would sort it out for ya Smile
quasar
The element xmlObj has no child node. So replace

var html = xmlObj.getElementsByTagName('result').item(0).firstChild.data;

by

var html = xmlObj.getElementsByTagName('result').data;

Quasar
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.