php代码后重定向回页面

时间:2013-12-11 15:17:55

标签: php ajax

我正在尝试以下代码来更新名为" 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中,我包括&#39; page1a.php&#39;使用php,它会从我的数据库中输出一个值,并包含指向&#39; code1a.php&#39;的链接。我有一个更新此值的PHP代码。 (这只是一个测试,将来不仅仅会更新一个值。)

<a href="javascript:void(0)" onClick="getdata('tmpa/code1a.php','content1');">update value</a>

在code1a.php里面我有一个更新数据库的php代码,在数据库更新后,有没有办法用&#39; page1a.php&#39;更新我的div(content1)。再次? 我已经尝试了我能想到的一切,并在网上搜索了几天,但没有找到解决我问题的方法。

该脚本位于:http://www.battrewebbsida.se/index2.php

1 个答案:

答案 0 :(得分:0)

有很多变种可以做到这一点,你的解决方案不是最好的,但是这里修改了你的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()

相关问题