php代码后重定向回页面(redirect back to page after php code)

编程入门 行业动态 更新时间:2024-10-28 18:35:37
php代码后重定向回页面(redirect back to page after php code)

我正在尝试以下代码来更新名为“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.

更多推荐

本文发布于:2023-07-20 11:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1198656.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   代码   页面   php   page

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!