I'm moving from the realm of just JS to php and Ajax. I've dabbled some with PHP in the past. I really appreciate how much help stackoverflow has been in helping me with basic questions.
let says I have a div called #divName
.
I use the following JS for Ajax. Some of this is just pseudo code.
var request = false;
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = false;
}
}
}
if (!request)
alert("Error initializing XMLHttpRequest!");
function getAjaxInfo(<name of the php function???>) { //<<<<< this is the function I need help with
var myFunction= nameOfPHPfunctionIJustGot;
var url = "filename.php?func=" + myFunction;
request.open("GET", url, true);
request.onreadystatechange = updatePage;
request.send(null);
}
function updatePage() {
if (request.readyState == 4) {
if (request.status == 200) {
var response = request.responseText;
document.getElementById("divName").innerHTML = response; //probably going to use Jquery append here, but this example works.
} else
alert("status is " + request.status);
}
}
The I have my fileName.php file:
<?php
function header() { ?>
header content goes here
<?php }
function footer() { ?>
footer content goes here
<?php }
?>
My goal is that when I execute getAjaxInfo()
, I can pull whatever PHP function I want.
So lets say if I do a onClick="getAjaxInfo(header)
" it will get the php header function, apply it to a javascript function, and then apply that to a div.
Any help would be appreciated!