views:

97

answers:

3

I have a variable inside my php file, which I want to pass to a javascript function whenever the user clicks a link on my php page.

here is the php in brief:

$delete_img=$image_id.'_'.$newfilename;
$display_image.="<td><img src='../temp_images/$newfilename'>";
if ($i==$extra_pic){ 
$display_image.="<br><a href='#' onclick='window.parent.reset_imageform($i, $delete_img);'>delete picture</a></td>";}

the "if($i==$extra_pic) is TRUE so dont mind that...

The problem is, if you click on the link 'delete picture' the function reset_imageform(nr, pic_nr) should get called, but it doesn't whenever I try to pass the variable $delete_img to the function. As soon as I remove this variable from the call like this 'reset_imageform($i);' then it works. but adding that variable, will make it not to work. The function doesnt get called at all!

The variable contains a random id nr, with an underscore, and then a filename, for example like this: 23432423439_picture.jpg

is there something Im missing here, should I do something to $delete_img before sending it in php?

All my documents are in utf-8 format!

Thanks

PS: just let me know if you need more input...

+5  A: 

As it's a string you'll need to quote it.

The proper way is like this:

$display_image.='<br><a href="#" onclick="window.parent.reset_imageform(' . $i . ', '
    . htmlspecialchars(json_encode($delete_img))
    . ');">delete picture</a></td>';

I've swapped the quotes around to make it work for any string; otherwise if you used it somewhere else and the string contained an apostrophe it wouldn't work. The alternative do swapping the quotes would be to pass ENT_QUOTES to htmlspecialchars().

You says that $i on it's own works, so I'm assuming $i is an integer - therefore it doesn't need quoting.

Greg
+1 correct escaping. The $i variable will need it too if it is not a simple integer. Although it doesn't hurt to `htmlspecialchars-json_encode` an integer either; it's usually best to use the same escaping on any value just in case.
bobince
this wont work for me... why?, yes $i is an integer...
Camran
I missed the `$` off `$display_image` - is that why?
Greg
sorry, no that wasnt why... anyways, answer below works... is there really a reason to use json_encode if the documents are all in utf-8?
Camran
It's just the right thing to do.
Greg
If you don't use json_encode, any characters that are special to JavaScript string literals, in particular quotes and backslashes, will go right through, making your script error out in the best case and giving you a cross-site-scripting security hole in the worst. Whilst you may consider `$delete_img` to be safe if you have generated it yourself without user input, it's not a good idea to get into the habit of skipping escaping steps.
bobince
+4  A: 

If you're passing a string to a JavaScript function, it must be properly quoted:

So without php, it looks like:

<br><a href='#'
  onclick='window.parent.reset_imageform(5, "23432423439_picture.jpg");'>
    delete picture</a></td>

So to get that resulting HTML, you need to include quotes:

$display_image.="<br><a href='#'
  onclick='window.parent.reset_imageform({$i}, \"{$delete_img}\");'>
    delete picture</a></td>";

We have multiple levels of quotes here, so the double quotes around 23432423439_picture.jpg need to be escaped, like \"

artlung
This works! thanks alot
Camran
Glad to help out
artlung
+3  A: 

When having problems with Javascript, do not look at the server side code which generates it, but look at the final output (rightclick page in webbrowser and choose 'view source' or so) and concentrate on the generated JS code only. This way you should have spotted the missing quotes around the JS string variable much sooner.

BalusC
+1, sound advice
Greg