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:
- function mycomps_add_listing() {
- global $jscript;
- $jscript .= '<script language="javascript" type="text/javascript">
- <!--
- //Browser Support Code
- function ajaxFunction(){
- var ajaxRequest; // The variable that makes Ajax possible!
- try{
- // Opera 8.0+, Firefox, Safari
- ajaxRequest = new XMLHttpRequest();
- } catch (e){
- // Internet Explorer Browsers
- try{
- ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
- } catch (e) {
- try{
- ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
- } catch (e){
- // Something went wrong
- alert("Your browser broke!");
- return false;
- }
- }
- }
- // Create a function that will receive data sent from the server
- ajaxRequest.onreadystatechange = function(){
- if(ajaxRequest.readyState == 4){
- document.myForm.button.type = "text";
- document.myForm.button.value = ajaxRequest.responseText;
- }
- }
- ajaxRequest.open("GET", "/v4.1/addons/mycomps/serverTime.php", true);
- ajaxRequest.send(null);
- }
- -->
- </script>';
- $display = '<form name="myForm">
- <input type="button" onClick="ajaxFunction();" name="button" value="Click Me!" />';
- return $display;
- }
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