SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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. #2
    SitePoint Zealot rhlowe's Avatar
    Join Date
    Sep 2006
    Location
    San Diego
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The short answer is yes, an AJAX call can be made to a php page, assuming your server is configured to run php
    San Diego Freelance Web Development Contractor
    Great Taste, no Filler. Now with no Trans Fat!

  3. #3
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes i know the call can made to a php page but i am wondering the if i can call the php if its on that page for example this is what i want to do?

    register.php
    PHP 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", "register.php" + query, true);
        ajaxRequest.send(null); 
    }
    //-->
    </script>
    </head>
    <body>
    <?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;
    ?>
    <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>
    This don't work because when i start to type it says error on page on the bottom of the page in the status bar!

  4. #4
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    solved!

  5. #5
    SitePoint Zealot rhlowe's Avatar
    Join Date
    Sep 2006
    Location
    San Diego
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please share what you did for any future people who look for a similar solution!
    San Diego Freelance Web Development Contractor
    Great Taste, no Filler. Now with no Trans Fat!


Bookmarks

Posting Permissions

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