Pass Get Variable (noob)

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

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

Pass Get Variable (noob)

Post by Foxy999 »

How can I pass the variable $_GET[x] when it equals xx ($_GET[sc] = xx) when I click on an image? I cannot just echo the html code I need because I am doing a rollover image in dreamweaver, and for some reason it won't work.


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

Re: Pass Get Variable (noob)

Post by John Cartwright »

I'm not exactly sure the issue you are having because of rollover images. All you simply need to do is check $_GET['sc'] == xx and append it to the url.

I.e.,

Code: Select all

 
$imagepath = "yourscript.php";
if (!empty($_GET['sc']) && $_GET['sc'] == 'xx') {
   $imagepath .= '?sc=xx';
}
 
echo $imagepath;
or

Code: Select all

yourscript.php<?php echo !empty($_GET['sc']) && $_GET['sc'] == 'xx' ? '?sc=xx' : ''; ?>
Foxy999
Forum Commoner
Posts: 45
Joined: Sat Mar 21, 2009 11:50 am

Re: Pass Get Variable (noob)

Post by Foxy999 »

I am not sure if I understand this, but I don't think it is the correct solution, I will try to explain better.

This is my image link:

Code: Select all

<a href="buy.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('x','','images/menubar/buy2.gif',1)"><img src="images/menubar/buy.gif" alt="" name="x" width="150" height="100" border="0" id="x"/>
But I need to add $_GET[sc] to the url, so I try this:

Code: Select all

<?php      
      echo "<a href='buy.php?sc=".$_GET[sc]."' onmouseout='MM_swapImgRestore()' onmouseover='MM_swapImage('buyRoll','','images/menubar/buy2.gif',1)'>";
      echo "<img src='images/menubar/buy.gif' name='buyRoll' width='150' height='100' border='0' id='buyRoll'/></a>";
?>
But my rollover image doesn't work (rolling over doesn't change image) but the link works correctly, it seems that dreamweaver does it in javascript.


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

Re: Pass Get Variable (noob)

Post by John Cartwright »

The example I've shown is in fact what you are after.

Can you post the generated HTML with the SC parameter present? Everything looks ok in the code you posted, however, I suspect you are getting notices within your script because you are perhaps using an initialized variable (which is why I add the empty() checks in my example).
Foxy999
Forum Commoner
Posts: 45
Joined: Sat Mar 21, 2009 11:50 am

Re: Pass Get Variable (noob)

Post by Foxy999 »

Sorry I am still quite confused, are you saying I might be running into problems because of an initialized variable (or function?) that isn't in the scope? And I don't think I understand your first script, I don't know too much about PHP. Actually what I am confused about is this part of the code:

Code: Select all

$imagepath = "yourscript.php";
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Pass Get Variable (noob)

Post by jackpf »

I think John Cartwright means that you should check that $_GET['whatever'] exists before using it...which is indeed true :D

And his code is a demonstration of how you can append the value of $_GET['sc'] to the query string of the script you're trying to run.
Foxy999
Forum Commoner
Posts: 45
Joined: Sat Mar 21, 2009 11:50 am

Re: Pass Get Variable (noob)

Post by Foxy999 »

Well it is not a problem appending $_GET[sc] to the url, because it works fine with the code I have. My problem is that my pop-up image is not working correctly when echo it with php (so I can append $_GET[sc] to the url.)

The pop-up image is done with dreamweaver, I don't know if there would be a problem with the scope, but the thescript functions are declared outside of my php code, and this is declared outside of the php code, it is the image that should pop-up:

Code: Select all

<body onload="MM_preloadImages('images/menubar/buy2.gif')">
If this is the problem can I initialize onload in the php, and still have the <body> tag where it is originally?


Foxy
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Pass Get Variable (noob)

Post by jackpf »

What do you mean by not working correctly?

This seems like a javascript problem, not php.

In which case I suggest you make use of the firefox error console (I find firefox's particularly good, but most browsers have one).
If this is the problem can I initialize onload in the php, and still have the <body> tag where it is originally?
I'm not really sure what you mean by that.
Foxy999
Forum Commoner
Posts: 45
Joined: Sat Mar 21, 2009 11:50 am

Re: Pass Get Variable (noob)

Post by Foxy999 »

I ran the error console in firefox and I got this:

Error: syntax error
Source File: http://.../index_beta.php
Line: 1, Column: 12
Source Code:
MM_swapImage(

It happens every time I go to roll over the image, so I guess it is a javascript problem. But the problem goes away when I don't put my html code in php (echo the link like before.)

Foxy
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Pass Get Variable (noob)

Post by jackpf »

Ooooooh I see - you've got nested quotes. You need to use either double quotes in your html, or javascript.

Here:

Code: Select all

onmouseover='MM_swapImage('buyRoll','','images/menubar/buy2.gif',1)'
Foxy999
Forum Commoner
Posts: 45
Joined: Sat Mar 21, 2009 11:50 am

Re: Pass Get Variable (noob)

Post by Foxy999 »

Where would I add the double quotes? Wouldn't it make my php not work?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Pass Get Variable (noob)

Post by jackpf »

Yes, so you need to escape them with a backslash. You need to replace one set of quotes with double quotes.
Foxy999
Forum Commoner
Posts: 45
Joined: Sat Mar 21, 2009 11:50 am

Re: Pass Get Variable (noob)

Post by Foxy999 »

Thanks that worked great!


Foxy
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Pass Get Variable (noob)

Post by jackpf »

Phew :P

Cool, no problem.
Post Reply