Ok i just started learning ajax and i was wondering if i can have the ajax and the php on the same page?
Here's my pages:
register.html
registercheck.php
register.html
Code:
<html>
<head>
<title>Register</title>
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function checkusername(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser do not support ajax!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('username_div');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var username = document.getElementById('username').value;
var query = "?username=" + username;
ajaxRequest.open("GET", "registercheck.php" + query, true);
ajaxRequest.send(null);
}
//-->
</script>
</head>
<body>
<form>
<table border="1" bordercolor="#000000" cellspacing="0">
<tr>
<td>Username:</td>
<td><input type="text" id="username" onkeyup="checkusername()" /></td>
<td><div id="username_div">Username Status!</div></td>
</tr>
</table>
</form>
</body>
</html>
registercheck.php
PHP Code:
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "race";
//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
//Select Database
mysql_select_db($dbname) or die(mysql_error());
//Set variables
$username = $_GET['username'];
//Mysql Variables
$result_query = mysql_query("SELECT * FROM `users` WHERE `username`='$username'") or die(mysql_error());
$result_num_rows = mysql_num_rows($result_query);
if(isset($username)):
if($result_num_rows == 0):
echo '<font color="green"><strong>Username avaliable!</strong></font>';
elseif($result_num_rows != 0):
echo '<font color="red"><strong>Username unavaliable!</strong></font>';
endif;
endif;
?>
I hope someone could help! Thanks anyways!