tags:

views:

220

answers:

4

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

+8  A: 

It sounds like you might want an HTML form:

<form method="post" action="other_file.php">
    <input name="foo" type="..."... /> ...
</form>

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

jtbandes
A: 

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.

 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.

acrosman
Uh, that's quite a condescending approach to teaching. Why not just explain why one would want to keep views and logic separate and give a quick example?
eyelidlessness
+2  A: 

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.

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.

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.

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    switch ($_POST['command']) {
    case 'show_file_1':
        include 'file_1.php';
        break;
    case 'show_file_2':
        include 'file_2.php';
        break;
    }   
    exit;
}   

?>
<form method="POST">
    <button name="command" value="show_file_1">Show file 1</button>
    <button name="command" value="show_file_2">Show file 2</button>
</form>

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

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:

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.

noob source
A: 

noob source got my question right. I tried both methods you posted, include and header but it keeps giving me: Parse error: syntax error, unexpected ';'

chustar
Post your code. I can see nothing in the code supplied in the answers above that should be causing that.
ceejayoz