Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Regular Coder
    Join Date
    Mar 2007
    Posts
    357
    Thanks
    46
    Thanked 22 Times in 21 Posts
    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>

  • #3
    New Coder
    Join Date
    Jul 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes i tried that and it dosen't work!

  • #4
    New Coder
    Join Date
    Jul 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    solved!

  • #5
    Regular Coder
    Join Date
    Mar 2007
    Posts
    357
    Thanks
    46
    Thanked 22 Times in 21 Posts
    Oh yeah? What was it in the end?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •