0

I want to change the content in javascript of a textbox while using PHP.

If I change it with plain Text as:

echo "<script> var test = 0; simplemde.value('test'); 
test+= 5; alert(test);</script>";

it works. But with a php-Variable it isn't working any longer:

echo "<script> var test = 0; simplemde.value('$markdown'); 
test+= 5; alert(test);</script>";

Not sure what I'm doing wrong. If I open the site and watching the source-text it stands in the correct form there but It isn't in the box.

Here is my full code:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="./css/markdown.css">
<script src="./js/markdown.js"></script>
</head>
<body>

<textarea id="myID">
</textarea>

<script>
var simplemde = new SimpleMDE({
            element: document.getElementById("myID"),
            spellChecker: false,
            });

</script>

<?php
require_once './Michelf/Markdown.inc.php';
use \Michelf\Markdown;
require_once 'loader.php';

$json_a = load();

if(count($json_a) > 0)
{
  $text = $json_a[0]['blog'];
  echo "found";
}
else
  echo "not found";

$markdown = Markdown::defaultTransform($text);

echo $markdown;

echo "<script> var test = 0; simplemde.value('test'); test+= 5;
alert(test);</script>";
echo "<script> var test = 0; simplemde.value('$markdown'); 
test+= 5; alert(test);</script>";

?>
</body>
</html>
7
  • 3
    Start debugging... Any error in console ? Is your script rendered as you wanted it to be ? Commented May 4, 2016 at 11:57
  • Do you got alert two times or one? Commented May 4, 2016 at 11:57
  • @missjohanna visit the page and see JavaScript code in source code Commented May 4, 2016 at 12:01
  • @Rayon I'm getting Uncaught SyntaxError: Unexpected token ILLEGAL. Commented May 4, 2016 at 12:19
  • @jekaby Nope. Sry I missed to say that I just get it while I'm using the plain text. Commented May 4, 2016 at 12:19

1 Answer 1

2

Things your question is missing:

  • The value of $markdown
  • The resulting JS you get for running the PHP
  • The error messages displayed on your browser's Developer Tools console.

That said, we can make some assumptions.

Markdown is a text markup language designed to write documents in something that looks a lot like plain text. One of its key features are new lines (used to delimit paragraphs).

Literal new line characters are not allowed in JavaScript string literals.

(Even without those assumptions, it is reasonable to say (for any answer to a question about replacing a string literal with some unspecified text):)

You need to escape or otherwise encode characters in $markdown that have special meaning.

The easiest way to do this is to take advantage of JSON being more-or-less the same as JS literal syntax.

$js_string = json_encode($markdown);
echo "<script> var test = 0; simplemde.value($js_string); 
      test+= 5; alert(test);</script>";

Note that $js_string gets quotes of its own, so you don't need to manually include them when outputting the variable.

Sign up to request clarification or add additional context in comments.

2 Comments

Not all json can be embedded safely; one needs to ensure that \u2028 and \u2029 and / are escaped in strings as well. json_encode does this correctly by default but you can do it incorrectly by adding flags, or using a JSON string encoded with a different encoder, including EcmaScript JSON.stringify
@AnttiHaapala — json_encode escapes all of those by default

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.