Jump to content

[SOLVED] Learning AJAX


LooieENG

Recommended Posts

index.html

 

<html>
<body>

<script language="javascript">

function checkUsername(str){
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function(){
if (ajax.readyState == 4) {
document.login.usernameResponse.innerHTML = ajax.responseText;
}
}
ajax.open("GET", "checkUsername.php?q=" + str, true);
ajax.send(null);
}

</script>
<form name='login'>
Username: <input type='text' onkeyup="checkUsername(this.value);" name='username' /> <div id="usernameResponse"></div>
</form>

</body>
</html>

 

checkUsername.php

 

<?php

if ($_GET['q'] == 'test') {
echo 'Username exists.';
} else {
echo 'Okay!'
}

?>

 

Doesn't work :(

Link to comment
https://forums.phpfreaks.com/topic/129868-solved-learning-ajax/
Share on other sites

function checkUsername(str){
var ajax = new XMLHttpRequest();
ajax.open("GET", "checkUsername.php?q=" + str, true);
ajax.send(null);
ajax.onreadystatechange = function(){
if (ajax.readyState == 4) {
document.login.usernameResponse.innerHTML = ajax.responseText;
}
}
}

Link to comment
https://forums.phpfreaks.com/topic/129868-solved-learning-ajax/#findComment-673700
Share on other sites

Ah, thanks for the help but a friend told me yesterday. Forgot to edit this topic.

 

Needed to change this

 

document.login.usernameResponse.innerHTML = ajax.responseText;

 

to this

 

document.getElementById("usernameResponse").innerHTML = ajax.responseText;

Link to comment
https://forums.phpfreaks.com/topic/129868-solved-learning-ajax/#findComment-673998
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.