Search code examples
phpjavascriptmysqlapostrophehtmlspecialchars

Apostrophes issue while passing as parameter


i tried to fix an issue with apostrophes but did not succeed. Please have a look into this and suggest me the needful.

I am fetching a value from database which has apostrophes. I am passing the value using a variable into another javascript function. But because of the apostrophe, the function is not executing. Please have a look into below mentioned code.

PHP code:

<?php
$Id = $abc->id;
$str = $abc->street; // output: 111 O'CG street
?>

HTML:

<a href="javascript:void(0)" onclick="show_details(<?php echo $Id1;?>,'<?php echo $str;?>')"> 
Display
</a>

I tried to use the HTML code using backslash as well. But did not overcome the issue.

Code with backslash:

<a href="javascript:void(0)" onclick="show_details(<?php echo $Id1;?>,\"<?php echo $str;?>\")"> 
Display
</a> 

Please suggest me. Thanks in advance.


Solution

  • You have to use addslashes() function.

    Like:

    <?php echo addslashes($str);?>