phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


PHP - AJAX: Error Handler for Server Side

Tag: AJAX, javacript, DOM, CSS, client side, javascript event, error handler   Category: PHP Application
post: 18 Mar 2008 read: 129


PHP AJAX Step By Step Tutorial - part 8: We want to build better error handler. For first, put error handler at server side.

Create a file named "error_handler.php". Enter following code:


<?php
set_error_handler('error_handler', E_ALL);

function error_handler($errNo, $errStr, $errFile, $errLine){
  if(ob_get_length()) ob_clean();
  
  $error_message = 'ERRNO: ' . $errNo . chr(10).
                   'TEXT: ' . $errStr . chr(10).
				   'LOCATION: ' . $errFile . 
				   ', line ' .$errLine;
  echo $error_message;
  
  exit;
}
?>

Then, modify our "calc.php". Add a line that load the error handler.


<?php
require_once('error_handler.php');
switch($_GET['func']){
  case "add":
    $result = ($_GET['param1'] + $_GET['param2']);
	break;
	
  case "min":
    $result = ($_GET['param1'] - $_GET['param2']);
	break;  
	
  case "div":
    $result = ($_GET['param1'] / $_GET['param2']);
	break;  	
	
  case "mul":
    $result = ($_GET['param1'] * $_GET['param2']);
	break;  	

}

header('Content-type: text/xml');

$dom = new DOMDocument();

$datas = $dom->createElement('datas');
$dom->appendChild($datas);

$calculation = $dom->createElement('calculation');
$calculationText = $dom->createTextNode($result);
$calculation->appendChild($calculationText);

$datas->appendChild($calculation);

$xmlString = $dom->saveXML();
echo $xmlString;

?>



| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting