Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-25-2008, 09:20 PM   PM User | #1
birdbrain24
New Coder

 
Join Date: Jul 2007
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
birdbrain24 has a little shameless behaviour in the past
Ajax & PHP: On the same page?

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!
birdbrain24 is offline   Reply With Quote
Old 02-25-2008, 09:28 PM   PM User | #2
flynch01
Regular Coder

 
Join Date: Mar 2007
Location: England
Posts: 357
Thanks: 46
Thanked 22 Times in 21 Posts
flynch01 is on a distinguished road
I think I know what you mean.

register.php

PHP Code:
<?php
    
if (!empty($_GET['username'])):
        
$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;
    endif;
?>

<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", "register.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>
flynch01 is offline   Reply With Quote
Old 02-25-2008, 09:41 PM   PM User | #3
birdbrain24
New Coder

 
Join Date: Jul 2007
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
birdbrain24 has a little shameless behaviour in the past
yes i tried that and it dosen't work!
birdbrain24 is offline   Reply With Quote
Old 02-25-2008, 09:45 PM   PM User | #4
birdbrain24
New Coder

 
Join Date: Jul 2007
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
birdbrain24 has a little shameless behaviour in the past
solved!
birdbrain24 is offline   Reply With Quote
Old 02-25-2008, 09:46 PM   PM User | #5
flynch01
Regular Coder

 
Join Date: Mar 2007
Location: England
Posts: 357
Thanks: 46
Thanked 22 Times in 21 Posts
flynch01 is on a distinguished road
Oh yeah? What was it in the end?
flynch01 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:40 PM.


Advertisement
Log in to turn off these ads.