[英]Why javascript code with a php var isn't working?
I want to change the content in javascript of a textbox while using PHP. 我想在使用PHP时更改文本框的javascript中的内容。
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:
但是使用php-Variable它将不再起作用:
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>
Things your question is missing: 您的问题缺少的东西:
$markdown
$markdown
的值 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. Markdown是一种文本标记语言,旨在以看上去很像纯文本的形式编写文档。 One of its key features are new lines (used to delimit paragraphs).
它的主要功能之一是换行(用于分隔段落)。
Literal new line characters are not allowed in JavaScript string literals. JavaScript字符串文字中不允许使用文字换行符。
(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. 您需要对
$markdown
中具有特殊含义的字符进行转义或以其他方式编码。
The easiest way to do this is to take advantage of JSON being more-or-less the same as JS literal syntax. 最简单的方法是利用JSON与JS文字语法大致相同的优势。
$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. 请注意,
$js_string
获取其自己的引号,因此在输出变量时无需手动将其包括在内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.