简体   繁体   中英

correct way to echo a link with a onclick javascript function

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

$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

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>";

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM