Forum Moderators: coopster

Message Too Old, No Replies

include function

include("main.php?page=test.php");

         

opiston

2:59 am on Jul 28, 2005 (gmt 0)

10+ Year Member



Hi all again,

My main php has a top pannel. There are links on the top of the pannel, and when I click links on the top, it should only change the bottom part of the page, and the top pannel stays the same.

I use this code to do what I just described

<a href = main.php?page=test.php>test</a>

so test.php should be displayed on the bottom half of the page.

The problem:
I created a admin function which allows admin of the website to enter data into the database. I ran into redirection problem when I finished entering data and clicked submit.

The data went into the database just fine; however, I want to go to a specific page after clicking submit. So I use this code:

include('main.php?page=test.php');

But it seems like include treats the whole line main.php?page=test.php as a filename. So it gives me an error says "cannot file the file" (something like that)

Does anyone have experience on this kind of problem?

Thanks
Opiston

dreamcatcher

8:13 am on Jul 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The header function is probably what you need:

[uk.php.net...]

However, make sure there is no output sent to the browser before you call this function or else you will see errors.

dc

opiston

1:14 am on Jul 29, 2005 (gmt 0)

10+ Year Member



Hi dreamcatcher,
I tried using header before. As you said, I can't send output to the browser when I use header.

test.php is a file which checks if all the fields in the box are filled with user inputs. If it doesn't have user input, then it should return to main.php?page=test.php for the user to try again. And it should also print out an error message. So I have to send an echo to the browser.

This is why I can't use header.

Thanks for your help
Opiston

opiston

1:56 am on Jul 29, 2005 (gmt 0)

10+ Year Member



Hi all,
I have a question about header


<?php
include('../func.php');

if($procedures_title == "") {
not_fill("Title");
}
else if($procedures_text == "") {
not_fill("Text");
}
else {
update_procedures($procedures_title, $procedures_text);
}

header("Location: [$_SERVER[HTTP_HOST]...]
?>

When the program execute

header("Location: [$_SERVER[HTTP_HOST]...]
does it still execute the if statements?

because when I try to access a variable in not_fill(), it returns nothing to me. So I was wondering if the function is actually executed.

badone

1:57 am on Jul 29, 2005 (gmt 0)

10+ Year Member



From php.net/include

// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
// local filesystem.
include 'file.php?foo=1&bar=2';

// Works.
include 'http://www.example.com/file.php?foo=1&bar=2';

HTH,
BAD

badone

2:39 am on Jul 29, 2005 (gmt 0)

10+ Year Member



You may need to declare the variables as global.

You shouldn't need header as your original include will work as long as you specify the entire URL of the file you are trying to include.

Cheers,
BAD

bloke in a box

10:15 am on Jul 29, 2005 (gmt 0)

10+ Year Member



Not 100% sure if this is the most efficient way to do it but as mentioned above I declare global variables, set them and then include just 'index.php' and use Case to include the relevant file.

opiston

8:05 pm on Jul 29, 2005 (gmt 0)

10+ Year Member



Hi badone,
Thanks for your help
I used include("www.name.com/main.php?page=test.php") as you described, and it works fine.

A file called test_proc.php checks if the fields are filled in by the users. Here is the simple code of test_prc.php:


<?php
include('../func.php');

if($procedures_title == "") {
not_fill("Title");
}
include("www.name.com/main.php?page=test.php");
?>

not_fill is in func.php; there is another function called print_err() in function that I will be using in a bit.


<?php
$msg;
function not_fill($message) {
global $msg;
$msg = $message;
}

function print_err() {
global $msg;
print "testing";
print $msg;
}
?>

In test.php I have a code to print $msg with the function print_err():


<?php
include("../func.php");
print_err();
?>

But when test.php is called after test_proc.php, only "testing" is showing up on the screen and $msg has nothing in it.
I believe I am using global variable incorrectly. But I am not sure what's the best approach to this.

Regards
Opiston

opiston

8:10 pm on Jul 29, 2005 (gmt 0)

10+ Year Member



Hi bloke in a box,

Thanks for your help too, but I can't quite understand what you mean by

I declare global variables,
set them
and then include just 'index.php' and use
Case to include the relevant file.

Could you explain a little more about that sentence.
I just started php about 1 month ago, so I am not too familiar with it.

Regards
Opiston

badone

12:16 am on Aug 2, 2005 (gmt 0)

10+ Year Member



opiston: When you declare $msg declare it as global in test.php.

You can also do a print_r( $msg ) ; at various points to work out where it "disappears" and why.

opiston

1:15 am on Aug 3, 2005 (gmt 0)

10+ Year Member



Hi badone,
Thanks for your help.

If I make $msg a global variable, can I use it in another page? Or do I have to use session variables to carry over the value in a variable.

Regards
Opiston

badone

6:52 am on Aug 3, 2005 (gmt 0)

10+ Year Member



You need to pass it to the next page somehow.

You can use GET, POST, session (cookies), write it to a database and read it out again, write it to a flat file and read it out again, etc.

Whether it's global or not will not matter if you are trying to pass it to another page.

opiston

7:47 am on Aug 3, 2005 (gmt 0)

10+ Year Member



Hi badone,
Thanks for providing five suggestions: GET, POST, session, write it to a database, write it to a flat file. However, I have no clue on what GET and POST is, and writing to database and file seems to be inefficient. So I have decided to use session variables to trasfer values between files.

Thank you for your help
Opiston