DOAP - Remote Abstract Data Access Protocol by Dave

<< First Simple ExampleContentsArrays >>


DOAP functions can have parameters passed to them. At the client end these are put into an array and passed to the method.

When a function is registered on the DOAP Server you specify a minimum and maximum number of parameters. If the function is called with too many or too few an error will be generated.

Please note DOAP currently supports a maximum of 10 parameters

The passing and handling of parameters is shown in the following example (click here to see it run).

Server-Side DOAP Server PHP
ob_start(); // ensure no messy output from includes
// Our Addition Function - takes two numbers and adds them
function addition($one, $two)
	// sanitise user input just in case...
	if (!is_numeric($one) || !is_numeric($two)) return 0; 
	// return the sum
	return $one + $two;
require(""); // include the xmlCreate library
require(""); // include the DOAP Server library
$doap = new DOAP_Server(); // create a DOAP Server object
// register the addition function with a minimum and maximum of 2 parameters
// e.g. there must be two parameters
ob_clean(); // clean any messy output from includes
$doap->Process(); // get DOAP Server to process the request

The above code registers the addition() function with the DOAP Server requiring a minimum and maximum of two parameters. The function itself just checks the data is numeric and if so returns the sum of the two values.

Client-Side DOAP Server HTML/JavaScript
<TITLE>DOAP Example 2: Parameters</TITLE>
<!-- Include the DOAP Client JavaScript -->
<SCRIPT TYPE="text/javascript" SRC="doap.js"></SCRIPT>
<SCRIPT TYPE="text/javascript">
var doap = new DOAP(); // create the DOAP Client object
doap.url = "example2.php"; // set the DOAP Server URL
function addition() // this will get parameters and call addition() on the server
		var params = Array(); // parameters are passed in an array
		params.push(; // the first number
		params.push(document.forms.addform.two.value); // the second number
		var result ='addition',params); // call addition() on server passing params
	catch(e) // catch any errors
		// deal with any errors if it's a DOAP error or any other type of exception
		if(e instanceof DOAP_Error) alert("DOAP Error "+e.code+": "+e.desc);
		else alert("Unknown Error: "+e);
		return; // and exit the function if an error was encountered
	// otherwise put the result into the results DIV
	document.getElementById('results').innerHTML = result;
<DIV ID="results" CLASS="result_div">Results will appear here BY MAGIC!</DIV>
<BR /><BR />
<!-- This time we have a form to input two numbers which will be added by the server -->
<FORM ID="addform" onSubmit="return false;"> <!-- Start the form -->
<!-- The fields -->
<!-- The Submit Button which in it's onClick calls the JavaScript addition() function -->
<INPUT TYPE="SUBMIT" VALUE="Calculate" onClick="addition();">
<BR /><BR />
<!-- And a link to clean the results div back if needed -->
<A HREF="#" onClick="document.getElementById('results').innerHTML='<BR />';">Clear Results DIV</A>

The client script initialises DOAP and displays a form to input two values. When the button is clicked the addition() function is called which puts the two values into a parameter array and does a for the addition function.

<< First Simple ExampleContentsArrays >>