简体   繁体   中英

error while passing a php variable's value having _ as second parameter onclick to a javascript function

This is my php code

foreach($resource as $res) {    
    $PDF = $res->res_link; 
    $pdfname = $res->$res_name; (which is Dimesional_analysis) 

    echo "<a class='pdflink' href='#' onClick=OpenPdf('$PDF','$pdfname');><div class='txt'><table class='txt-in'><tr><td>$icon</td><td> $temp </tr></table></div></a>";
}

and my onclick javascript code

           function OpenPdf(pdf,pdfname) {
            some functionality;
            }

However, on clicking the link it is showing "unterminated string literal". however, when the pdfname has no underscore, that is if I give like $pdfname = 'Dimension' it is working fine.Hope someone will help me solve this. Thanks.

Always look at the generated HTML:

<a class='pdflink' href='#' onClick=OpenPdf('something','Dimensional_analysis');>

That doesn't look valid to me :p It's certainly vulnerable to problems.

Try this: (newlines added for readability)

echo '<a class="pdflink" data-pdf="'.htmlspecialchars($PDF).'"
    data-pdfname="'.htmlspecialchars($pdfname).'"
    onClick="OpenPdf(this.getAttribute(\'data-pdf\'),
                                       this.getAttribute(\'data-pdfname\'));">';

尝试这个:

echo "<a class='pdflink' href='#' onClick=OpenPdf('".$PDF."','".$pdfname."');><div class='txt'><table class='txt-in'><tr><td>$icon</td><td> $temp </tr></table></div></a>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM