photo

Richard Wisniewski

shared this question
5 years ago

Statistics

0
Comments
263
Views

Relates to

Share

1
votes

proper use for ajax calls

I am struggling to figure out how to get ajax to to work as a framework addon. I have this simple code away from the framework and it works as expected:

in my addon.inc.php:

  1. function mycomps_add_listing() {
  2. global $jscript;
  3. $jscript .= '<script language="javascript" type="text/javascript">
  4. <!--
  5. //Browser Support Code
  6. function ajaxFunction(){
  7. var ajaxRequest; // The variable that makes Ajax possible!
  8. try{
  9. // Opera 8.0+, Firefox, Safari
  10. ajaxRequest = new XMLHttpRequest();
  11. } catch (e){
  12. // Internet Explorer Browsers
  13. try{
  14. ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
  15. } catch (e) {
  16. try{
  17. ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
  18. } catch (e){
  19. // Something went wrong
  20. alert("Your browser broke!");
  21. return false;
  22. }
  23. }
  24. }
  25. // Create a function that will receive data sent from the server
  26. ajaxRequest.onreadystatechange = function(){
  27. if(ajaxRequest.readyState == 4){
  28. document.myForm.button.type = "text";
  29. document.myForm.button.value = ajaxRequest.responseText;
  30. }
  31. }
  32. ajaxRequest.open("GET", "/v4.1/addons/mycomps/serverTime.php", true);
  33. ajaxRequest.send(null);
  34. }
  35. -->
  36. </script>';
  37. $display = '<form name="myForm">
  38. <input type="button" onClick="ajaxFunction();" name="button" value="Click Me!" />';
  39. return $display;
  40. }

My php file simply returns an echo statement.

The script will works fine when I use it outside of the addon environment.

I simply am trying to design a button that will run a php file (mysql query) on click.

I would be open to alternative approaches also.

Thank you for taking the time to read my post.

Leave Comment

photo

Attach files...

The file must be a jpg, gif, png, bmp, ico, pdf, doc, rtf, txt, zip or rar no more than 20M