私はJSだけの領域からphpとAjaxに移行しています。私は過去にPHPを少し使ったことがあります。私は、stackoverflowが基本的な質問を手伝ってくれたことに本当に感謝しています。
と呼ばれるdivがあるとしましょう#divName
。
Ajaxには次のJSを使用しています。これのいくつかは単なる擬似コードです。
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);
}
}
私は私のfileName.phpファイルを持っています:
<?php
function header() { ?>
header content goes here
<?php }
function footer() { ?>
footer content goes here
<?php }
?>
私の目標は、実行するときにgetAjaxInfo()
、必要なPHP関数をプルできることです。
したがって、onClick="getAjaxInfo(header)
「」を実行すると、phpヘッダー関数が取得され、javascript関数に適用されてから、divに適用されます。
どんな助けでもいただければ幸いです!