Page 1 of 1

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

Posted: Wed Apr 01, 2015 4:07 am
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 !

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

Posted: Wed Apr 01, 2015 5:14 am
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

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

Posted: Wed Apr 01, 2015 6:46 am
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.

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

Posted: Wed Apr 01, 2015 7:17 am
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/

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

Posted: Wed Apr 01, 2015 8:22 am
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 !

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

Posted: Wed Apr 01, 2015 9:24 am
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.

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

Posted: Wed Apr 01, 2015 10:49 am
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.

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

Posted: Wed Apr 01, 2015 10:55 am
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.

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

Posted: Wed Apr 01, 2015 12:03 pm
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 !

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

Posted: Wed Apr 01, 2015 2:18 pm
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>

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

Posted: Thu Apr 02, 2015 11:07 am
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.