Framework


SUBMITTED BY: Gummzag3400

DATE: Oct. 10, 2023, 3:25 a.m.

FORMAT: PHP

SIZE: 7.4 kB

HITS: 9604

  1. Zend Framework
  2. It's easy, read article how integrate jQuery-PHP with Zend Framework
  3. Initialize
  4. PHP: require jQuery library
  5. require_once 'libraries/jQuery.php';
  6. // many actions
  7. // ...
  8. // get JSON response
  9. jQuery::getResponse();
  10. HTML: initialize jQuery and jQuery.php
  11. JavaScript: call AJAX (example)
  12. // do an ajax post request (example)
  13. jQuery.extend({
  14. php: function (url, params) {
  15. // do an ajax post request
  16. $.ajax({
  17. // AJAX-specified URL
  18. url: url,
  19. // JSON
  20. type: "POST",
  21. data: params,
  22. dataType : "json"
  23. })
  24. }
  25. });
  26. Requirements:
  27. System requirements
  28. PHP 5.2.0 or higher (with JSON extension)
  29. jQuery 1.1.2 or higher
  30. Download:
  31. Google Code
  32. Web Access:
  33. http://code.google.com/p/jquery-php/downloads/list
  34. SVN Command-Line Access:
  35. svn checkout http://jquery-php.googlecode.com/svn/trunk/ jquery-php-read-only
  36. Ads:
  37. Google Ads
  38. Examples:
  39. Should be work...
  40. jQuery
  41. Assign html by ID (click to run):
  42. jQuery('#test0') -> html('new content');
  43. This is PHP code equal JavaScript (click to run):
  44. // set new content to element with ID test0
  45. $('#test0').html('new content');
  46. old content
  47. Assign html and chage CSS by ID (click to run):
  48. jQuery('#test1') -> html('new content')
  49. -> css('backgroundColor' , '#ffff00');
  50. This is PHP code equal JavaScript (click to run):
  51. // set new content to element with ID test1 and change CSS options
  52. $('#test1').html('new content')
  53. .css('backgroundColor' , '#ffff00');
  54. old content
  55. Assign html and chage CSS by Path (click to run):
  56. jQuery('#test2 div') -> html('new content');
  57. jQuery('#test2 div.red') -> html('new content')
  58. -> css('backgroundColor' , '#ff0000');
  59. This is PHP code equal JavaScript (click to run):
  60. // set new content to element by Path and change CSS options by another Path
  61. $('#test2 div') .html('new content');
  62. $('#test2 div.red').html('new content')
  63. .css('backgroundColor' , '#ff0000');
  64. old content
  65. old content
  66. old content
  67. Bind event by Path(click to run):
  68. // event function
  69. function eventAlert(event) {
  70. alert('Run "eventAlert": ' + event.data.test);
  71. }
  72. jQuery('#test3 div') -> bind('click', array('test'=>'answer'), 'eventAlert')
  73. -> css ('cursor', 'pointer')
  74. -> css ('color', '#0000ff')
  75. -> css ('textDecoration', 'underline');
  76. This is PHP code equal JavaScript (click to run):
  77. // set event to elements by Path and change CSS options
  78. $('#test3 div').bind('click', {'test':'answer'}, eventAlert)
  79. .css('cursor', 'pointer')
  80. .css('color', '#0000ff')
  81. .css('textDecoration', 'underline');
  82. Element 1
  83. Element 2
  84. Element 3
  85. Use animate (click to run, restore):
  86. jQuery('#test4 div.hide') -> css ('color', '#ff0000')
  87. -> animate (array('opacity'=>'show'), 1000);
  88. This is PHP code equal JavaScript (click to run):
  89. $('#test4 div.hide').css ('color', '#ff0000')
  90. .animate ({'opacity':'show'}, 1000);
  91. old content
  92. Use animate with callback (click to run, restore):
  93. var eventHideCounter = 0;
  94. function eventHide() {
  95. alert('Run "eventHide" (count:'+ ++eventHideCounter +')');
  96. }
  97. jQuery('#test5 div.hide')-> slideUp(500, 'eventHide');
  98. This is PHP code equal JavaScript (click to run):
  99. $('#test5 div.hide').slideUp(500, eventHide);
  100. content
  101. old content
  102. content
  103. Send a Object as JSON
  104. This is example use json2.js library.
  105. Click here for send a Array to server side:
  106. HTML:
  107. Click here ...
  108. JavaScript:
  109. getJson = function () {
  110. // isn't requried
  111. var args = Array.prototype.slice.call(arguments);
  112. $.php(url,{act:'json',data:JSON.stringify(args)});
  113. }
  114. PHP:
  115. ob_start();
  116. var_dump(json_decode(stripslashes($_REQUEST['data'])));
  117. $data = ob_get_contents();
  118. ob_end_clean();
  119. jQuery('#testjson')->html('
  120. '.$data.'
  121. ');
  122. Send a Form
  123. Form Example:
  124. Field 1:
  125. Field 2:
  126. Field 3:
  127. Result:
  128. HTML:
  129. Field 1:
  130. Field 2:
  131. Field 3:
  132. JavaScript:
  133. formAjax = function () {
  134. $.php(url, $('form#form').serialize());
  135. return false;
  136. }
  137. PHP:
  138. $field1 = isset($_REQUEST['field1'])?$_REQUEST['field1']:'';
  139. $field2 = isset($_REQUEST['field2'])?$_REQUEST['field2']:'';
  140. $field3 = isset($_REQUEST['field3'])?$_REQUEST['field3']:'';
  141. $response = 'Field 1 = "'.htmlentities(stripslashes($field1)).'"
  142. '.
  143. 'Field 2 = "'.htmlentities(stripslashes($field2)).'"
  144. '.
  145. 'Field 3 = "'.htmlentities(stripslashes($field3)).'"
  146. ';
  147. jQuery('#testform')->html($response);
  148. Handle System Events
  149. Create loading image (like under right menu), click to run:
  150. // example registering custom beforeSend function
  151. php.beforeSend = function (){
  152. $('#loading').slideDown('fast');
  153. }
  154. // example registering custom complete function
  155. php.complete = function (){
  156. $('#loading').slideUp('slow');
  157. }
  158. Change default error reporting:
  159. // example registering custom error function
  160. php.error = function (){
  161. alert("Something wrong");
  162. }
  163. // example registering still error function
  164. php.error = function (){
  165. return true;
  166. }
  167. Additional Actions
  168. addMessage():
  169. Return message (click to run):
  170. jQuery::addMessage('Message 1...');
  171. jQuery::addMessage('Message 2...');
  172. Message handler: you can reset default callback function:
  173. // example registering simple callback function for messages
  174. php.messages.defaultCallBack = function (msg, params){
  175. alert ("My messages CallBack func: " + msg);
  176. }
  177. Or create own callback function (click to run):
  178. // example registering simple callback function for messages
  179. php.messages.myCallBack = function (msg, params){
  180. alert ("My messages CallBack func: " + msg);
  181. }
  182. and call it from PHP:
  183. jQuery::addMessage('Message...', 'myCallBack');
  184. addError():
  185. Return error (click to run):
  186. jQuery::addError('Error 1...');
  187. jQuery::addError('Error 2...');
  188. Errors handler equal to messages handler
  189. addData():
  190. Return data (click to run):
  191. jQuery::addData('key1', 'value1');
  192. jQuery::addData('key2', 'value2', 'myCallBack');
  193. evalScript():
  194. Eval script (click to run):
  195. jQuery::evalScript('alert("Eval script...");');
  196. Change Callback functions
  197. Show "Loading..." status
  198. Element with id = "loading" - is our "Loading..." image:
  199. Change error callback
  200. Change to alert (O_o):

comments powered by Disqus