onclick 替换 php 包含的文件
我不确定我想做的事情是否可能,但它就是这样。
我有一个标头,该标头内是“login.php”的 php 包含内容。
“login.php”上是一个将用户带到“forgot.php”的链接。
我想做的是,用户不是被带到“forgot.php”,而是用“login.php”刷新页面,包括替换为“forgot.php”,或者进行某种内容切换,排序就像使用 Iframe 一样。
我不想将用户带到新页面,我只想切换标题中显示的内容。
感谢您提供的任何帮助,代码示例表示赞赏。
I am not sure if what I am trying to do is possible but here it is.
I have a header, inside that header is a php include for "login.php"
On "login.php" is a link that takes the user to "forgot.php".
What I would like to do is, instead of the user being taken to "forgot.php" is to just refresh the page with "login.php" include replaced with "forgot.php" or do some sort of content switch out, sort of like using an Iframe.
I dont want to bring the user to a new page, I just want to switch out the content displayed inside my header.
Thanks for any help you can provide, code samples appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您尝试在不重新加载页面的情况下完成此操作,则需要使用 AJAX。
如果你只想保留login.php,你也许可以这样做:
使用 php 类似
If you are trying to accomplish this without reloading the page you will need to use AJAX.
If you want to just keep the login.php you can perhaps do something like:
with php something like
在页面显示在用户浏览器中之前,PHP 会被完整解析,因此 JavaScript 中的
onclick()
或onsubmit()
等功能(客户端-side 语言)在 PHP 中不可用。有几种可能的解决方案:
1) 使用 AJAX 向服务器提交查询,并用结果替换页面上的 HTML 内容。
2) 正如您提到的,使用 iFrame。
3) 在您的
login.php
页面上有一个隐藏的,其中包含
forgot.php
的 HTML >,并使用简单的 JavaScriptonclick()
方法来交换页面内容。在这种情况下,两个“页面”实际上都位于login.php
的同一个文件中。PHP is parsed in it's entirety before the page is displayed in a user's browser, therefore, things such as
onclick()
oronsubmit()
that are featured in JavaScript (a client-side language) are not available in PHP.There would be a few solutions possible:
1) Use AJAX to submit a query to the server and replace the HTML content on the page with the result.
2) As you mentioned, use iFrames.
3) Have a hidden
<div>
on yourlogin.php
page that contains the HTML forforgot.php
, and use a simple JavaScriptonclick()
method to swap the page contents. In this case, both "pages" would actually all be in the same file oflogin.php
.我可以想到两件事:
假设你的login.php和forget.php之间的差异不是太大,因为你不更改页面,我会做的就是将html放在forgot.php中就在login.php 的html 下方,隐藏login.php html 并显示forget.php 内容。
示例:
Javascript:
否则,您可以使用 ajax 调用页面并插入必要的元素。这会产生明显的停顿。
I can think of two things:
What I would do, assuming that the differences between your login.php and forgot.php aren't too different because you don't to change the page, is to put the html for the forgot.php just below the html for the login.php and hide the login.php html and show the forgot.php content.
example:
Javascript:
Otherwise, you could use an ajax call to the page and insert the necessary elements. This would create a noticeable pause.
您无法从 javascript 更改 include 语句,因为当您在浏览器中看到页面时脚本已经执行。
使用ajax动态更改页面内容而无需刷新。
如果您使用 jquery,代码将非常简单:
You can't change the include statement from javascript because the script was already executed by the time you see your page in the browser.
Use ajax to dinamically change the content of the page without refreshing.
If you're using jquery the code would be pretty simple: