我正在尝试以下代码来更新名为"content1"的div内的外部内容
ajax.js:
var ajaxdestination="";
function getdata(what,where) { // get data from source (what)
try {
xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) { /* do nothing */ }
document.getElementById(where).innerHTML ="<center><img src='loading.gif'></center>"; // Define the destination DIV id, must be stored in global variable (ajaxdestination)
ajaxdestination=where;
xmlhttp.onreadystatechange = triggered; // when request finished, call the function to put result to destination DIV
xmlhttp.open("GET", what);
xmlhttp.send(null);
return false;
}
function triggered() { // put data returned by requested URL to selected DIV
if (xmlhttp.readyState == 4) if (xmlhttp.status == 200)
document.getElementById(ajaxdestination).innerHTML =xmlhttp.responseText;
}
在我的div中,我将"page1a.php"包含在php中,wich从我的数据库中输出一个值,并包含一个到"code1a.php"的链接,在那里我有一个更新该值的php代码。(这只是一个测试,将来将不仅仅更新一个值)。
<a href="javascript:void(0)" onClick="getdata('tmpa/code1a.php','content1');">update value</a>
在code1a.php中,我有一个更新数据库的php代码,在数据库更新后,有没有办法再次用"page1a.php"更新我的div(content1)?我尝试了我能想到的一切,并在网上搜索了几天,但没有找到解决我问题的方法。
该脚本位于:http://www.battrewebbsida.se/index2.php
有很多变体可以做到这一点,您的解决方案并不是最好的解决方案,但这里是修改后的javascript代码,这正是您想要的。
通过Javascript
var ajaxdestination="";
var tmpcache = '';
function getdata(what,where) { // get data from source (what)
try {
xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) { /* do nothing */ }
tmpcache = document.getElementById(where).innerHTML;
document.getElementById(where).innerHTML ="<center><img src='loading.gif'></center>"; // Define the destination DIV id, must be stored in global variable (ajaxdestination)
ajaxdestination=where;
xmlhttp.onreadystatechange = triggered; // when request finished, call the function to put result to destination DIV
xmlhttp.open("GET", what);
xmlhttp.send(null);
return false;
}
function triggered() { // put data returned by requested URL to selected DIV
if (xmlhttp.readyState == 4) if (xmlhttp.status == 200)
document.getElementById(ajaxdestination).innerHTML =tmpcache;
}
通过PHP
在"code1a.php"中进行更新后,将标头位置发送到您的第一个"page1a.php"文件
header("Location: ".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'/page1a.php');
注意:不要忘记脚本顶部的ob_start()
。