Jump to content

Echoing PHP within PHP via AJAX?


MikeDXUNL

Recommended Posts

Alright, I have something so that if a user types in "JoeSchmo123" as a username upon registration, and the username is already taken, ajax sends that input onkeyup and tells the viewer wether the name is taken or not.

 

 

I want it so that if the username is taken...the SUBMIT button will be disabled

 

here is the coding so far:

 

 

register.php

<html>
<head>
<script src="js/finduser.js"></script>
</head>

<body>

<form name="regForm" action="register.php" method="post">
Username: <input type="text" name="username" onkeyup="findUser(this.value)" /><br />
<div id="txtHint"> </div>
<input type="submit" value="Submit" <?php echo $error; ?>/>


</form>

</body>
</html>

 

 

 

 

finduser.js

var xmlHttp

function findUser(str)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="finduser.php";
url=url+"?username="+str;
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

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

 

 

 

finduser.php

<?php
include 'incl/dbconnect.php';

$username = $_GET['username'];




if(strlen($username) == 0) {
	echo "";
}
elseif(strlen($username) < 6) {
	echo "Not Long Enough";
} else {

$sqlusercheck = mysql_query("SELECT username FROM members 
            WHERE username='$username'");
$usercheck = mysql_num_rows($sqlusercheck);
  if($usercheck > 0){
        unset($username);
        echo "Username is in use!";
        $error = "disabled";
    } else {
	echo "It is alright";
	$error = "";
}
}


?>

Link to comment
Share on other sites

add an id= to you submit button:

<input id="mysubmit" type="submit" value="Submit" <?php echo $error; ?>/>

 

test if the error message was returned by ajax in stateChanged and modify your submit button disable attribute:

function stateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
    document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
    if (xmlHttp.responseText == '*error message*')
        document.getElementById("mysubmit").disabled = true;
    else
         document.getElementById("mysubmit").disabled = false;   

}
}

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.