Php 如何在不刷新的情况下更改页面内容

Php 如何在不刷新的情况下更改页面内容,php,javascript,Php,Javascript,如何在不刷新的情况下更改页面内容。我知道我们需要使用隐藏框架,但我遇到的所有教程都只针对HTML文件教授这一点,如果内容是从PHP文件返回的,在这种情况下我该怎么做?php文件应该回显或返回什么?如果使用隐藏帧,则不会显示内容(因此为“隐藏”),我认为您只是想使用iframe。但这不符合您对“不刷新”的描述,因为您必须刷新帧 当在框架内加载PHP文件时,PHP文件只需生成HTML,生成方式与生成普通页面相同。无论PHP文件是否加载到框架内,都是一样的。您必须使用它,请参阅本教程: 隐藏帧是一

如何在不刷新的情况下更改页面内容。我知道我们需要使用隐藏框架,但我遇到的所有教程都只针对HTML文件教授这一点,如果内容是从PHP文件返回的,在这种情况下我该怎么做?php文件应该回显或返回什么?

如果使用隐藏帧,则不会显示内容(因此为“隐藏”),我认为您只是想使用iframe。但这不符合您对“不刷新”的描述,因为您必须刷新帧

当在框架内加载PHP文件时,PHP文件只需生成HTML,生成方式与生成普通页面相同。无论PHP文件是否加载到框架内,都是一样的。

您必须使用它,请参阅本教程:


隐藏帧是一种设计模式,是整个AJAX设计模式的一部分。这是一个非常高层次的概述,但本质上它是这样工作的:

  • HTML页面中的Javascript使用XMLHTTPRequest对象或隐藏帧或iframe向PHP脚本发出请求。这通常是异步完成的,因此您可以在发出请求时继续处理HTML页面

  • 数据将返回到Javascript。此时,您可以操作页面,并使用各种DOM方法更新页面上的数据


  • 我的很多网站都使用这种方法,谷歌也是如此。如果您想从PHP文件中获取数据,然后动态更新页面,那么您需要以某种方式“导入”PHP文件,而不必重定向整个页面,或者使用iFrame(iFrame也可以,但要复杂得多)。这样做的方式是将文件作为“javascript”文件导入

    下面的代码演示了一个名为“testform”的表单和一个名为“userpost”的文本输入。 当您提交表单时,它将导入一个文件,然后用您输入的内容更新div“outputText”。。。然后等待它。。。所有页面都没有被重定向或刷新

    我已经包含了很多额外的函数来展示如何在同一个DOM上访问所有函数,而不是使用必须使用“top.object”或其他什么的框架


    index.html //通过对象的id获取对象。我们将在PHP导入文件中使用它 Get=函数(id){ 返回(!id)?null:(id的类型==“对象”)?id: (document.getElementById)?document.getElementById(id): (document.all)?document.all[id]: (document.layers)?document.layers[id]:空; } //格式化字符串,使其不会在URL中中断 String.prototype.formatForURL=函数(){ var str=escape(this.replace(//gi,“%20”); str=str.replace(/\&/gi,“%26”)。replace(/\=/gi,“%3D”); str=str.replace(//\//gi,“%2F”) 返回str; } String.prototype.contains=函数(str){ 返回(!str)?false:(this.indexOf(str)>-1); } Object.prototype.killself=函数(){ this.offsetParent.removeChild(this); } //导入脚本 ImportScript=函数(js){ var head=document.getElementsByTagName(“head”)[0]; var script=document.createElement(“脚本”); setAttribute(“type”、“text/javascript”); setAttribute(“语言”、“JavaScript”); script.setAttribute(“字符集”、“utf-8”); //我们添加了is标记,以便在执行“js”文件时立即删除它 script.setAttribute(“id”、“导入”+head.children.length); script.setAttribute(“src”,js+(js.contains(“?”):“?”+”&is=“+head.childrence.length”); head.appendChild(脚本); } //获取并将值发送到php文件 sendInfo=函数(){ var file=“js/myFile.php?userpost=”; file+=document.testform.userpost.value.formatForURL(); 导入脚本(文件); } 此文本将替换为您键入的内容 并提交到上述表格中
    js/myFile.php 获取(“输出文本”).innerHTML=“”; //明文区 document.testform.userpost.setAttribute(“值”、”); //更改信息后从标头中删除文件 获取(“导入”)killself(); 如果我在文本输入“userpost”中输入了“helloworld”,那么 div“outputText”将填充“Hello World”
    删除之前的内容,文本输入将被清除

    我相信他指的是隐藏帧AJAX模式,其中隐藏帧用于处理HTTP请求。当开发人员希望允许用户使用浏览器中的后退/前进按钮返回到不同的状态时,有时会使用此模式。谢谢,这是我需要的答案:) <html> <head> // Get objects by their id. We will use this in the PHP imported file Get = function(id) { return (!id) ? null : (typeof id == "object") ? id : (document.getElementById) ? document.getElementById(id) : (document.all) ? document.all[id] : (document.layers) ? document.layers[id] : null; } // Formats a string so it does not break in a URL String.prototype.formatForURL = function() { var str = escape(this.replace(/ /gi, "%20")); str = str.replace(/\&/gi, "%26").replace(/\=/gi, "%3D"); str = str.replace(/\//gi, "%2F") return str; } String.prototype.contains = function(str) { return (!str) ? false : (this.indexOf(str) > -1); } Object.prototype.killself = function() { this.offsetParent.removeChild(this); } // Import the script ImportScript = function(js) { var head = document.getElementsByTagName("head")[0]; var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.setAttribute("language", "JavaScript"); script.setAttribute("charset", "utf-8"); // we add the is tag so can delete the "js" file as soon as it executes script.setAttribute("id", "import_" + head.children.length); script.setAttribute("src", js + (js.contains("?") ? "" : "?") + "&is=" + head.children.length); head.appendChild(script); } // Get and send value to php file sendInfo = function() { var file = "js/myFile.php?userpost="; file += document.testform.userpost.value.formatForURL(); ImportScript(file); } </head> <body> <div> <form name=testform onsubmit="sendInfo(); return false"> <input type=TEXT name=userpost /> <input type=SUBMIT value=Go /> </form> </div> <div id=ouputText> This text will be replaced by what you type and submit into the form above </div> </body> <html> <?php // Here you can now use functions like mysql_connect() etc. even exec() // ANYTHING! Save them into variables and output them as text which goes // Straight into the javascript! e.g. : // $con = mysql_connect("localhost", "username", "password"); // if($con) { // ... code to retrieve data and save into $variable // } // print "alert(\"$variable\");"; // this alerts the value in variable if(isset($_GET['userpost'])) { $userpost = $_GET['userpost']; ?> Get("outputText").innerHTML = "<?=$userpost; ?>"; <?php } ?> // Clear text area document.testform.userpost.setAttribute("value", ""); // Remove the file from header after info is changed Get("import_<?=$_GET['is']; ?>").killself();