Jump to content

basic problem im sure


ldb358

Recommended Posts

hi i am working on a very basic ajax script but for some reason its not working the script is simply put check if a username is a available so that the user doesn't have to submit the whole form to know is the username is available

 

heres the html script:

<html>
<head>
<link href='smartform.css' type='text/css' rel='stylesheet'/>
<script src='smartform.js' type='text/javascript'></script>
</head>
<body>
<form>
username:<input type='text' id='username' onchange='checkUsername()'/><div id="cUsername"></div><br/>
password:<input type='text' id='password'/><br/>
confirm password:<input type='text' id='vPassword'/><br/>
first name:<input type='text' id='fName'/><br/>
last name:<input type='text' id='lName'/><br/>
email:<input type='text' id='email'/><br/>
<input type='submit' value='register' />
</form>
</body>
</html>

 

now the javascript file:

 

var xmlhttp = newHttpObject();
function checkUsername(){
var username = document.getElementById("username").value;
var div1 = document.getElementById("cUsername");
sendUsername(username);
if(xmlhttp == null){
div1.innerHTML = "";
}

}
function sendUsername(username){
	var url = "http://lbflash.summerhost.info/smartform.php?username=" + username;
	var type = "GET";
	xmlhttp.open(type,url,true);
	xmlhttp.onreadystatechange = response;
	xmlhttp.send(null);

}
function response(){

	if(xmlhttp.readyState == 4){
if(xmlhttp.responseText > "0"){
	div1.innerHTML = "<img src='x.png' 			style='margin-left:5px;margin-right:5px;'/>taken";
	}else if(xmlhttp.responseText == "0"){
	div1.innerHTML = "<img src='checkmark.png' 	style='margin-left:5px;margin-right:5px;'/>not taken";
	}
}
}
function newHttpObject(){
	if (window.XMLHttpRequest){
  		return new XMLHttpRequest();
  		}	
	if (window.ActiveXObject){
		return new ActiveXObject("Microsoft.XMLHTTP");
	}else{
	return null;
	}
}

 

now the php file:

 

<?php
include('db.php');
$username = $_REQUEST['username'];
$check = mysql_query("SELECT * FROM users WHERE username='$username'");
$results = mysql_num_rows($check);
echo $results;
?> 

 

whats wrong?

    Thanks in advanced

Link to comment
Share on other sites

Where does it stop?  What is the error / not working part?  At a glance, a problem might arise:

   if(xmlhttp.responseText > "0"){

at that line.  You may want to convert the responseText to an integer, and then use > 0 rather than > "0";

 

Be more specific about what part doesn't work in order to save us time.

 

$username = $_REQUEST['username'];

Also you may want to sanitize your inputs (mysql_esape_real_string)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.