lherrou
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&to_id=5&TB_iframe=true&height=400&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.
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>"
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_displ ayname 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.
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_displ
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&to_id=7&TB_iframe=true&height=400&width=450', '', './images/trans.gif');">Send Message</a>
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>"
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=First Name L'astName&to_id=7&TB_ iframe=tru e&heig ht=400& ;width=450 ', '', './images/trans.gif');">Se nd Message</a></a>
<a href="javascript:TB_show('
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
you will need to investigate where that could be.
ps: how's the treasure hunt going?
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>
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>
Open in new window