我正在尝试以下代码来更新名为“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里面我用php包含'page1a.php',它从我的数据库中输出一个值并包含一个'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
I am trying the following code to update external content inside a div named "content1"
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; }Inside my div I include 'page1a.php' with php, wich outputs a value from my database and contains a link to 'code1a.php' where I have a php code that updates this value. (This is just a test and will do more than update a value in the future).
<a href="javascript:void(0)" onClick="getdata('tmpa/code1a.php','content1');">update value</a>Inside code1a.php where I have a php code that updates my database, after the database has been updated, is there a way to update my div (content1) with 'page1a.php' again? I have tried everything i could think of and search the web for a few days, but not found a solution to my problem.
The script can be found on: 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() 。
There are many variants to do this, your solution isn't best to do it, but here's the modified your javascript code, which is that what you want.
By 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; }By PHP
after doing your updates in 'code1a.php' send header location to your first 'page1a.php' file
header("Location: ".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'/page1a.php');NOTE: dont forget about ob_start() at the top of script.
更多推荐
发布评论