What you should be doing is generating a JavaScript string , so you need to escape for JavaScript (json_encode()
) and remove the call to addslashes
which is for escaping PHP.
<a onclick='share(<?= json_encode($aux) ?>)'>Send</a>
Note that if you have any HTML entities in your PHP string, such as <
they will be decoded by the HTML parser. That was the problem with HTML encoding your quotes, they were being decoded to quotes within a JavaScript quote.
Ideally, you'd separate your concerns to avoid combining 3 languages.
The following example makes the data from PHP available in JavaScript (escape for JavaScript)
<a id='share-link'>Send</a>
<script>
document.getElementById('share-link').addEventListener('click', function() {
var shareContent = <?= json_encode($aux) ?>;
share(shareContent);
});
</script>
Or you could embed the data from PHP into a data attribute (escape for HTML)
<a id="share-link" data-share-content="<?= htmlentities($aux) ?>">Send</a>
<script>
document.getElementById('share-link').addEventListener('click', function() {
share( this.getAttribute("data-share-content") );
});
</script>
You could even go back to your inline script (not recommended)
<a id="share-link"
onclick="share(this.getAttribute('data-share-content'))"
data-share-content="<?= htmlentities($aux) ?>"
>Send</a>