Setting a variable when clicking link

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

User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Setting a variable when clicking link

Post by John Cartwright »

I have a stupid question but here it is

When you click on a link, it sets a variable and then then page is reloaded. Depending on what variable it loads it will have different things show up on the site. For example

Link 1 is clicked and it loads the contents of link 1 inside the specified table. Anyone got a solution or tutorial? Thanks a bunch!

Phenom
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I've been looking around and trying stuff like

Code: Select all

<?php 

 $_SERVER&#1111;'PHP_SELF'] = "$page";

echo "images/$page";  

?>
something along that line but what I am really looking for is to have 1 file global.php with all the variables setup with all the content and the index.php just puts on the correct information on the site depending on the variables. If you need furthur explanation jus ask :d
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

I think what you are looking for is in perhaps Javascript. Responding to clicks is a client side thing, and PHP is a server side language.
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

You could do something like this

Code: Select all

&lt;a href="page.php?link=1"&gt;1 link&lt;/a&gt;
&lt;a href="page.php?link=2"&gt;2 link&lt;/a&gt;
Then, in page.php have this

Code: Select all

<?php
$link = $_REQUEST['link']
if ( isset($link == false ) {
die("You didn't follow a link here");
}
if ( $link == "1" ) {
echo "You clicked on link 1";
} elseif ( $link == "2" ) {
echo "You clicked on link 2";
}
?>
Or page.php could look like this

Code: Select all

<?php
$link = $_REQUEST['link']
if ( isset($link == false ) {
die("You didn't follow a link here");
}
echo "You clicked on link ".$link;
?>
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Client Side

Post by ol4pr0 »

Isnt there a Client Side thingie with GTK-php ?
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

You shouldn't need it with what I wrote.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I'm gettting a parse error with this

Code: Select all

if ( isset($link == false ) &#123;

The reason I'm using php is because I'm going to have a lot of other things on my site that requires php so I would rather keep it 100% php based.
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

try this

Code: Select all

if ( isset($link == false ) ) {
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

no luck, still getting parse erorr on line 7.. which is that line


*update*

nvm the parse error was for the line before it

which was

Code: Select all

$link = $_REQUEST&#1111;'link']
changed to

Code: Select all

$link = $_REQUEST&#1111;'link'];
silly me ty a lot
Last edited by John Cartwright on Wed Jan 14, 2004 2:44 pm, edited 1 time in total.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

..

Post by ol4pr0 »

$shopping_cart = array('bagel' => 2,
'sandwich => 1,
'Plain Bagel => 4,);

print '<a href="next.php?cart='urlencode(serialize($shopping_cart)).'"Next</a>';



Do you mean something like that or do i got it wrong
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

Use this code as your page.php

Code: Select all

<?php 
if ( isset($_REQUEST['link']) == false ) { 
die("You didn't follow a link here"); 
}
$link = $_REQUEST['link']  
if ( $link == "1" ) { 
echo "You clicked on link 1"; 
} elseif ( $link == "2" ) { 
echo "You clicked on link 2"; 
} 
?>
Or

Code: Select all

<?php  
if ( isset($_REQUEST['link']) == false ) { 
die("You didn't follow a link here"); 
}
$link = $_REQUEST['link'] 
echo "You clicked on link ".$link; 
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I had problems with the code you gave me so I removed a little bit of it and it seemed to be working fine but here is what I am with right now.

Code: Select all

<?php 
		$link = $_REQUEST&#1111;'link'];
		if ( $link == "2" ) &#123; 
		include("portfolio.php"); 
		&#125; 
		if ( $link == "3" ) &#123; 
		include("faq.php"); 
		&#125; 
		if ( $link == "4" ) &#123; 
		include("pricing.php"); 
		&#125;
		if ( $link == "5" ) &#123; 
		include("aboutme.php"); 
		&#125;
		if ( $link == "6" ) &#123; 
		include("mycomputer.php"); 
		&#125;
		if ( $link == "7" ) &#123; 
		include("tutorials.php"); 
		&#125; 
		if ( $link == "8" ) &#123; 
		include("contact.php"); 
		&#125;
		else 
	    &#123; 
		include("main.php"); 
		&#125; 
?>
What the problem is it loads the main page right away....and then when I click on the links it brings me to the correct page but with the main page under it. I tried changing the includes to include_once() but didn't have any luck. Any Ideas??? TY !!
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

This is because of your If statements, try this code

Code: Select all

<?php 
      $link = $_REQUEST['link']; 
      if ( $link == "2" ) { 
      include("portfolio.php"); 
      } elseif ( $link == "3" ) { 
      include("faq.php"); 
      } elseif ( $link == "4" ) { 
      include("pricing.php"); 
      } elseif ( $link == "5" ) { 
      include("aboutme.php"); 
      } elseif ( $link == "6" ) { 
      include("mycomputer.php"); 
      } elseif ( $link == "7" ) { 
      include("tutorials.php"); 
      } elseif ( $link == "8" ) { 
      include("contact.php"); 
      } else { 
      include("main.php"); 
      } 
?>
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

Also, you might make your life a little easier (and the user's, actually) by rewriting it like so:

Code: Select all

$link = $_REQUEST['link']; 
      if($link)
      {
            include($link . ".php");
      } else
      {
            include("main.php");
      }
Example link:
<a href="sections.php?link=portfolio">Portfolio</a>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Good job thanks alot for your help guys.. ill be back soon :d
Post Reply