Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 3rd, 2007, 02:19 PM
Registered User
 
Join Date: Jul 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Changing php variables using the href tag

Hey,

New to PHP, and i'm basically trying to set the value of a php variable when a specific link is clicked.

For example, if you click on the family link, the variable would be changed to family.

I'm using PHP5 if that makes any difference.

A true friend stabs you in the back
 
Old July 4th, 2007, 07:45 AM
Friend of Wrox
 
Join Date: Nov 2005
Posts: 223
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nope the php version doesn't make any difference in your case
you will need to do something like this

your hyperlink
Code:
<a href="mypage.php?phpvar=family">family</a>
and your php page for retrieving the variable
Code:
<?php
$myVar=$_GET['phpvar'];  //value will be family
?>
piece of cake huh ;)

__________________________________________________ ________
I am DJ Kat...that's my name. Its a D and a J and a Kat with a K.
 
Old July 6th, 2007, 01:06 PM
Registered User
 
Join Date: Jul 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks DJ,

I'll try this out over the weekend... watch this space as i'll be posting any problems...

Thanks again...

A true friend stabs you in the back
 
Old July 9th, 2007, 05:33 PM
Registered User
 
Join Date: Jul 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Problem with the above...

I actually want to change the value of a variable so that it changes the picture displayed on the page depending which link you click.

So its basically a photo album...

Code:
<?php

   $Picture = "Water.jpg";
   $ThisPicture = "";
   $Src = "images/";
   $Category = "Photoshop";
   $Flag = 1;
?>
Is the code at the top of the page to initialise the variables.

Code:
<a href="http://localhost:8080/Website.php?$Picture=Eye.jpg?>
Is what i use to change the value of $Picture

Code:
 $ThisPicture=$_GET['Picture'];
 echo "<img src='images/$ThisPicture' width='353' height='280' alt=''></td>"  ;
 echo $Picture;
 echo $ThisPicture;
Is what I use to change the picture... however i get an error stating 'Notice: Undefined index: Picture in'

Also, the value of $Picture does not change after clicking on the link... at first I thought this was due to me intialising it at the top of the page, but even when i removed this it still does not change??

I'm not redirecting to a new page or anything, i would like it to be the same page but for the picture only to change to save on loading times... is there a way of doing this or will i have to refresh the entire page?

A true friend stabs you in the back
 
Old July 9th, 2007, 07:04 PM
Friend of Wrox
 
Join Date: Nov 2005
Posts: 223
Thanks: 0
Thanked 0 Times in 0 Posts
Default

here is the mistake you made

<a href="http://localhost:8080/Website.php?$Picture=Eye.jpg?>

$ThisPicture=$_GET['Picture'];


and that causes the error
'Notice: Undefined index: Picture in'

because you are asking for the variable Picture and not $Picture
just remove the dollar in the url and it will work

Code:
<a href="http://localhost:8080/Website.php?Picture=Eye.jpg?>
Quote:
quote: i would like it to be the same page but for the picture only to change to save on loading times... is there a way of doing this or will i have to refresh the entire page?
You will need to refresh a whole page if you want to change the image on the page. That is if you use PHP only. PHP only generates code to show on your client(browser) once its in your browser php cant change the code for that page. Javascript is clientside and that you can use to refresh only parts of your HTML without refreshing the entire page

__________________________________________________ ________
I am DJ Kat...that's my name. Its a D and a J and a Kat with a K.
 
Old July 10th, 2007, 12:53 PM
Registered User
 
Join Date: Jul 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Once again DJ Kat you're right...

I'm so buying you a drink.

A true friend stabs you in the back
 
Old July 10th, 2007, 04:20 PM
Registered User
 
Join Date: Jul 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Actually, the error still appears (as i've turned error reporting on while i code the site) but now the images change when i click on the link.

However, due to the error no image appears when i first click onto the site. I have to click a link for an image to appear.

What i've done is this:

Code:
$ThisPicture=$_GET['Picture'];
                 if(!isset($_GET['Picture']))
                 {
                     echo "<img src='images/water.jpg' width='353' height='280' alt=''></td>"    ;
                 }
                 else
                 {
                        echo "<img src='images/$ThisPicture' width='353' height='280' alt=''></td>"  ;
                 }
This still shows the error, but at least it now shows an image when first accessing the site. I dont know if theres a better way of doing it or not.

Also DJ Kat (as i'm assuming you're the one who'll be replying), as I have to refresh the page like you said a few posts back, is there an easy way of doing that rather than writing the entire url into the href tag?

A true friend stabs you in the back
 
Old July 11th, 2007, 04:27 AM
Friend of Wrox
 
Join Date: Nov 2005
Posts: 223
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok some things aren't really clear to me
Quote:
quote:
but now the images change when i click on the link.
However, due to the error no image appears when i first click onto the site. I have to click a link for an image to appear.
What is suppose to be showed when the page loads for the first time?

Quote:
quote:
 Actually, the error still appears
what error do you get?

Quote:
quote:
is there an easy way of doing that rather than writing the entire url into the href tag?
easier in what way? You want to have an action without using an URL or do you wish to have an action without refreshing the page. It is possible without refreshing your page but then you will need javascript.

Is there a url with your code working or perhaps you have an example of something you want to make. There is a lot of stuff online maybe you can lookup a website that does something similar and give me that url so I can advice you how to build it. If you can't find anything similar try to explain all the functionality that your code is suppose to do and it would be a piece of cake for me to help you out

__________________________________________________ ________
I am DJ Kat...that's my name. Its a D and a J and a Kat with a K.
 
Old July 11th, 2007, 07:34 AM
Registered User
 
Join Date: Jul 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry DJ,

When the page first loads there should be a 'starter image' displayed in the center of the page. But this does not display due to the error 'Notice: Undefined index: Picture in'. Even after i changed what you told me to in the url

 
Quote:
quote:<a href="http://localhost:8080/Website.php?Picture=Eye.jpg?>
Quote:
But, i've changed the code to include the

Code:
$ThisPicture=$_GET['Picture'];
 if(!isset($_GET['Picture']))
   {
    echo "<img src='images/water.jpg' width='353' height='280' alt=''></td>"    ;
   }
  else
   {
    echo "<img src='images/$ThisPicture' width='353' height='280' alt=''></td>"  ;
   }
This displays the water.jpg image at startup, and when i click a link, this image is changed to the value of $ThisPicture, which is what i want and the error dissapears (as $_GET[] has an index after i click the link).

So this is solved, though i'm unhappy about finding a way to supress the error rather than fixing it altogether. The error ('Notice: Undefined index: Picture in') still appears in my site as i have error reporting on. I would like to find a way to fix it rather than hide it. But if there isn't a way then i'm happy too.

As for the easy way to refresh the page, I was just wondering if theres a shortcut to say in a href tag 'Link to this page', rather than writing out the url of the same page. I remember you told me earlier that using php i will need to refresh the page, but with javascript i can refresh just the html section i wish. I will look into using javascript, but i dont think i should be asking Javascript question on a PHP forum.

Thanks for all your help DJ... There will be a thanks to you on the finished site.

A true friend stabs you in the back
 
Old July 11th, 2007, 09:53 AM
Friend of Wrox
 
Join Date: Nov 2005
Posts: 223
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yeah I know that this is the PHP section but actually i think the javascript solution is more elegant since it doesn't force a user to reload the whole darn page. Sure some will say that you should keep in mind that some use old browsers that don't support javascript but these are such a few that I don't really care about people using old browsers.
you might wanna try out my javascript function its a lot easier than your php solution.

place the following code in your head
Code:
<script language="javascript" type="text/javascript">
//function that changes the image without reloading
function swapImage(imgUrl){
    document.getElementById('image_box').innerHTML='<img src="'+imgUrl+'" />';
}
</script>
place the next in your body
Code:
<div id="image_box">
    <img src="http://www.unimaas.nl/guesthouseum/trein.jpg" />
</div>
place these hyperlinks somewhere on your page
Code:
<a href="javascript:swapImage('http://www.trainweb.org/tgvpages/images/thalys/thalysbercy3.jpg');" >                
    french thalys
</a><br />
<a href="javascript:swapImage('http://i1.trekearth.com/photos/28148/ice_train_filtered.jpg');" >                
    german ICE train
</a><br />
<a href="javascript:swapImage('http://www.uscibooks.com/taylor/locomotive.jpg');" >                
    german ICE train
</a>
change the url arguement for the swapImage('') function to add a new link. And guess what no undefined index notice ;)

__________________________________________________ ________
I am DJ Kat...that's my name. Its a D and a J and a Kat with a K.





Similar Threads
Thread Thread Starter Forum Replies Last Post
changing the place of the tag rajesh_css XSLT 3 October 30th, 2008 08:35 PM
Want to pass value at href in anchor tag on xslt Abhinavnaresh XSLT 4 February 21st, 2008 04:32 AM
Changing variables in match templates chicken XSLT 0 September 11th, 2006 10:26 AM
changing the value of variables in PHP jflores1 PHP How-To 1 January 8th, 2006 12:09 AM
Placing <a href etc. in PHP scripts mercury7 BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 June 21st, 2003 07:23 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.