0

I have input type text for user to edit their text

<input type='text' id='about'>
$('#about').val('<?PHP echo $about;?>');

the reason I didn't use value is because I have a reset button can reset the form.

<input type='text' id='about' value='<?PHP echo $about;?>'>

if I place inside of value, the reset button will not working.

My problem is if user enter the text with ' it will end the line from my script

ex.
$('#about').val('It's cool');

any way to solve this problem?

2
  • By escaping like $('#about').val('It\'s cool'); Commented Feb 20, 2014 at 13:06
  • $('#about').val('<?PHP echo addslashes($about);?>'); Commented Feb 20, 2014 at 13:08

4 Answers 4

3

addslashes() is what you want, see below:

$('#about').val('<?PHP echo addslashes($about);?>');
Sign up to request clarification or add additional context in comments.

Comments

1

This is what you can do and it works for sure

<?php 
$data = "aa ' aaa ' aaa";
?>
<script>
var aa = <?php echo json_encode($data); ?>;
alert(aa);
</script>

So in your case

$('#about').val('<?php echo json_encode($about);?>');

Comments

0

Use htmlspecialchars() to escape the quote.

Comments

0

you have to enclose your string in double quotes javascript as follow:

$('#about').val("<?PHP echo $about;?>");

1 Comment

And what if they want to use a double quote? ;)

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.