onclick 替换 php 包含的文件

发布于 2024-12-07 12:37:15 字数 282 浏览 0 评论 0原文

我不确定我想做的事情是否可能,但它就是这样。

我有一个标头,该标头内是“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

纵情客 2024-12-14 12:37:15

如果您尝试在不重新加载页面的情况下完成此操作,则需要使用 AJAX。

如果你只想保留login.php,你也许可以这样做:

<a href="login.php?p=forgot">link</a>

使用 php 类似

<?
if ( isset($_GET['p']) && $_GET['p']=="forgot") {
    include('forgot.php');
} else {
    include('login.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:

<a href="login.php?p=forgot">link</a>

with php something like

<?
if ( isset($_GET['p']) && $_GET['p']=="forgot") {
    include('forgot.php');
} else {
    include('login.php');
}
箜明 2024-12-14 12:37:15

在页面显示在用户浏览器中之前,PHP 会被完整解析,因此 JavaScript 中的 onclick()onsubmit() 等功能(客户端-side 语言)在 PHP 中不可用。

有几种可能的解决方案:

1) 使用 AJAX 向服务器提交查询,并用结果替换页面上的 HTML 内容。

2) 正如您提到的,使用 iFrame。

3) 在您的 login.php 页面上有一个隐藏的

,其中包含 forgot.php 的 HTML >,并使用简单的 JavaScript onclick() 方法来交换页面内容。在这种情况下,两个“页面”实际上都位于 login.php 的同一个文件中。

PHP is parsed in it's entirety before the page is displayed in a user's browser, therefore, things such as onclick() or onsubmit() 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 your login.php page that contains the HTML for forgot.php, and use a simple JavaScript onclick() method to swap the page contents. In this case, both "pages" would actually all be in the same file of login.php.

你的心境我的脸 2024-12-14 12:37:15

我可以想到两件事:

假设你的login.php和forget.php之间的差异不是太大,因为你不更改页面,我会做的就是将html放在forgot.php中就在login.php 的html 下方,隐藏login.php html 并显示forget.php 内容。

示例:

<div id = "login">
    <!-- Login HTML -->
</div>
<div id = "forgot" style = "display:none" >
    <!-- forgot password HTML -->
</div>
<input type="button" value="Forgot Password?" onclick="forgot()" />

Javascript:

function forgot(){
    document.getElementById('login').style.display='none';
    document.getElementById('forgot').style.display='block';
}

否则,您可以使用 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:

<div id = "login">
    <!-- Login HTML -->
</div>
<div id = "forgot" style = "display:none" >
    <!-- forgot password HTML -->
</div>
<input type="button" value="Forgot Password?" onclick="forgot()" />

Javascript:

function forgot(){
    document.getElementById('login').style.display='none';
    document.getElementById('forgot').style.display='block';
}

Otherwise, you could use an ajax call to the page and insert the necessary elements. This would create a noticeable pause.

牵你的手,一向走下去 2024-12-14 12:37:15

您无法从 javascript 更改 include 语句,因为当您在浏览器中看到页面时脚本已经执行。

使用ajax动态更改页面内容而无需刷新。
如果您使用 jquery,代码将非常简单:

<script type="text/javascript">
$(document).ready(function() {
    $('#link').click(function() {
        $.get('script.php', function(data) {
            $('#divOfContainer').html(data);
        });
    });
});
</script>
<div id="divOfContainer"><!-- the content to be fetched with ajax will be put here --></div>
<a href="#" id="link">Link</a>

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:

<script type="text/javascript">
$(document).ready(function() {
    $('#link').click(function() {
        $.get('script.php', function(data) {
            $('#divOfContainer').html(data);
        });
    });
});
</script>
<div id="divOfContainer"><!-- the content to be fetched with ajax will be put here --></div>
<a href="#" id="link">Link</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文