HOW TO USE PHPLIVEX ? (Version 2.1, 2.2, 2.3)

The text in blue means that it's a property of version 2.3!

ConstructorDescription
PHPLiveX($functions) Starts to handle ajax requests. Exports php functions for ajax.
$functions parameter: Optional. Its value is function names (if more than 1, seperated by commas);

MethodDescription
Export("functionNames,...") Exports the functions for ajax. Can only be called over run method.
Parameter Value:
1) Function names (if more than 1, seperated by commas).
2) "ALL" for all user defined functions.
ExportClassFunctions("objectName->methodName,...") Exports class functions for ajax. Can only be called over run method.
Parameter Value:
objectName (like myObject in "$myObject = new myClass();"). methodName is name of the class function. (if more than 1, seperated by commas);
ExportClass("objectNames,...") Exports all functions of the classes for ajax. Can only be called over run method.
Parameter Value:
Object names (if more than 1, seperated by commas);
Run() Creates javascript codes to handle ajax requests. Must be called between script tags

At first, you must create the phplivex object. To export your functions, you can use both constructor function and export method.

<?

function myFunction($text){
   return $text;
}

$plx = new PHPLiveX("myFunction");
### OR
$plx = new PHPLiveX();
$plx->Export("myFunction");

?>

If you want to export a class function, you must create the instance at first. Then you can export its functions as in the example. Note that this property uses session so it needs session_start() ahead.

<?

class myClass(){
    var $Text;
  
    function myClass($text){
        $this->Text = $text;
    }

    function setText(){
        return $this->Text;
    }
}

$test = myClass("it's very easy-to-use");
	
$plx->ExportClassFunctions("test->setText");
### OR
$plx->ExportClass("test");

?>

You must call run method between script tags anywhere on the page. But give attention that methods under run method does not work. So it's better to call it at the end of the page.

<script language="javascript">
<? $plx->Run(); ?>
</script>

You can call your exported functions via javascript. The global functions are called directly with its name (e.g. myFunction) but the class functions are called with #ưnstanceName__functionName# (e.g. test__setText).
In order to call these functions, you must use necessary parameters below within one last argument. But all of these properties are optional. If you don't use any of them (pass the argument empty), the function runs without returning a value.

Parameter Description
type Can take only one value "r". If you want to get returned value of the function (e.g. pass the value to a variable). And if this parameter is used, the others cannot be used. Note that if this parameter is used, the ajax request runs synchronously. (During the process, no other function runs.)
target Takes the ID value of a DOM element and returned value of the function is printed to its innerHTML. If this parameter is used as "target=alert", the returned value is alerted. If it takes more values, these are given in this way: "target=id1:id2:id3".
mode The default value is "rw"(rewrite) which means rewriting the value to the target. If you use "aw" (append), the value is appended to the initial content of target dom element. If you use "asw", the value is added before the initial content.
preload Can take the ID value of a DOM element used for preload. It must be hidden(style="visibility:hidden") initially. During the process, the foregoing element is made visible and then hidden when the process finishes. Note that this parameter can not be used with type together.
method Can take "post" and "get" values. These are the http-methods of the xmlhttp request. Default value is get. Added in version 2.1.
onFinish The value must be the name of a js function. This function runs when the response of request is taken but before it is written to page. In addition, the response code is passed as an argument to the function. If it returns something, the response code to be written is replaced with this returned value.
onUpdate The value must be the name of a js function. This function runs when the response of request is taken but after it is written to page. In addition, the response code is passed as an argument to the function.
hideContent Can take 0 (default) and 1 values. "1" value means that during the preloading, the content of target dom element is emptied.
interval Can take a time interval in milliseconds. With this interval, the function is called repeatedly.
myPreload Provides users to create their own preloading systems. Two js function are written; one is called before the request and the other is called after the request. This property must be used in this way: "beforeRequestFunctionName:afterRequestFunctionName".

<a href="javascript:myFunction('it is an example', 'target=showText,preload=loadSpan');">
test it</a>

<span id="showText"></span>
<span id="loadSpan" style="display:none;">Loading...</span>


<!--
Pay attention here! 
Versions before 2.3 use "visibility:hidden" attribute but version 2.3 uses "display:none".
-->


The function ahead prints "it is an example" to the innerHTML of the DOM element(span) whose id is "showText". It also uses preloading by displaying "Loading..." text during the process. The mode parameter is "rw" by default. By each click on the anchor, foregoing span shows the same text. But if "mode=aw" parameter is added, the text will append to the initial value. For example; after 3 calls, innerHTML of the span becomes "it is an exampleit is an exampleit is an example". If you don't use preload parameter, there won't be any preloading. In addition, you don't have to use target method. For example; supposing that your function just updates database, you don't define a target and it doesn't print anything.

<script>
function changeResponse(resp){
	return "This text is added to returned value of myFunction. " + resp;
}

function showContent(resp){
	alert(document.getElementById("showText").innerHTML);
}
</script>

<a href="javascript:
myFunction('It is another example', 
'target=showText,preload=loadSpan,onFinish=changeResponse,onUpdate=showContent');
>test it</a>


The function above prints "This text is added to returned value of myFunction. It is another example" to the target dom element (showText). When it is done, an alert box containing this text appears.

<script language="javascript" >
var output = test__setText("an example", "type=r");
# ...
</script>

As this function use "type=r" parameter, the value it returns may be passed to a variable.