Forum Moderators: coopster

Message Too Old, No Replies

PHP Include

Call a document when it is requested

         

max4

7:15 pm on Apr 26, 2009 (gmt 0)

10+ Year Member



Hello,

I am new to PHP and am having some difficulty calling several PHP documents into the same page. My code looks like this:

<?php
include 'nma.php';
include 'reg.php';
include 'fetch.php';
?>
..HTML Code..
Content area:
<?php
if (!isset($_GET['register'])) nma();
else reg();
?>
..Rest of HTML..

The registration form is located in a separate PHP document called reg.php. When a user clicks on the link, "page.php?register=1" the registration form shows up. Otherwise they are shown nma.php which is the default page. What I want to do is include a third document, fetch.php; and this is where the problem begins. Here is my code:

<?php
include 'nma.php';
include 'reg.php';
include 'fetch.php';
?>
..HTML Code..
Content area:
<?php
if (!isset($_GET['register'])) nma();
elseif(isset($_GET['fetch'])) fetch();
else reg();
?>
..Rest of HTML..

What I would like to happen is when a user goes into the page nma.php shows up as the default page. When a user clicks page.php?register=1 reg.php shows up in the content area, and when a user clicks on page.php?fetch=1 fetch.php shows up. Currently, when a user clicks on page.php?fetch=1 nma.php shows up. Any suggestions?

Thank you,
Max

midtempo

7:23 pm on Apr 26, 2009 (gmt 0)

10+ Year Member



<?php
if ($_GET['register']==1) {
nma();
} elseif($_GET['fetch']==1) {
fetch();
} else {
reg();
}
?>

mind you, i'd probably change the code to:

<?php
if ($_GET['register']==1) {
include 'nma.php';
} elseif($_GET['fetch']==1) {
include 'fetch.php';
} else {
include 'reg.php';
}
?>

and call the function in the include.

i'd also probably want to check my $_GET vars to make sure people weren't doing anything bad with the url...

max4

7:33 pm on Apr 26, 2009 (gmt 0)

10+ Year Member



Hi midtempo,

Thanks for the reply!

I'm receiving Undefined Index notices. No notices when reg.php is called, "Undefined index: register" when fetch is called and both register and fetch when nma is called.

Also, when I attempt to load the page with your second suggestion I receive the same notices without the documents being called.

midtempo

7:45 pm on Apr 26, 2009 (gmt 0)

10+ Year Member



not sure why - but you could try the following:

$register = 1*$_GET['register'];
if ($register == 1) {
etc.

[the 1* will make sure that $_GET['register'] is a number]

OR

you could make your links all use the same variable...

page.php?page=register
page.php?page=fetch


$page = addslashes(strip_tags($_GET['page']));
switch ($page) {
case 'register':
include 'register.php';
break;
case 'fetch':
include 'fetch.php';
break;
default:
include 'reg.php';
}

max4

8:04 pm on Apr 26, 2009 (gmt 0)

10+ Year Member



Thanks again midtempto.

I'm getting the same problem. With the first suggestion I'm receiving the Undefined Index notice in the same manner as before and with the second suggestion again, undefined index without any of the documents showing up.

I tried playing with it a big but couldn't come up with anything. After a few google searches I read a comment stating that the solution is to turn off notice reporting in php.ini. I'm not sure how that is a solution though !

max4

9:15 pm on Apr 26, 2009 (gmt 0)

10+ Year Member



I just solved the problem:

if (isset($_GET['register'])) {
reg();
} elseif(isset($_GET['fetch'])) {
fetch();
} else {
nma();
}

That does the trick.

Also, you said, "i'd also probably want to check my $_GET vars to make sure people weren't doing anything bad with the url..."

Is there a tutorial on checking $_GET vars somewhere on the net?

Thanks

midtempo

10:40 pm on Apr 26, 2009 (gmt 0)

max4

12:28 am on Apr 27, 2009 (gmt 0)

10+ Year Member



Thank you! That will be quite useful later on.