提交表单后仅通过 PHP 刷新 1 DIV
我的页面上有一个评论板,我可以根据用户所在的页面,使用 ChangeTopic() 函数中的 XMLHttpRequest 加载不同的主题。我最初在提交表单 php 的末尾有这个:
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_POST['page']);
问题是我不想刷新整个页面,只刷新包含消息的 DIV。我尝试通过将其插入带有 echo 的脚本标签内来运行 changeTopic() 函数。其一,我无法获取 .$_GET['topic']。在 echo 内部工作,即使我首先创建了它的变量,但我也尝试通过硬插入可能的值之一来运行该函数,结果如下:
1)当消息部分刷新正确时,我丢失了表单,因为它包含在index.html,而我只使用查询字符串从外部 gettopic.php 加载消息。
2)我得到了一个奇怪的结果,我丢失了一个完全加载到完全不同的 div 中的外部文件。该文件更改了主页的哈希值,每次刷新时都会检查该哈希值,并根据哈希值加载正确的文件,因此使用整个页面刷新永远不会导致这种情况。
// 编辑
function changeTopic(topic) {
if (topic=="") {
document.getElementById("messagelist").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("messagelist").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST", "gettopic.php?t="+topic,true);
xmlhttp.send();
}
我页面上的主要应用程序是我用 RaphaelJS 完成的 SVG 地图。用户可以从此 SVG 地图将信息页面加载到另一个 div“info”中。这些页面加载了类似的功能,除了更改#hash 并运行changeTopic() 之外,还可以更改留言板,以便人们可以就每个主题进行对话。
PHP 表单采用正常填写的信息以及由用户正在浏览的当前页面设置的隐藏“pageid”,并将其发送到数据库。不同的消息按此pageid排序,因此changeTopic()函数仅带来正确的消息:gettopic.php?t=topic('pageid')。
提交表单后,我只想刷新消息部分并清除表单。目前,要么是整个页面刷新(用户失去了他们在 SVG 地图上的位置),要么是部分刷新,我失去了表单(而是得到了一个空白点),并且奇怪的信息页面丢失了。
I have a comments board on my page, to which I load different topics according to the page the user is on with XMLHttpRequest in a changeTopic() function. I originally had this at the end of my submit form php:
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_POST['page']);
The problem is that I don't want to refresh the whole page, only the DIV that contains the messages. I tried running the changeTopic() function by inserting it inside script tags with echo. For one, I couldn't get .$_GET['topic']. working inside echo even if I made a variable of it first, but also I tried running the function by hard inserting one of the possible values with the following results:
1) While the messages section refreshed right, I lost the form as it's contained in the index.html while I only load the messages from an external gettopic.php with query string.
2) I got a weird result where I lost an external file that was loaded into a completely different div altogether. This file changes the hash of the main page, which is checked with every refresh and the right file is loaded according the hash, so using the whole page refresh never resulted in this.
// EDIT
function changeTopic(topic) {
if (topic=="") {
document.getElementById("messagelist").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("messagelist").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST", "gettopic.php?t="+topic,true);
xmlhttp.send();
}
The main application on my page is a SVG map which I've done with RaphaelJS. The user can load information pages into another div 'info' from this SVG map. The pages are loaded with a similar function which in addition changes the #hash and runs the changeTopic() as well to change the message board so people can have a conversation about each topic.
The PHP form takes the normal filled info as well as the hidden 'pageid' which is set by the current page the user is browsing, and sends it to the database. The different messages are sorted by this pageid so the changeTopic() function only brings the right messages: gettopic.php?t=topic ('pageid').
After submitting the form I'd like only the messagespart to refresh and the form to clear. At the moment it's either a whole page refresh (user looses their position on the SVG map) or partial refresh where I lose the form (get a blank spot instead) and that weird information-page missing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以做这样的事情:
并且
你可以添加你的innerHTML东西:)
gettopic.php
应该回显类似的东西:
并且你可以通过调用来访问它,
这样你就可以通过做类似的事情来简单地构建你的innerHTML东西
you can do something like this:
and in
you can add your innerHTML stuff :)
the gettopic.php
should then echo something like:
And the you can access this by calling
so you can simply build your innerHTML stuff by doing something like
使用 jQuery - 很棒的工具。它可能看起来像
这样!
Use jQuery - great tool. It could look like
and that's it !
我对你在做什么感到很困惑,但 XMLHttpRequest 应该是在readystatechange处理程序中运行changeTopic()的那个。
I'm quite confused about what you are doing, but XMLHttpRequest should be the one running changeTopic() in the readystatechange handler.