(Newbie) How to change variable on Image click

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Foxy999
Forum Commoner
Posts: 45
Joined: Sat Mar 21, 2009 11:50 am

(Newbie) How to change variable on Image click

Post by Foxy999 »

I need to change a variable on the click of an image so that I can put it into the $_GET array. This is what I have:

echo "<p><a href='index.php'><img src='images/books.jpg' width='450' height='125', $_SERVER['PHP_SELF'], '$cat=1'/></a></p>";

Then when they click the image, it will change what database the looked up on the next page:

if($_GET['cat'] == 1)
{$query = "SELECT * FROM cat1";}
else if($_GET['cat'] == 2)
{$query = "SELECT * FROM cat2";}

I am new to PHP, please help.
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: (Newbie) How to change variable on Image click

Post by jazz090 »

well b4 getting into PHP, try fixing up ur html markup knowledge cos that html markup its not even valid

what u want is this: <a href="index.php?cat=1"><img scr="src.jpg" width="10" height="10"></a> (u will have to change the img attributes to ur own needs)
this will send the variable 1 into the $_GET (which is NOT an array unless told otherwise)

then the rest of ur code will come into play.
Foxy999
Forum Commoner
Posts: 45
Joined: Sat Mar 21, 2009 11:50 am

Re: (Newbie) How to change variable on Image click

Post by Foxy999 »

Its just code I had found online, I use dreamweaver most of the time.

Thanks for the help, you also had a syntax error in your html code.

Foxy
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: (Newbie) How to change variable on Image click

Post by pickle »

Moved to Javascript as PHP isn't client-side.

Please explain yourself a little more. I'm assuming you want to change the URL the user is taken to when they click on the link?

You can modify the href of a link in Javascript, using an onclick event call for the image.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Foxy999
Forum Commoner
Posts: 45
Joined: Sat Mar 21, 2009 11:50 am

Re: (Newbie) How to change variable on Image click

Post by Foxy999 »

pickle wrote:Moved to Javascript as PHP isn't client-side.

Please explain yourself a little more. I'm assuming you want to change the URL the user is taken to when they click on the link?

You can modify the href of a link in Javascript, using an onclick event call for the image.
I have it working, and it has nothing to do with Javascript.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: (Newbie) How to change variable on Image click

Post by pickle »

Glad to hear you have it working, but there's no way to change anything on the client-side, based on a user action.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply