Page 1 of 1
Pass Get Variable (noob)
Posted: Sat Aug 01, 2009 8:13 pm
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
Re: Pass Get Variable (noob)
Posted: Sat Aug 01, 2009 8:44 pm
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' : ''; ?>
Re: Pass Get Variable (noob)
Posted: Sat Aug 01, 2009 9:05 pm
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
Re: Pass Get Variable (noob)
Posted: Sat Aug 01, 2009 11:53 pm
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).
Re: Pass Get Variable (noob)
Posted: Sun Aug 02, 2009 3:37 pm
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:
Re: Pass Get Variable (noob)
Posted: Sun Aug 02, 2009 4:32 pm
by jackpf
I think John Cartwright means that you should check that $_GET['whatever'] exists before using it...which is indeed true
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.
Re: Pass Get Variable (noob)
Posted: Sun Aug 02, 2009 4:45 pm
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
Re: Pass Get Variable (noob)
Posted: Mon Aug 03, 2009 7:12 am
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.
Re: Pass Get Variable (noob)
Posted: Mon Aug 03, 2009 1:47 pm
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
Re: Pass Get Variable (noob)
Posted: Mon Aug 03, 2009 1:52 pm
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)'
Re: Pass Get Variable (noob)
Posted: Mon Aug 03, 2009 2:00 pm
by Foxy999
Where would I add the double quotes? Wouldn't it make my php not work?
Re: Pass Get Variable (noob)
Posted: Mon Aug 03, 2009 2:01 pm
by jackpf
Yes, so you need to escape them with a backslash. You need to replace one set of quotes with double quotes.
Re: Pass Get Variable (noob)
Posted: Mon Aug 03, 2009 2:07 pm
by Foxy999
Thanks that worked great!
Foxy
Re: Pass Get Variable (noob)
Posted: Mon Aug 03, 2009 2:10 pm
by jackpf
Phew
Cool, no problem.