Hi Monks, I'm having trouble with escaping an apostrophe. I'm using a javascript popup and a perl script to add something to the database. The problem is the $favtitle variable in my javascript link has an apostrophe in it, so I escape it. When it hits my script I read in the $favtitle with cgi param. However, I can't get my script to stuff the title in the database because of the apostrophe. I try switching it back and getting rid of the escape, but it doesn't work. Maybe I'm doing something wrong. The javascript that calls the popup is here. You'll notice I'm escaping the apostrophe for favtitle:
<a href="javascript:popUp('/cgi-bin/script.pl?favadd=yes&num=6210&fav +title=jones\'s title&favlink=6210.html')">
Here is the script:
$favtitle = param('favtitle'); if ($favadd && $username_valid == 1) { if ($favtitle && $favlink) { $favtitle =~ s/\\'/\'/g; $favorites = $favorites . "$favtitle|||$favlink|||$num~~~"; $oldtitle = $favtitle; $oldlink = $favlink; @favs = split /~~~/,$favorites; if (@favs > $maxfavs) { print <<HTML; Currently, the maximum number is $maxfavs. <br> <br>Max number reached! <br> <br> <font face="arial" size="-1"><a href="javascript:window.close()">Close + Window</a></font> HTML } else { @sorted = sort {uc($a) cmp uc($b)} @favs; $favorites = ""; foreach (@sorted){ ($favtitle,$favlink,$num) = split /\|\|\|/,$_; $favorites = $favorites . $favtitle . "|||" . $favlink + . "|||" . $num . "~~~"; } # update existing record $SQL = "UPDATE $regtable SET favorites='$favorites' WHERE +ID='$ID'"; &Do_SQL; # get favorites $favslist = ""; $rowcolor = "#EEEEEE"; @favs = split /~~~/,$favorites; foreach (@favs){ ($favtitle,$favlink,$num) = split /\|\|\|/,$_; $favslist = $favslist . "<tr><td bgcolor=$rowcolor><a +href=\"$favlink\">$favtitle</a></td></tr>"; # alternate grey and white bar if ($rowcolor eq "#EEEEEE") { $rowcolor = "#FFFFFF"; } else { $rowcolor = "#EEEEEE"; } # end if ($rowcolor eq "#EEEEEE") } $favslist = "<table border=0 width=100% cellpadding=4 cell +spacing=0><tr><td> <b>My Saves:</b><br></td></tr>".$favslist."</table +>"; print <<HTML; Saved! HTML
I get the confirmation pages like I'm supposed to, but nothing goes in the database. If I take out this line:
$favtitle =~ s/\\'/\'/g;
It makes no difference. If I don't escape the apostrophe in the javascript the popup doesn't work. The only way I can get it to work is to completely remove the apostrophe in the javascript link, but then the title goes in the database without an apostrophe which isn't ideal. The other weird thing is, if I don't use a javascript popup, and force people to change pages to the confirmatoin page, it works with the apostrophe. See anything I'm doing wrong? Thanks.

In reply to Escaping then un-escaping an apostrophe by htmanning

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.