views:

868

answers:

3

Hello,

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

+1  A: 

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>'
Gumbo
json_encode?? ok, interesting. I will try that if the first one diddn't work.
@Richard: `json_encode` is required to get a valid JavaScript string declaration.
Gumbo
@Gumbo: in both cases you need the ENT_QUOTES option to htmlspecialchars()
gahooa
I have php v5.6, non the less, it gave a blank pagemayby something with the quotes. I will use the one from @mupdyke, because it did the job, thank you.
@gahooa: No, ENT_QUOTES just replaces single quotes too. But the default value for `$quote_style` is ENT_COMPAT that will replace double quotes.
Gumbo
@Richard: You’re right, forgot to replace a single quote.
Gumbo
+5  A: 

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>";
mupdyke
this one did it, thank you
@Richard You might want to accept this answer then ;)
victor hugo
what, is there some button to click then?
@Richard: Click the check mark to the left of the answer, just below the vote counter.
Prestaul
A: 

can you please help me with the following code:

 echo '<div id="editprofile"> <a href="#" onClick="loadXMLDoc('editprofile.php')"> Edit profile </a> </div>';

i want to view the edit profile text in the div called editprofile.. and when they click on the text "edit profile" it appears in a div called change.

I have an javacode called loadXMLDoc which is ajax.

i get this error message when loading the page:

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /Applications/xampp/xamppfiles/htdocs/mmismatch/viewprofile.php on line 78
george