Jump to content

Problem with email checker which is powered by AJAX, PHP, and MySQL


Jerzxu

Recommended Posts

Okay basically I want AJAX and PHP to communicate together to real time update things. The problem is that it updates, but if I type in the user name I want to register (I have one put in the database) it doesn't change the text to: "User name unavailable"

 

Below are my codes:

 

 

<p><b>Email</b><br />
	          <input type="text" onkeyup="showEmail(this.value)" name="email"  align="absmiddle"/><span id="txtemail"></span> 
	          <br />

 

That is the email form text box

 

// JavaScript Document
var xmlHttp

function showEmail(str)
{
if (str.length==0)
  { 
  document.getElementById("txtemail").innerHTML=" No Email Entered"
  return
  }
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Browser does not support HTTP Request")
  return
  } 
var url="email.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
} 

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{ 
document.getElementById("txtemail").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;
}

 

the javascript code

 

<?php
 $dbh=mysql_connect ("localhost", "**********", "******") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("********");


$q=$_REQUEST["q"];
$result="";

if (strlen($q) > 0)
{
$result = mysql_query("SELECT * FROM emails");
{
	if ($q != $result)
		{
			$response=" Email is Avaible";
		}
		else
		{
		$response=" Email is Already Regristered";
	}
}
}



echo $response;
?>

the PHP code. The database has one user added into it but when typed it doesn't change the text to: Email is Already Regristered.

 

Link to comment
Share on other sites

  • 2 weeks later...

I noticed a small problem in your sql statement.  Here is the workaround..

 

$result = mysql_query("SELECT * FROM emails where emailid = '$q'");
$test_rows = mysql_num_rows($result);

if ($test_rows == 1){
echo " email already exists ";
}
else
{ echo "Email is available.";}
}

 

best of luck...

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.