Link to home
Start Free TrialLog in
Avatar of lherrou
lherrouFlag for Ukraine

asked on

Apostrophe in PHP/JS code issue

I'm using SocialEngine to set up a social networking site for a client. SocialEngine allows an admin setting so that the users use their full name (instead of a username). Javascript is used by SocialEngine to create a popup div to send private messages between users, and the problem I have run into is that when users have an apostrophe in their name (John O'Bryan, for example), it's seen as a single quote in the javascript, and prematurely ends the javascript string - so no popup box is generated, and no one can originate an email to that user.


Here's the code that generates the string:
"<a href=\"javascript:TB_show('"+l.Base.Language.Translate(784)+"', 'user_messages_new.php?to_user="+o.comment_authoruser_displayname+"&to_id="+o.comment_authoruser_username+"&TB_iframe=true&height=400&width=450', '', './images/trans.gif');\">"+l.Base.Language.Translate(834)+"</a>"
 
Here's that the results look like:
<a href="javascript:TB_show('Compose New Message', 'user_messages_new.php?to_user=FirstName LastName&amp;to_id=5&amp;TB_iframe=true&amp;height=400&amp;width=450', '', './images/trans.gif');">Send Message</a>
 
The problem is if the user's name is FirstName L'astName, then the javascript string is prematurely terminated because of the apostrophe in L'astName, and a popup div to send a PM isn't generated.

Open in new window

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

you have to escape the single quote inside the java string.


yourstring.replace(/'/, "\'")

Open in new window

applied to your code:
"<a href=\"javascript:TB_show('"+l.Base.Language.Translate(784).replace(/'/, "\'") +"', 'user_messages_new.php?to_user="+o.comment_authoruser_displayname.replace(/'/, "\'")+"&to_id="+o.comment_authoruser_username.replace(/'/, "\'")+"&TB_iframe=true&height=400&width=450', '', './images/trans.gif');\">"+l.Base.Language.Translate(834)+"</a>"

Open in new window

Avatar of lherrou

ASKER

A3,

Hmm, tried your fix, but the generated code remains the same. Note how the apostrophe (single quote) in the name terminates the variable for o.comment_authoruser_displayname before it gets to the second letter of the last name.

Ultimately the fix is probably to encode the apostrophe differently in the database field for the username, but that's a level of tinkering that I don't want to take on. I am hoping to find a quick fix within the JS itself.

<a href="javascript:TB_show('Compose New Message', 'user_messages_new.php?to_user=FirstName L'astName&amp;to_id=7&amp;TB_iframe=true&amp;height=400&amp;width=450', '', './images/trans.gif');">Send Message</a>

Open in new window

wait!!! we are inside a string, so we have to escape the generated \ also ...

"<a href=\"javascript:TB_show('"+l.Base.Language.Translate(784).replace(/'/, "\\\'") +"', 'user_messages_new.php?to_user="+o.comment_authoruser_displayname.replace(/'/, "\\\'")+"&to_id="+o.comment_authoruser_username.replace(/'/, "\\\'")+"&TB_iframe=true&height=400&width=450', '', './images/trans.gif');\">"+l.Base.Language.Translate(834)+"</a>"

Open in new window

Avatar of lherrou

ASKER

The code as generated remains the same... see the bold area?

<a href="javascript:TB_show('Compose New Message', 'user_messages_new.php?to_user=FirstName L'astName&amp;to_id=7&amp;TB_iframe=true&amp;height=400&amp;width=450', '', './images/trans.gif');">Send Message</a></a>
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lherrou

ASKER

Nope, still the same results.
well, I don't know then, why that \ is "eaten" up...
you will need to investigate where that could be.

ps: how's the treasure hunt going?
Avatar of lherrou

ASKER

I found the issue, there were TWO places where I needed to change the code, and your help had fixed it in one, but I was still looking at the output from the other.

Thanks!

<ot>Well, thanks :) Helped our secretary get started (her BF and his mom are both geocachers), she placed her first cache in the landscaping of our office. </ot>