Sunday, December 13, 2009

Fetch record in table format using Ajax,PHP


This is example for fetch email address group by and count them and display every result in table format. this example is apply for all type of fetching record in display in table format using ajax and php.

//************************** Server site fetchEmail.php for fetch record and return in table format******************


<?php

  header("Cache-Control: no-cache, must-revalidate" );
  header("Pragma: no-cache" );
  header("Content-Type: text/html; charset=utf-8");

$dbc=mysql_connect(HOSTNAME,USERNAME,PASSWORD) or die('There is error to connect to server:-'.mysqli_connect_error());
  $db_selected = mysql_select_db(DATABASE,$dbc);

  $groupName=$_GET['group'];

  function emailCount($emailId){
  $EmailArray=explode(",",$emailId);
  $totemail=count($EmailArray);
  return($totemail);
  }
  function format($txt){ //Function for RIght syntex for html format
  $txt=str_replace(',','<br>',$txt);
  return($txt);

  }

  $selectGroupRS="select group_name,emailId from emailgroup where group_name='$groupName'  ";
  $result1=mysql_query($selectGroupRS,$dbc) or die('error in group:-'.mysql_error());
  echo"<table width=\"90%\" cellpadding=\"0\" cellspacing=\"0\" border=\"1\">
  <tr>
  <td width=\"15%\" align=\"center\"><strong>Group Name</strong></td>
  <td align=\"center\"><strong>Email Id's</strong></td>
  <td align=\"center\"><strong>Count of Email Id's</strong></td>

  </tr>";
  while($row1=mysql_fetch_row($result1)){
  $name=$row1[0];
  $email.=','.$row1[1];

  }
  echo"
  <tr>";
  echo "<td align=\"center\">".$name."</td>";
  echo "<td ><div style=\"height:300px; overflow:scroll\">".format($email)."</div></td>";
  echo "<td align=\"center\">".emailCount($email)."</td>";
  echo"</tr>";
  echo "</table>";
  ?>


//****************************** End of fetchEmail.php***************************************************************



//************************Php file where record display****************************************

  <script language="javascript">

  function getXmlHttpRequestObject(){
  //alert('in xmlhttp');
  if(window.XMLHttpRequest){
  return new XMLHttpRequest();
  }else if(window.ActiveXObject){
  return new ActiveXObject("Microsoft.XMLHTTP");
  }else {
  alert("Your Browser Sucks!\nIt's about time to upgrade don't you think?");
  }

  }

  var searchemail = getXmlHttpRequestObject();

  function searchResult(){

  //alert('on search');

  //alert('in search');
  var groupName=escape(document.getElementById('searchgroup').value);
  //alert(groupName);
  searchemail.open("GET",'fetchEmail.php?group=' + groupName,true);
  searchemail.onreadystatechange=SearchEmailHandler;
  searchemail.send(null);



  }

  function SearchEmailHandler(){

  if(searchemail.readyState==4 || searchemail.readyState=="complete"){
  //alert('in hand');
document.getElementById('fetchId').innerHTML=searchemail.responseText;
  
  
  }
  }


  </script>


<strong> List of All group with Email Id</strong>
  <br />
  <select id="searchgroup">
  <?php


  $dbc=mysql_connect(HOSTNAME,USERNAME,PASSWORD) or die('There is error to connect to server:-'.mysqli_connect_error());
  $db_selected = mysql_select_db(DATABASE,$dbc);



  $selectGroup="select group_name from emailgroup group by group_name";
  $result=mysql_query($selectGroup,$dbc) or die('error in group:-'.mysql_error());
  while($row=mysql_fetch_row($result)){
  echo'<option>'.$row[0].'</option>';

  }

  ?>


</select>
  <input  type="button" value="search" onclick="searchResult();" />
  <br />



  <div id="fetchId"></div>


  //************************Php File End***************************************************


No comments:

Post a Comment