PDA

View Full Version : Really simply image problem



MrRSMan
10-28-2009, 01:53 PM
What I need to do is display an image on the page, but part of the URL of that image comes from a PHP variable.

Example- I want to display this image:

http://website.com/images/$name/file.gif

This is the code I have so far:



<?php
$name = $_POST[name];
?>
<img scr = "http://website.com/images/$name/file.gif">


But it doesn't work. I know the problem is with the image insertion as the $name = $_POST[name]; works fine.

Thanks in advance for your help.

jscheuer1
10-28-2009, 03:19 PM
<?php
$name = $_POST[name];
?>
<img scr = "http://website.com/images/<?php echo $name; ?>/file.gif">

djr33
10-28-2009, 10:13 PM
There is also a problem with the syntax of $_POST[name]: you should be using $_POST["name"] or $_POST['name']. I'm actually surprised it works without that.


(In theory $_POST[name] would be proper syntax if name were defined as a constant, but that is unlikely here, especially because $_POST is an array of input data, not data you are creating yourself.)

jscheuer1
10-28-2009, 11:40 PM
There is also a problem with the syntax of $_POST[name]: you should be using $_POST["name"] or $_POST['name']. I'm actually surprised it works without that.

Good point. I'm still such a newb at PHP, but I at least thought I had this one. However, it has generally been my experience in PHP that quotes aren't always required in the same way that they would be in javascript. This may or may not be one of those cases.

djr33
10-28-2009, 11:46 PM
Correct-- this is parsed properly (apparently), but it is not proper code.

But, yes, you solved why it wasn't displaying the image.