Page 1 of 2
Setting a variable when clicking link
Posted: Wed Jan 14, 2004 1:30 pm
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
Posted: Wed Jan 14, 2004 2:20 pm
by John Cartwright
I've been looking around and trying stuff like
Code: Select all
<?php
$_SERVERї'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
Posted: Wed Jan 14, 2004 2:22 pm
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.
Posted: Wed Jan 14, 2004 2:27 pm
by Straterra
You could do something like this
Code: Select all
<a href="page.php?link=1">1 link</a>
<a href="page.php?link=2">2 link</a>
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;
?>
Client Side
Posted: Wed Jan 14, 2004 2:30 pm
by ol4pr0
Isnt there a Client Side thingie with GTK-php ?
Posted: Wed Jan 14, 2004 2:32 pm
by Straterra
You shouldn't need it with what I wrote.
Posted: Wed Jan 14, 2004 2:34 pm
by John Cartwright
I'm gettting a parse error with this
Code: Select all
if ( isset($link == false ) {
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.
Posted: Wed Jan 14, 2004 2:35 pm
by Straterra
Posted: Wed Jan 14, 2004 2:39 pm
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
changed to
silly me ty a lot
..
Posted: Wed Jan 14, 2004 2:42 pm
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
Posted: Wed Jan 14, 2004 2:43 pm
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;
?>
Posted: Wed Jan 14, 2004 5:21 pm
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ї'link'];
if ( $link == "2" ) {
include("portfolio.php");
}
if ( $link == "3" ) {
include("faq.php");
}
if ( $link == "4" ) {
include("pricing.php");
}
if ( $link == "5" ) {
include("aboutme.php");
}
if ( $link == "6" ) {
include("mycomputer.php");
}
if ( $link == "7" ) {
include("tutorials.php");
}
if ( $link == "8" ) {
include("contact.php");
}
else
{
include("main.php");
}
?>
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 !!
Posted: Wed Jan 14, 2004 5:30 pm
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");
}
?>
Posted: Wed Jan 14, 2004 6:59 pm
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>
Posted: Wed Jan 14, 2004 8:04 pm
by John Cartwright
Good job thanks alot for your help guys.. ill be back soon :d