Jump to content

AJAX + PHP + SQL Not working


Evilzebra

Recommended Posts

On the form page I have

 

<script language="javascript" type="text/javascript">
var xmlHttp

function showUser(str)
{ 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="check.php"
url=url+"?check_user="+str
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{ 
document.getElementById("displayConfirm").innerHTML=xmlHttp.responseText 
} 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
}
return xmlHttp;
}
</script>

<H1 align="center">Login</H1>
<form name="form1" method="post" action="index.php?p=login">
  <p align="center">
	  <b>Username:</b><br><input name="username" type="text" id="username" onblur="showUser(this.value);"><div id="displayConfirm"></div>
  <p align="center">
	  <b>Password:</b><br><input name="password" type="password" id="password"><br>
	  <a href="index.php?p=forgot">Forgot your password?</a><br><br>
	  <input type="checkbox" name="keep" id="keep">Keep me logged in.<a href="#" onmouseover="Tip('Checking this box will keep you logged in for 30 days.')">[?]</a><br><br>
	  <input class="button" onClick="ajaxFunction();" value="Login" type="button">
  </p>
</form>

 

And a separate file(check.php) I have

<?
$loginUsername = $_GET['check_user'];
$query = "SELECT * FROM users WHERE Name='$loginUsername'";
$result = mysql_query($query);
if($result && mysql_num_rows($result) == 1){
	Echo "Username found";
} else {
	Echo $loginUsername;
}
?>

 

When I test out the code it will dynamically print the username as you type it in but it won't seem to find the username in the database. I am using Firefox. I have triple check my DB and it that is all correct.

Link to comment
Share on other sites

Try this..

 

check.php

 

<?php
$loginUsername = $_GET['check_user'];
$query = "SELECT * FROM users WHERE Name='$loginUsername'";
$result = mysql_query($query);
if(mysql_num_rows($result)>0){
echo "Sorry: $loginUsername is unavailable";
} else {
echo "Congrats $loginUsername is available";
}
?>

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.