0

I have the following line of code:

<a href="javascript:;" onClick="tweeet('myid')">My Tweets!</a>

Now while this is working perfectly fine the following line is not:

<a href="javascript:;" onClick="tweeet(<?php echo 'myid'; ?>)">My Tweets!</a> 

Can anyone help me out why it is not working and suggest any changes? The variable I want to pass to the Javascript function is a PHP variable. I have tried the PHP with single quotes and double quotes but it is not working.

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
ayush
  • 14,350
  • 11
  • 53
  • 100

4 Answers4

6

You need quotes for both the php side and the Javascript side. You've only got php quotes there.

<a href="javascript:;" onClick="tweeet('<?php echo 'myid'; ?>')">My Tweets!</a>

looks weird but it should work, though I'm no php expert. Note that if there's any chance that "myid" (on the php side) might contain user-supplied data (like, something that came from an <input> field at some time), or if it's otherwise unpredictable, then it has to be put through something on the server side to make sure that the resulting tag is "clean".

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • You're dead on, his above code would render onClick="tweet(myid)"> and that would work if myid was supposed to be a variable. – Dan Heberden Jun 10 '10 at 20:04
  • @ayush Are you clear on *why* it worked? `` tells PHP to output the string `myid`, not `'myid'`; `` would tell it to output the variable myid instead, while `` would tell it to output `'myid'` – Michael Mrozek Jun 10 '10 at 20:07
  • This way works for this particular case and variable, but it lacks failsafe; for example if the php variable `$myid` contains a quote char, then it's will sadly break down. – azatoth Jun 10 '10 at 20:33
  • Yes, that's totally true. Again, I don't know php. I know exactly how I'd handle that in a JSP page! I'll amend the answer nevertheless. – Pointy Jun 10 '10 at 22:03
1

I usually do as following:

<script>
  var jsvar = <?=json_encode($php_var)?>;
</script>

After that I can use jsvar under the javascript codes. And for readability I usually place all those assignments in an own script tag.

What you gain by using <?=json_encode($php_var)?> is that you won't need to go on escaping, and it works for arrays and hashes as well as strings, numbers etc...

For example, following php code:

<?php
$php_string = "hello";
$php_array = array( 'a', 'b', 'c' );
$php_hash = array( 'a' => 1, 'b' => 16, 'c' => 42 );
$php_number = 123;
$php_bool = false;
$php_null = null;
?>
<script type="text/javascript">
var js_string = <?=json_encode($php_string)?>;
var js_array = <?=json_encode($php_array)?>;
var js_hash = <?=json_encode($php_hash)?>;
var js_number = <?=json_encode($php_number)?>;
var js_bool = <?=json_encode($php_bool)?>;
var js_null = <?=json_encode($php_null)?>;
</script>

produces the following result:

<script type="text/javascript">
var js_string = "hello";
var js_array = ["a","b","c"];
var js_hash = {"a":1,"b":16,"c":42};
var js_number = 123;
var js_bool = false;
var js_null = null;
</script>
azatoth
  • 2,379
  • 15
  • 18
1

You forgot to Quote your answer. You are echoing the string OK, but you need to ' ' the response so JavaScript will know it's a string. You can use ' or \"

Evan
  • 3,191
  • 4
  • 29
  • 25
0
<a href="javascript:;" onClick="tweeet('<?='myid'; ?>')">My Tweets!</a>

Uses short echo thing.

nebkat
  • 8,445
  • 9
  • 41
  • 60