How to try and make a link act as an include.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ajoo
Forum Newbie
Posts: 6
Joined: Thu Mar 19, 2015 9:57 pm

How to try and make a link act as an include.

Post by Ajoo »

Hi all !

What I am trying to achieve is to make a link behave like an include. By this I mean that I would like to click on a link and that action, instead of opening a webpage should cause a page to be included in the same webpage at a given place i.e. within the div where the include should takes place.

I came across a snippet of code that also tried to achieve the same effect. This code is as follows:

Code: Select all

		switch($_GET['something']) 
		{ 
		  case 'page1': 
			include('page1.php'); 
			break; 
		  case 'page2': 
			include('page2.php'); 
			break; 
		  default: 
			include('error.php'); 
			break; 
Now while this seems like a great solution, I fail to understand how can I have the 'something' value sent to the php program from a link such as :

Code: Select all

<a href= "MyPage"> View a special member page</a>
If someone can tell me how that Get value of 'something' can be generated, then this should be good.

Right now it gives an undefined index 'something' error.

Thanks all !
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to try and make a link act as an include.

Post by requinix »

Look into AJAX. It will let you make a request to a URL grab the HTML it returns, then you use Javascript to insert that content into whatever part of the page you want.
But before you do that, just get a plain link version working. You can worry about the "instead of opening a webpage" later.

If you don't put a "something" in the URL then you will get that warning. The solution is to use isset to check if the "something" is present before blindly trying to use it.
An easy way to do that is

Code: Select all

switch(isset($_GET['something']) ? $_GET['something'] : 'default page here') {
Then all you have to do is provide the "something" in the URL. Or not, to get the default (ie, error) page. Like

Code: Select all

page.php?something=page1
Ajoo
Forum Newbie
Posts: 6
Joined: Thu Mar 19, 2015 9:57 pm

Re: How to try and make a link act as an include.

Post by Ajoo »

Hi Rexuinix,

Thanks for the reply. Using isset() definitely clears the message but the main problem still remains. How can I have the 'something' have the value = page1 or page2 upon clicking a link ? I mean how am i to define a link that would assign the values page1 or page2 to 'something'? That is what I am unable to get. I don't want to put the get variable into 'something' directly in the address bar. Clicking the link should assign that value to it.

If there is a way to achieve that then please show me.

Thanks again for the reply.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to try and make a link act as an include.

Post by Celauran »

Ajoo wrote:I don't want to put the get variable into 'something' directly in the address bar.
That's how GET requests work. Doing it asynchronously, nobody will see that anyway. http://api.jquery.com/jquery.ajax/
Ajoo
Forum Newbie
Posts: 6
Joined: Thu Mar 19, 2015 9:57 pm

Re: How to try and make a link act as an include.

Post by Ajoo »

Is there no way to do this using plain simple php ? I have never used ajax so I am unfamiliar with it. Thanks !
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to try and make a link act as an include.

Post by Celauran »

You could use POST instead of GET (since you don't want query parameters in the URL), wrap each link in a form, have the link be the submit button, and reload the page on click.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How to try and make a link act as an include.

Post by Christopher »

To do it in-page without making a new page request, you need to use Ajax. If you want to make new page requests, implement a Front Controller (which is essentially what your posted code shows).

If you want to do Ajax, then make it easy on yourself and use jQuery or some other Javascript library.
(#10850)
Ajoo
Forum Newbie
Posts: 6
Joined: Thu Mar 19, 2015 9:57 pm

Re: How to try and make a link act as an include.

Post by Ajoo »

Hi ! Thanks for the replies. I am aware of the method Celauran is suggesting. However I was wondering if there was indeed a method to generate the GET request from a link and use it in PHP. I guess I'll implement as suggested by Celauran. Thanks all for the suggestions and information. I am very grateful.
Ajoo
Forum Newbie
Posts: 6
Joined: Thu Mar 19, 2015 9:57 pm

Re: How to try and make a link act as an include.

Post by Ajoo »

Hi all !

As suggested by Celauran, I tried as follows. However the output does not echo as desired.
In fact the form remains permanently displayed and it seems like clicking on the link is making no difference.

Please can someone look at the code and find what mistake am I making or tell me what is wrong here.

Code: Select all


----- testLinks.php -----
<?php
if(isset($_POST['page']))
{
	switch($_POST['page'])
	{
		case 'page1':
			echo "Active Page 1";
			break;
		case 'page2':
			echo "Active Page 2";
			break;
		default:
			break;
	}		
}

		echo "<form action='testLinks.php' method='post'>";
                echo "<input type='hidden' name ='page' value='page1'>";
		echo "<a href= 'testLinks.php'> PAGE 1</a> ";
                echo "</form>";

		echo "<br>";

		echo "<form action='testLinks.php' method='post'>";
		echo "<input type='hidden' name ='page' value='page2'>";
		echo "<a href= 'testLinks.php'> PAGE 2</a> ";
		echo "</form>";	
?>

Thanks all !
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to try and make a link act as an include.

Post by requinix »

The problem is that you don't seem to understand how webpages and HTML work.

If you use a form then do not use an <A> link. Use a button, like <input type=submit> or the cooler <button type=submit>. Links do not trigger form actions unless you use Javascript, but there's no need for Javascript since the functionality you need already exists with normal HTML.

Code: Select all

<form action="testLinks.php" method="post">
<input type="hidden" name="page" value="page1">
<button type="submit">PAGE 1</button>
</form>
Of course repeating the form over and over again for each button sounds silly, right? It should sound silly to you. You can pass a value with a <button> instead of a hidden input.

Code: Select all

<form action="testLinks.php" method="post">
<button type="submit" name="page" value="page1">PAGE 1</button>
<button type="submit" name="page" value="page2">PAGE 2</button>
</form>
Ajoo
Forum Newbie
Posts: 6
Joined: Thu Mar 19, 2015 9:57 pm

Re: How to try and make a link act as an include.

Post by Ajoo »

Hi Requinix,

Thanks for clearing that. I was kinda fixated on trying to do this with a hyperlink. Thanks for pointing that that cannot be achieved. This works just fine.

Thanks very much.
Post Reply