Passing variables to a php file


#1

Hello again,

I am learning php and have a simple project in mind. I would like to create a site with two frames (left and main), and have in the left frame thumbnails of larger images which will be loaded into the main frame when clicked… problem is, I cannot figure out how to pass a variable from the thumbnails in the left frame to the php page which will rebuild the main frame…

I’ve tried a link technique using just text instead of an image (I’m not even sure I this is right.): <a href="imagebuilder.php?imagetoshow=image01">link to imagenuilder.php</a>

It failed…

I have a book on php but it only teaches to pass values from within forms with submit buttons. So perhaps I can only use forms to pass variables? I hope this isn’t so. :wink:


#2

Hi grafenberg,

No, forms aren`t the only way of passing information to another page. You are on the right track with your query string.

Lets say your images are named:

image01.jpg
image02.jpg
image03.jpg

You can just add the name of the image on to the query string, like you already have, but with the .jpg extension.

link to imagenuilder.php

On your imagebuilder.php page, you need to reference the variable “imagetoshow” like this:

$imagetoshow = $_GET[‘imagetoshow’];

To show the full size image, just add this variable to your img tag:

Hope this helps.


#3

carella!

thanks for the quick reply. your suggestions were helpful. I’ve decided to drop the idea of having two pages and instead try a single page approach… I’m now having the page pass the variable to itself, and it works, my images are shown, however I’m getting a ‘notice’ at the top of the page stating: “Notice: Undefined index: imagetoshow in c:inetpubwwwrootimagebuilder.php on line 3”.

my code is as follows… :slight_smile:)

gracias! :)


#4

Yes, it is PHP being a little sensitive and you can easily remove the error by switching off error reporting.

error_reporting(0);

However, this is not necessarily good coding practice. Try using the isset function to test the state of the variable before you use it.

Change:

$imagetoshow = $_GET['imagetoshow'];

to:

if (isset($_GET['imagetoshow']))

{

$imagetoshow = $_GET[‘imagetoshow’];

}

Hope that helps. :)


#5

splendid! :stuck_out_tongue:


#6

You`re very welcome. :)