Web Hosting Talk







View Full Version : Javascript question


Lord Northern
02-25-2008, 07:10 PM
Hey.
I have a php file with Javascript in it.
Now, this page has a photo within it which this page displays. The photo name is forwarded to it by the GET method.
Now, I want to know one thing: say this page is loaded with a certain photo (example.com/viewphoto.php?photoname=dudes)
How can I access the value of photoname through Javascript that is on that page?

Codebird
02-25-2008, 07:26 PM
<img src="path_to_photo/<?php echo $_GET['photoname'];?>" id='theimage'/>

javascript:

var imageSource=document.getElementById('theimage').src

Lord Northern
02-25-2008, 07:31 PM
oh, thanks

foobic
02-25-2008, 08:16 PM
<img src="path_to_photo/<?php echo $_GET['photoname'];?>" id='theimage'/>
You'd want to sanitize the input first rather than just including it like that, or some joker will exploit it. eg.
preg_match('/\w+/', $_GET['photoname'], $matches);
$photoname = $matches[0];

...

<img src="path_to_photo/<?php echo $photoname;?>" id='theimage'/>

Also, image.src will give you the full url of the image rather than just your photo name - depending on what you're trying to do you might find it easier to use a hidden form field
<input type="hidden" id="photoname" name="photoname" value="<?php echo $photoname; ?>" />
and
document.getElementById('photoname').value