0

I'm using the following code in php:

PHP:

 $suggested_sentence[0] = "Hello, how are you?";
  echo $suggested_sentence[0] . ' <input type="image" src="button.png" 
  onclick = update_textarea('. $textareacount.','. "'".$suggested_sentence[0]."')/>";

Javascript function:

function update_textarea(count, new_sentence) {
    document.getElementById('sentence' + count).value = new_sentence;
}

But when i press the button i get the error "unterminated string literal but if i change the value of $suggested_sentence[0] = "Hello" it works fine.

What should i do then?

0

2 Answers 2

1

Check the file after PHP has executed to ensure that your button looks like this...

<input type="image" src="button.png" onclick="update_textarea(1, 'Hello, how are you?');"/>

Note that the onclick event is surrounded with double-quotes.

You can adjust your example like this:

 $suggested_sentence[0] = "Hello, how are you?";
  echo $suggested_sentence[0] . ' <input type="image" src="button.png" 
  onclick="update_textarea('. $textareacount.', '. "'" . $suggested_sentence[0]."'" . ')"/>';
Sign up to request clarification or add additional context in comments.

1 Comment

@fawad, glad I could help. Feel free to click on that tick next to the answer if it solved your problem. Cheers :)
0

HTML encode $suggested_sentence[0] as it may contain characters that break the javascript literal (such as ' so make sure you set quote_style to ENT_QUOTES for this).

1 Comment

This is a good suggestion, although not the culprit in this case.

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.