Wednesday, June 23, 2010

Error Checking with HTTP Status Codes in Ajax

Error checking is very important when working with AJAX. If you are inserting data directly into a page, you must ensure at the least that the request completed succesfully. While the request may have been completed (determined by a ready state of 4 - see above), it may not have been succesful - for example, if the page was not found, or if access was denied. We can handle this using HTTP status codes.
You will be familiar with the most commonly seen status code - 404, or file not found. The most commonly used code, however, is 200 - OK. We can check the HTTP status code returned by the web server using the status property of the XMLHttpRequest object. So, our previous example should check that the ready state is 4 and the status code is 200:
xhrobj.onreadystatechange = function()
{
  if (xhrobj.readyState == 4 && xhrobj.status == 200)
  {
    if (xhrobj.responseText)
    {
     document.write(xhrobj.responseText);
    }
  }
};

Common status codes

Here is a list of common status codes:
200 OK
400 Bad Request
403 Forbidden
404 Not Found
50x Server Errors
500 Internal Server Error

Convert String Into object using javascript

Convert String Into object using javascript:

How to get a input value from the string using javascript.
Use "eval" function to get input value from the string.

Example:
<form>
  <input type="text" name="test" id="test" value="1" />
  <input type="button" name="te" onclick="testing()" />
</form>

<script>
  function testing() {
   var st = "document.getElementById('test').value";
   alert(st);
//output:  document.getElementById('test').value
  //output : 1
   alert(eval(st));
  //output : 1
  }
</script>

Monday, June 14, 2010

Graphical Pie Chart With Google Chart API in 3 steps


Download the latest version package from this link - http://code.google.com/p/googchart/


Step 1:

Include the API class file in the PHP file where you want to Display the Chart

include( 'GoogChart.class.php' );


Step 2:

Assign the datas which should be charted in the array variable say $data like below

$data = array(
'IE7' => 22,
'IE6' => 30.7,
'IE5' => 1.7,
'Firefox' => 36.5,
'Mozilla' => 1.1,
'Safari' => 2,
'Opera' => 1.4,
);
Set the Graph Color of your choice here in a array variable called $color
// Set graph colors
$color = array(
'#99C754',
'#54C7C5',
'#999999',
);

Step 3:

Finally building the Chart and printing it.

$chart->setChartAttrs( array(
'type' => 'pie',
'title' => 'Browser market 2008',
'data' => $data,
'size' => array( 400, 300 ),
'color' => $color
));

// Print chart
echo $chart;



Output:

















Supported types:

  • Pie
    Standard pie chart
  • Line
    Standard Line chart
  • Sparkline
    Almost identical to Line, except defaults to no axis lines.
  • Bar-horizontal
    Horizontal bar chart
  • Bar-vertical
    Vertical bar chart
Note :Examples for these types are Available in example.php available when you download this package.

Enjoy!



List DB and Tables using PHP

$conn = mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());
    //List DB
    $result = mysql_list_dbs( $conn );
    while( $row = mysql_fetch_object( $result ) ):
         echo  $row->Database "<br />";
    endwhile;

   //List Tables
  $db_name  = "test";
  $tables = mysql_list_tables("$db_name");
   while (list($table) = mysql_fetch_row($tables)) {
    $treeNodes->add($table,$table,$table,true,false,"","");
   }

  or
 $tables_list = mysql_query("SHOW TABLES FROM $db_name");
 

Followers