Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

Possible Duplicate:
Pass PHP variable to Javascript code

My question is how to pass php variables in a javascript method call which is in a php echo command. I am doing so, to make the image size dynamic.

My code below:

$imagedata = getimagesize("artwork/x.jpg");                                 
echo '<td align="center"><img thumb="' . $big . $newFile . '" src="' . $images . $file . '" onclick="showImg(this, "'$imagedata[0]'", "'$imagedata[1])'");"></td>';

I understand why this results into an error and know how to pass php variables in a javascript method call but the code needs to be in a php echo which makes it a bit more complicated.

share|improve this question

marked as duplicate by rlemon, Christian, PeeHaa, GordonM, Octavian Damiean Aug 23 '12 at 12:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
What is the problem? – hjpotter92 Aug 22 '12 at 22:53
    
Which is the error you stumble upon actually? – moonwave99 Aug 22 '12 at 22:53
    
Error comes from here: " onclick="showImg(this, "'$imagedata[0]'", "'$imagedata[1])'");" – Athos Aug 22 '12 at 22:57
    
Exact duplicate of the right side. – PeeHaa Aug 23 '12 at 12:54

3 Answers 3

up vote 1 down vote accepted

You can try this

<?php
$imagedata = getimagesize("artwork/x.jpg");                                 
?>
<td align="center"><img thumb="<?php echo $thumb_name?>' src = "<?php echo $path?>" onclick="showImg(this,<?php echo $imagedata[0]?>,<?php echo $imagedata[1]?>)"></td>

Its not a good practice to echo or print html tags within php

share|improve this answer
    
That minus a small single quote error works. Thanks! (for the code example + advice) – Athos Aug 23 '12 at 3:17
    
A little cumbersome, with all those echos one after the other. IMHO. – Tom Aug 23 '12 at 7:49

For a more maintainable and readable code, use printf():

printf('
    <td align="center"><img thumb="%s" src="%s" onclick="showImg(this, %s, %s)"></td>',
    $big . $newFile,
    $images . $file,
    $imagedata[0],
    $imagedata[1]
);

Granted that having intrusive JS [onClick], html and php logic in the same file is so 2000ish and you [and everybody else] should avoid it.

share|improve this answer
    
+1 for printf although i would avoid the incline click handler and use jQuery or vanilla js to scan the DOM and then assign event handlers then pull the needed vars out of data attributes (data-thumb, data-height, data-width). Much cleaner that way. – prodigitalson Aug 22 '12 at 23:09
1  
I would avoid printf itself and use templates, but this goes over the question's scope I think ^^ – moonwave99 Aug 22 '12 at 23:13
    
Well youd still have to output a string liek that in the template... a single image tag shouldnt be an include it should be a text function whether you use printf or make a custom function... – prodigitalson Aug 22 '12 at 23:24
    
Thanks for the printf. It does work and yes, i should be avoiding this type of coding. – Athos Aug 22 '12 at 23:24

When implicitly inserting variables which are members of arrays into a double-quoted string you need to use the brace syntax:

"A string with an array field - {$arr[0]} - in it"

Edit:

Of course, you can also stick to the dot concatenation:

'onclick="showImg(this, "'.$imagedata[0].'", "'.$imagedata[1].'");"

(Perhaps that's what you meant, but you omitted the dots and had an extra paren?)

share|improve this answer
    
the problem is that i am inserting php values in a javascript method call which is not allowed. Furthermore, the javascrip call is within a php echo. I need a way to be able to pass php variables in javascript, within a php echo. – Athos Aug 22 '12 at 23:00
    
@Athos: I don't understand what is supposedly not allowed. Also, there's no problem with the echo. Have you tried the replacement I offered in my edit? – Tom Aug 22 '12 at 23:12
    
I tried that and it didn't work. That is because i can't put PHP code inside js without closing and reopening php within js. baig772 mentioned how it can be fixed that way. Unless i'm missing your point. Thanks regardless. Your edit helped. – Athos Aug 23 '12 at 3:30
    
You're echoing the whole line anyway, so all you have to do is put in the variables correctly. Your original code was almost correct - you just missed a couple of dots and had an extra paren; I corrected that in my edit as you saw. As for "putting PHP code inside JS" - if your PHP is closed you have to reopen it and use echo indeed. But in your code PHP is not closed, and you are using echo anyway - so you are within PHP! – Tom Aug 23 '12 at 7:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.