1

I have a button on my website that triggers an onclick event with the following function

onclick="
updatePage('<?php echo $page['title']; ?>','<?php echo $page['id']; ?>','<?php echo $page['attachedfiles']; ?>','<?php echo $page['attachmentprefix']; ?>');
"

this usually works fine but if the $page['title'] contains a single quote it breaks the rest for example if the string was "What's your name?" it would break after the "what '"

Is there a fix for this, i have tried using echo htmlspecialchars($page['title']) but it doesnt work. I am still a beginner so i am not too sure on how to solve this.

I am sorry if this is a duplicate question but from other solutions ive seen this seems like more of a javascript issue rather than php

1

1 Answer 1

2

You can try the addslashes function. Example:

<div class="thediv"></div>

<?php
    $title = "This isn't the title";
?>

<script>

    var div = document.querySelector('.thediv');

    div.innerHTML = '<?= addslashes($title) ?>';

</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.