用onclick javascript函数回显链接的正确方法

时间:2022-11-24 14:50:19

my question is how I can echo this the right way because the variable in the onclick function gives out a undefined error

我的问题是如何以正确的方式回应这个,因为onclick函数中的变量给出了一个未定义的错误

$openchat="<a href='javascript:void(0)' onClick='return chatWith(" . $livenaam .")'>" . $livenaam . "</a><br>";

echo $openchat;

I want to use it in a loop to get a list off users online for the chat

我想在循环中使用它来获取用户在线聊天列表

Thanks, Richard

2 个解决方案

#1


Looks like you are missing some quotes:

看起来你错过了一些引用:

$openchat="<a href='javascript:void(0)' onClick='return chatWith(\"" . $livenaam ."\")'>" . $livenaam . "</a><br>";

or for increased security:

或者为了提高安全性:

$openchat="<a href='javascript:void(0)' onClick='return chatWith(\"" . htmlspecialchars($livenaam,ENT_QUOTES) ."\")'>" . htmlspecialchars($livenaam,ENT_QUOTES) . "</a><br>";

#2


Try this:

'<a href="javascript:void(0)" onclick="return chatWith(' . htmlspecialchars(json_encode($livenaam)) . ')">' . htmlspecialchars($livenaam) . '</a><br>'

If json_encode is not available, try this:

如果json_encode不可用,请尝试以下方法:

'<a href="javascript:void(0)" onclick="return chatWith(' . htmlspecialchars('"'.addslashes($livenaam).'"') . ')">' . htmlspecialchars($livenaam) . '</a><br>'

#1


Looks like you are missing some quotes:

看起来你错过了一些引用:

$openchat="<a href='javascript:void(0)' onClick='return chatWith(\"" . $livenaam ."\")'>" . $livenaam . "</a><br>";

or for increased security:

或者为了提高安全性:

$openchat="<a href='javascript:void(0)' onClick='return chatWith(\"" . htmlspecialchars($livenaam,ENT_QUOTES) ."\")'>" . htmlspecialchars($livenaam,ENT_QUOTES) . "</a><br>";

#2


Try this:

'<a href="javascript:void(0)" onclick="return chatWith(' . htmlspecialchars(json_encode($livenaam)) . ')">' . htmlspecialchars($livenaam) . '</a><br>'

If json_encode is not available, try this:

如果json_encode不可用,请尝试以下方法:

'<a href="javascript:void(0)" onclick="return chatWith(' . htmlspecialchars('"'.addslashes($livenaam).'"') . ')">' . htmlspecialchars($livenaam) . '</a><br>'