<?php
ob_start();
extract($_REQUEST);
extract($_POST);
session_start();
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$db=mysql_select_db('murali',$con);


$message = '';
    $error = array();
     $select=mysql_query("select * from registration where username='$_REQUEST[username]'");
	  
   
     
$taken_usernames=mysql_fetch_array($select);

    // main submit logic
    if (@$_REQUEST['action'] == 'register') {
        $resp = check_username($_REQUEST['username']);
        
        if ($resp['ok']) {
            $message = 'All details fine';
        } else {
            $message = 'There was a problem with your registration details';
            $error = $resp;
        }
    } else if (@$_REQUEST['action'] == 'check_username' && isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
        // means it was requested via Ajax
        echo json_encode(check_username($_REQUEST['username']));
        exit; // only print out the json version of the response
    }
    
    function check_username($username) {
        global $taken_usernames;
        $resp = array();
        $username = trim($username);
        if (!$username) {
            $resp = array('ok' => false, 'msg' => "Please specify a username");
        } else if (!preg_match('/^[a-z0-9\.\-_]+$/', $username)) {
            $resp = array('ok' => false, "msg" => "Your username can only contain alphanumerics and period, dash and underscore (.-_)");
        } else if (in_array($username, $taken_usernames)) {
            $resp = array("ok" => false, "msg" => "The selected username is not available");
        } else {
            $resp = array("ok" => true, "msg" => "This username is free");
        }

        return $resp;        
    }
?>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

      <html xmlns="http://www.w3.org/1999/xhtml">
  
      <head>
   
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

      <title>Ajax - PHP example</title>
  
      </head>
 
   
 
      <body>
 
       

      <script language="javascript" type="text/javascript">

      <!--
  
      // Get the HTTP Object
  
      function getHTTPObject(){
 
      if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");

      else if (window.XMLHttpRequest) return new XMLHttpRequest();

      else {

      alert("Your browser does not support AJAX.");

      return null;

      }

      }

       

      // Change the value of the outputText field
 
      function setOutput(){

      if(httpObject.readyState == 4){
  
      document.getElementById('outputText').value = httpObject.responseText;
  
      }
 
       

      }

       
 
      // Implement business logic

      function doWork(){
 
      httpObject = getHTTPObject();
 
      if (httpObject != null) {
  
      httpObject.open("GET", "upperCase.php?inputText="

      +document.getElementById('inputText').value, true);
 
      httpObject.send(null);
 
      httpObject.onreadystatechange = setOutput;
  
      }
 
      }
  
       
 
      var httpObject = null;

       
  
      //-->
 
      </script>

       

      <form name="testForm">

      Input text: <input type="text" onkeyup="doWork();" name="inputText" id="inputText" />

      Output text: <input type="text" name="outputText" id="outputText" />
  
      </form>
 
      </body>
 
      </html>

.actually i am doing registration form validation in ajax. if username is already in database give an error. when user enters the username in textbox that will automatically checks it is in database or not.....with out submiting the form. in the above code array values are not passing into variable. below is that code.

$select=mysql_query("select * from registration where username='$_REQUEST[username]'");
	  
   
     
$taken_usernames=mysql_fetch_array($select);

instead of this code...

$select=mysql_query("select * from registration where username='$_REQUEST[username]'");
	     
$taken_usernames=mysql_fetch_array($select);

use this...

$select=mysql_query("select * from registration where username='$_REQUEST[username]'");
while($row = mysql_fetch_array($select)){
$taken_usernames[] = $row['username'];  //the "username" here is the name of column in your table 
}

Hope this helps

<?php
ob_start();
extract($_REQUEST);
extract($_POST);
session_start();
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$db=mysql_select_db('murali',$con);


$message = '';
    $error = array();
     $select=mysql_query("select * from registration where username='$_REQUEST[username]'");
	  
   
     
$taken_usernames=mysql_fetch_array($select);

    // main submit logic
    if (@$_REQUEST['action'] == 'register') {
        $resp = check_username($_REQUEST['username']);
        
        if ($resp['ok']) {
            $message = 'All details fine';
        } else {
            $message = 'There was a problem with your registration details';
            $error = $resp;
        }
    } else if (@$_REQUEST['action'] == 'check_username' && isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
        // means it was requested via Ajax
        echo json_encode(check_username($_REQUEST['username']));
        exit; // only print out the json version of the response
    }
    
    function check_username($username) {
        global $taken_usernames;
        $resp = array();
        $username = trim($username);
        if (!$username) {
            $resp = array('ok' => false, 'msg' => "Please specify a username");
        } else if (!preg_match('/^[a-z0-9\.\-_]+$/', $username)) {
            $resp = array('ok' => false, "msg" => "Your username can only contain alphanumerics and period, dash and underscore (.-_)");
        } else if (in_array($username, $taken_usernames)) {
            $resp = array("ok" => false, "msg" => "The selected username is not available");
        } else {
            $resp = array("ok" => true, "msg" => "This username is free");
        }

        return $resp;        
    }
?>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

      <html xmlns="http://www.w3.org/1999/xhtml">
  
      <head>
   
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

      <title>Ajax - PHP example</title>
  
      </head>
 
   
 
      <body>
 
       

      <script language="javascript" type="text/javascript">

      <!--
  
      // Get the HTTP Object
  
      function getHTTPObject(){
 
      if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");

      else if (window.XMLHttpRequest) return new XMLHttpRequest();

      else {

      alert("Your browser does not support AJAX.");

      return null;

      }

      }

       

      // Change the value of the outputText field
 
      function setOutput(){

      if(httpObject.readyState == 4){
  
      document.getElementById('outputText').value = httpObject.responseText;
  
      }
 
       

      }

       
 
      // Implement business logic

      function doWork(){
 
      httpObject = getHTTPObject();
 
      if (httpObject != null) {
  
      httpObject.open("GET", "upperCase.php?inputText="

      +document.getElementById('inputText').value, true);
 
      httpObject.send(null);
 
      httpObject.onreadystatechange = setOutput;
  
      }
 
      }
  
       
 
      var httpObject = null;

       
  
      //-->
 
      </script>

       

      <form name="testForm">

      Input text: <input type="text" onkeyup="doWork();" name="inputText" id="inputText" />

      Output text: <input type="text" name="outputText" id="outputText" />
  
      </form>
 
      </body>
 
      </html>

.actually i am doing registration form validation in ajax. if username is already in database give an error. when user enters the username in textbox that will automatically checks it is in database or not.....with out submiting the form. in the above code array values are not passing into variable. below is that code.

$select=mysql_query("select * from registration where username='$_REQUEST[username]'");
	  
   
     
$taken_usernames=mysql_fetch_array($select);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.