vote up -1 vote down
star

Hello, can someone help me out a bit

I am getting confused with the comma's here

I need to have the right string format in a jquerystring in order to add this to a list like this

userslist.html($users).fadeIn(2000);

and this is what I have sofar, but it has errors

		$resultaat +='<li class="geel_klein"><a class="openchat" href="javascript:void(0)" onClick="return chatWith(''+ $bijnaam + ')'">' + $bijnaam + '</a><span class="date">&nbsp;' + $detijd + '</span></li>';

before I had this with php, but for some reason the ajaxcall diddn't put the javascriptfunction in the string when I returned this string

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

thanks, Richard

flag
What is PHP and what JavaScript? Or is both JavaScript? You shouldn’t use $ as well for JavaScript variables to avoid confusion. Those are just needed for PHP. – Gumbo 2 days ago
add comment

7 Answers

vote up 1 vote down

When you put string delimiters in a string, you have to escape them. If you for example put the text It's in a string and use apostropes as delimiters, the string would be: 'It\'s'

With the apostropes in the strings escaped, your code would be:

$resultaat += '<li class="geel_klein"><a class="openchat" href="javascript:void(0)" onClick="return chatWith(\'\'+ $bijnaam + \')\'">' + $bijnaam + '</a><span class="date"> ' + $detijd + '</span></li>';

I am a bit curious about your habit of putting a $ sign first in all your variable names. Have you someway come to believe that Javascript variables is written that way?

link|flag
+1 for the escaping issue, although I think what he needs is: $resultaat +='<li class="geel_klein"><a class="openchat" href="javascript:void(0)" onClick="return chatWith(\''+ $bijnaam + '\')">' + $bijnaam + '</a><span class="date"> ' + $detijd + '</span></li>'; – Dror 2 days ago
add comment
vote up 0 vote down

I'm not sure I understand the question. It seems to me like you have a PHP string and you need to pass it as a parameter to a Javascript function...?

If that's the case, you'd need to echo the Javascript function call via PHP as well. You can't reference PHP variables directly from Javascript.

So you can do:


    echo "usersList.html('{$users}').fadeIn(2000)";

Also, in your original $resultaat += expression, you need to use the dot (.) for string concatenation in PHP. The plus is for Javascript.

Unless, of course, the $resultaat += expression is actually Javascript, in which case your question has thoroughly confused me.

link|flag
add comment
vote up 0 vote down

SORRY, the ($resultaat) string is what I needed to be corrected It is supposed to be a javascriptstring

and below ($openchat) is a phpstring wich is correct, but I can't use that one. It's only an example off the string I need, but in a phpformat

what I need Is the javascript version off that one

I hope this will make it clear. I cannot edit my original question, otherwise I would have done that.

Richard

link|flag
Edit your initial question! – Gumbo 2 days ago
add comment
vote up 0 vote down

userslist.html($users).fadeIn(2000); can better be read like this

userslist.html($resultaat).fadeIn(2000);

link|flag
1 
Edit your initial question! – Gumbo 2 days ago
add comment
vote up 0 vote down

Try this:

function htmlEncode(str) {
    var trans = {
        "&": "&amp;",
        '"': "&quot;",
        "'": "&#039;",
        "<": "&lt;",
        ">": "&gt;"
    };
    return str.replace(/[&"'<>]/g, function($0) { return trans[$0]; });
}
$resultaat += '<li class="geel_klein"><a class="openchat" href="javascript:void(0)" onClick="return chatWith(\'' + htmlEncode($bijnaam) + ')">' + htmlEncode($bijnaam) + '</a><span class="date"> ' + htmlEncode($detijd) + '</span></li>';
link|flag
add comment
vote up 0 vote down

Thanks Guffa, Gumbo

I know you don't have to use the dollarsign before variables, but in the beginning I thought it was more clear for myself to distinguish from normal text. I am not consistent threw all my code yet. I also thougt it could do no harm either.

Gumbo, thanks for your input. I hope both your codes work.

As I said above, I cannot edit my questions, or put comments underneath for some reason. The comment section says, requires 50 reputation. I don't know. I diddn't have that before?

Richard

link|flag
1 
Edit your initial question when you want to add something to that question. And post a comment to a specific answer if you want to comment to that answer. Don’t use the answer function for that. – Gumbo 2 days ago
add comment
vote up 0 vote down

Guffa, Gumbo You both missed closing the string properly. The variable inside the js function needed an extra /' after the variable. At least,I learned apostrophes can be escaped in js as well as in php.

I can't edit or comment, Gumbo I logged in with my google account I consider mu question answerd.

Maybe, it will go better with the next question. Paolo Bergantino suggested, my browser suposedley lost the cookie that recognizes my account. That's why I am logged in with my google account from now on.

link|flag
add comment

Your Answer

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.