MikeDXUNL Posted July 3, 2007 Share Posted July 3, 2007 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 More sharing options...
mainewoods Posted July 4, 2007 Share Posted July 4, 2007 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 https://forums.phpfreaks.com/topic/58297-echoing-php-within-php-via-ajax/#findComment-289734 Share on other sites More sharing options...
MikeDXUNL Posted July 4, 2007 Author Share Posted July 4, 2007 ahhh,, thank you very much. works like a charm Link to comment https://forums.phpfreaks.com/topic/58297-echoing-php-within-php-via-ajax/#findComment-289867 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.