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
https://forums.phpfreaks.com/topic/58297-echoing-php-within-php-via-ajax/
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;   

}
}

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.