Jump to content

Variables on a PHP Include


danjacko

Recommended Posts

What i'm trying to do is have 20 HTML (href) links and a PHP include under the links. What I want is when a link is clicked the PHP include changes to a different file. Im sorry if its not very clear but i'm pretty new to PHP so don't know if its possible... Please can someone help :)

 

Link to comment
Share on other sites

What i'm trying to do is have 20 HTML (href) links and a PHP include under the links. What I want is when a link is clicked the PHP include changes to a different file. Im sorry if its not very clear but i'm pretty new to PHP so don't know if its possible... Please can someone help :)

Here's a quick example:

 

<a href="?file=file1">file 1</a>
<a href="?file=file2">file 2</a>
<a href="?file=file3">file 3</a>

<?php
if(isset($_GET['file') && file_exists("/path/to/file/".$_GET['file'].".php")) {
   include("/path/to/file/".$_GET['file'].".php");
} else {
   // do something else
}
?>

 

Link to comment
Share on other sites

I have tried

 

<a href="<?php file() {return 'file.php';} ?>">file 1</a>

 

<?php include file(); ?>

 

But this failed as it wont allow me to redefine the function when i tried a different link.

 

Then I tried.

 

<a href="<?php $file='file.php' ?>">file 1</a>

 

<?php include $file; ?>

 

Yet this didn't work because it only included the last link and when i tried to click another link it wouldn't change from the last one

Link to comment
Share on other sites

That will never work as it is invalid code. You'll want to use AbraCadaver example posted earlier:

What i'm trying to do is have 20 HTML (href) links and a PHP include under the links. What I want is when a link is clicked the PHP include changes to a different file. Im sorry if its not very clear but i'm pretty new to PHP so don't know if its possible... Please can someone help :)

Here's a quick example:

 

<a href="?file=file1">file 1</a>
<a href="?file=file2">file 2</a>
<a href="?file=file3">file 3</a>

<?php
if(isset($_GET['file') && file_exists("/path/to/file/".$_GET['file'].".php")) {
   include("/path/to/file/".$_GET['file'].".php");
} else {
   // do something else
}
?>

 

Link to comment
Share on other sites

Thanks you so much! Worked perfectly. For anyone who reads this topic again there is a ] missing and the code i now use is

<a href="?file=file1">file 1</a>

<a href="?file=file2">file 2</a>

<a href="?file=file3">file 3</a>

 

<?php

if(isset($_GET['file']) && file_exists("/path/to/file/".$_GET['file'].".php")) {

  include("/path/to/file/".$_GET['file'].".php");

} else {

  // do something else

}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.