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
https://forums.phpfreaks.com/topic/73569-ajax-php-sql-not-working/
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";
}
?>

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.