For now clsJSPHP is in an alpha-phase. So test it and send me feedback (kutulik@gmx.de)!
Installation
Make a reference to the core javascript in the header of your html document:
code:
<script src="[PATH_TO_clsJSPHP.php]?jsphp=[PATH_TO_clsJSPHP.php]" language="JavaScript"></script> and you are ready to start!
Usage
clsJSPHP doesn't register any php functions that you can call with javascript on the client-side. clsJSPHP request a php-file and handle the returned output.How should the code look like?
THE HTML-SIDE:
code:
client-side:
<html>
<head>
<script src="[PATH_TO_clsJSPHP]?jsphp=[PATH_TO_clsJSPHP]" language="JavaScript"></script>
</head>
<body>
<input type="button" onClick="jsphp_exec('[PATH_2_PHPFILE]','[PARAMETER]',[ASYNC=true/false]);" />
</body>
</html>
[PATH_TO_clsJSPHP]
relative Path to clsJSPHP (e.g. './clsJSPHP.php' if it is in the same folder)
[PATH_2_PHPFILE]
relative Path to the clsJSPHP.php class (e.g. './ajaxhandle/ajaxinclude.php')
[PARAMETER]
The parameters you want to tranfer to the php file (e.g. 'id=1&user=frank')
[ASYNC]
Parameter if you want to request the php file asyncron or not (default is true)
THE SERVER-SIDE:
code:
server-side (the php file you are calling):
<?
//Make sure this file is called by clsJSPHP
if(!defined('JSPHP_INC')) die('YOU ARE NOT ALLOWED TO ACCESS THIS FILE DIRECTLY');
$jsphp->addAlert('This is a test');
$jsphp->output();
/*
To get transfered parameters use $_POST or $_REQUEST !!
Parameters are send with POST because you can transfer more data with it.
*/
?>
And that's it. After pressing the button (html-side) a message ('This is a test') will appear. Quite simple right!?
You have the following methods to manipulate the HTML-Side from the requested php file:
$jsphp->setStyle($targetid,$attribute,$style);
$jsphp->setAttributte($targetid,$attribute,$value);
$jsphp->innerHTML($targetid,$text);
$jsphp->addAlert($msg);
Quick javascript functions on the html-side
jsphp_gcontent(url,prm)
returns output as string from requested php file (url)
jsphp_shtml(id,url,prm,async)
set inner html of an element (with the given id) with the output from requested php file (url)
jsphp_svalue(id,url,prm,async)
set value of an element (e.g. input-field with the given id) with the output from requested php file (url)
More features will be included in the future.
Examples
Example 1
This example shows how to get just raw php output:
code:
alert(jsphp_gcontent('ajaxinclude.php','prm1=1')); Example 2
This example shows how to set an input-value with php output:
code:
jsphp_svalue('txtval','ajaxinclude.php','prm2=1&index=2'); Example 3
This example shows how to set inner html of an element with php output:click button!
code:
jsphp_shtml('cdiv','ajaxinclude.php','prm3=1'); Example 4
This example shows how to modify html with requested php file:Sample text...
code:
jsphp_exec('ajaxinclude.php','prm4=1');
ajaxinclude.php:
if(isset($_REQUEST['prm4'])){
$jsphp->addAlert('textcolor will be modified...');
$jsphp->setStyle('cmdiv','color','red');
$jsphp->output();
}
copyright (c) 2005 by Artur Heinze - published under the LGPL license