热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在PHP中链接文件?-HowdoIlinkfilesinPHP?

IhavetwoPHPfilesthatIneedtolink.HowcanIlinkthefilestogetherusingPHP?TheeffectI

I have two PHP files that I need to link. How can I link the files together using PHP? The effect I want is to have the user click a button, some information is proccessed on the page, and then the result is displayed in a different page, depending on the button the user clicked.Thanks

我有两个PHP文件,我需要链接。如何使用PHP将文件链接在一起?我想要的效果是让用户单击一个按钮,在页面上执行一些信息,然后结果显示在不同的页面中,具体取决于用户单击的按钮。谢谢

3 个解决方案

#1


2  

I've interpreted your question differently to the others.

我以不同的方式解释你的问题。

It sounds to me like you want to create a page with two buttons on it and execute one of your two existing PHP files, depending on which button was pressed.

听起来,我想要创建一个带有两个按钮的页面,并根据按下的按钮执行两个现有PHP文件中的一个。

If that's right, then here's a simple skeleton to achieve that. In this example, page_1.php and page_2.php are your two existing PHP files.

如果这是正确的,那么这是一个简单的骨架来实现这一目标。在此示例中,page_1.php和page_2.php是您现有的两个PHP文件。

Note if you're doing a lot of this stuff, you probably want to read up on the MVC (Model-View-Controller) pattern and/or try some of the popular PHP frameworks available. It's beyond the scope of this question, but basically both those things will give you a good foundation for structuring your code so that things stay managable and don't become a mess.

请注意,如果你正在做很多这样的事情,你可能想要阅读MVC(模型 - 视图 - 控制器)模式和/或尝试一些流行的PHP框架。这超出了这个问题的范围,但基本上这两个方面都将为您构建代码提供良好的基础,以便事情保持可管理性并且不会变得混乱。



    
    

Note: I've included only the relevant HTML and PHP to illustrate the point. Obviously you'd add , and tags, and likely shuffle and modularize the PHP a bit, depending on what you're going to add.

注意:我只包含了相关的HTML和PHP来说明这一点。显然你会添加,和标签,并且可能会稍微改组和模块化PHP,具体取决于你要添加的内容。

UPDATE: I should also add that if either of your existing PHP files contain forms that POST to themselves, you may want to change the include to a redirect. That is:

更新:我还应该补充一点,如果您现有的PHP文件中包含自己POST的表单,您可能希望将include更改为重定向。那是:

include 'file_1.php';

would become:

header('Location: http://mysite.com/file_1.php');

It's hard to know what to recommend without knowing the nature of your existing files.

在不知道现有文件的性质的情况下,很难知道推荐什么。

EDIT: I'm responding to the OP's second post this way because I don't have enough reputation to comment. Which line number do you get the unexpected ; error? If I had to guess, I would say check that you're using a : (colon) and not ; (semi-colon) at the end of the case 'show_file_1' and case 'show_file_2' lines.

编辑:我正在以这种方式回应OP的第二篇文章因为我没有足够的声誉来发表评论。您得到意外的哪个行号;错误?如果我不得不猜测,我会说检查你使用的是:(冒号)而不是; (“分号”)在案例“show_file_1”和案例“show_file_2”行的末尾。

#2


8  

It sounds like you might want an HTML form:

听起来你可能想要一个HTML表单:


     ...

Then $_POST["foo"] will contain the value of that input in other_file.php.

然后$ _POST [“foo”]将包含other_file.php中该输入的值。

#3


0  

In PHP's most basic setup, you can use two independent files: one generates the form, the second handles the response. You can also handle this with one file that checks to see if the form as been posted.

在PHP最基本的设置中,您可以使用两个独立的文件:一个生成表单,第二个处理响应。您还可以使用一个文件来处理此问题,该文件检查表单是否已发布。

 if (isset($_POST['foo'])) { ... }

or

 if ($_SERVER['REQUEST_METHOD'] == 'POST') { ... }

Once you get beyond this level, there are must cleaner ways to build the scripts so that you keep your logic and your interfaces separate, but I'm guessing from the question that you're not there yet.

一旦超出这个级别,就必须有更简洁的方法来构建脚本,以便保持逻辑和界面分离,但我猜测你还没有。


推荐阅读
author-avatar
手机用户2502861227
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有