我試圖將一個PHP字符串傳遞給一個javascript函數。 PHP部分看起來像這樣:將一個朋友的名字的PHP字符串傳遞給javascript函數
<?php
foreach ($friends as $key => $friend) {
// Extract the pieces of info we need from the requests above
$id = assertNumeric(idx($friend, 'id'));
$name = idx($friend, 'name');
// Here we link each friend we display to their profile
echo('
<li>
<a href="#" onclick="window.open(\'http://www.facebook.com/' . $id . '\')">
<img src="https://graph.facebook.com/' . $id . '/picture?type=square" alt="' . $name . '">'. $name . '
</a>
<form>
<input type="button" value="Meet" onclick="postMeet(. $name.)"/>
</form>
</li>');
}
?>
javascript函數看起來像這樣。如果我將$ key或$ id傳遞給函數,但不傳遞$ name,它會很好地工作。任何人有任何想法?我很新的PHP和JavaScript。
<script type="text/javascript">
function postMeet(key)
{
var met = 'Met ';
var body = met + ' '+ key;
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error) {
alert(response[0]);
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
}
</script>
我希望你會發一條消息 - onclick =「postMeet(\'」。htmlspecialchars($ name)。「\')」 – devdRew 2012-01-03 12:07:34
@devdRew:不,我沒有因爲他使用''''而不是'''引用傳遞給'echo'的字符串,但是我錯過了一對'''字符,所以謝謝。 – 2012-01-03 12:11:59