0

So I'm trying to define $picurl1 so that it uses the value in $pic1. So in the end I want it to be:

<img src="./pictures/{definition of pic1}.png">

Right now I use this php code:

$pic1 = '<script src="pic.js"></script>';
$picurl1 = '<img src="./pictures/' + $pic1 + '.png'">';

Sorry if I'm not being very clear. I don't really know how to explain it. I hope you understand.

In other words, please tell me what I should change $picurl1 to.

By the way the script comes up with a random picture name without the '.png'.

Thanks in advance.

Samuel Lindblom
  • 762
  • 1
  • 4
  • 20
Pvpoe
  • 1
  • 2
  • 2
    It's really not clear what you're trying to do. This (with the right concatenation operator) will result in the string `.png'">` which is very much not valid HTML. What is "definition of pic1"? – David Sep 29 '13 at 20:33
  • 2
    The concatenation operator is `.`, not `+` – Niet the Dark Absol Sep 29 '13 at 20:33
  • 1
    It doesn't matter if the script "comes up with random picture names without the '.png'". PHP doesn't execute JavaScript, so by the time you're assembling your HTML, no script is being run that will output a random picture name. – Kiruse Sep 29 '13 at 20:36
  • @David - actually, not even that. $picurl1 is likely to end up as `0` since PHP will coerce those strings to integers before it adds them. Using the concatenation operator `.` would make more sense, but then, as you say, the result is nonsensical. –  Sep 29 '13 at 20:36
  • @MikeW David did state on a little side not that he'd still have to use the right concatenation operator. – Kiruse Sep 29 '13 at 20:37
  • @Derija93 He obviously added that while I was adding my comment. It wasn't there just now. –  Sep 29 '13 at 20:38
  • @MikeW Well, I don't know how long it takes you to write your comment, but I saw that a couple of minutes ago already. Anyways... – Kiruse Sep 29 '13 at 20:39

3 Answers3

3

For starters, you're using the wrong operator to concatenate strings in PHP. I think you mean this:

$picurl1 = '<img src="./pictures/' . $pic1 . '.png'">';

More to the point, what is "definition of pic1"? Do you mean that the code in pic.js will randomly choose a file name, and you want its result to be the URL used in the img tag?

The problem you're encountering, then, is that PHP runs on the server while JavaScript runs on the client. So your PHP code can't use the result of pic.js because it won't have a result until the browser runs it, after the PHP code is done.

So you need to get that result client-side in JavaScript code.

How does pic.js create that result? That is, is there a function in pic.js? For now I'm going to assume there is, and I'm going to assume that function is called something like getFileName. (Just for the purpose of this example.)

After you included the JavaScript code, and after the img tag is in the document, you can call that function and set the src of the img tag to its results. To help us identify the img tag, let's give it an id:

<img src="default.gif" id="theImage" alt="This is a dynamic image" />

(I gave it a default value for the src since an empty value is invalid. I also have it an alt value for completeness.) To change its src value to the result of a function, you'd do something like the following:

document.getElementById('theImage').src = getFileName();

Remember, this is all client-side code. The only way you can use the "result" in PHP code is if the calculation is done in PHP, not in JavaScript.

David
  • 176,566
  • 33
  • 178
  • 245
1

You must consider that all the server side codes are executed before the client side codes (javascript, html, css , ...). so your code does not make any sense , you can not embed an undefined code inside another code that is executing sooner.

if your js code must return some thing, so remove php codes and simply use HTML instead

mamal
  • 69
  • 4
0

I tested this successfully:

 $picName = "greenButterfly7"; //note no spaces inbetween green and butterfly

 $picurl1 = "<img src='./pictures/" . $picName . ".png'>";

 echo $picurl1;

or in pure HTML form:

  <img src='pictures/greenButterfly7.png'>

or in embedded form (PHP inside HTML):

  <img src='pictures/<?php echo $picName; ?>.png'>
KarlosFontana
  • 188
  • 2
  • 7