Send array through ajax tools request to js


/ Published in: PHP
Save to your folder(s)

Send array through ajax tools request to js


Copy this code and paste it in your HTML
  1. $array //has our data
  2.  
  3. //in the tools file
  4. array_walk ($array, 'utf8_encode_array'); //could lead to UTF-8 problems..
  5.  
  6. echo json_encode($array);
  7.  
  8. function utf8_encode_array (&$array, $key) { //encodes the array recursively to utf8
  9. if(is_array($array)) {
  10. array_walk ($array, 'utf8_encode_array');
  11. } else {
  12. $array = utf8_encode($array);
  13. }
  14. }
  15.  
  16. /*********************************************/
  17. //Ajax request to the tools file:
  18. ?>
  19. <script type="text/javascript">
  20. $.ajax({
  21. type: "POST",
  22. url: '<?php echo Loader::helper('concrete/urls')->getToolsURL('path_to_tools_file.php', 'packagename_or_empty'); ?>',
  23. data: data, //send data as POST (data is an object)
  24. success: function(msg) {
  25. try {
  26. var myObj = jQuery.parseJSON(msg);
  27.  
  28. myObj.images;
  29. myObj.name;
  30. myObj.text[0 .. n];
  31.  
  32. }catch(e){
  33. alert(e.error);
  34. }
  35. }
  36. });
  37. </script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.