我创建了一个提交到PHP脚本的HTML表单。该脚本应该只将动态创建的input
元素的内容写为alert
JavaScript函数的参数。输入来自WYSIWYG编辑器(CKEditor)。
当我在编辑器中输入多行文本并按下表单的提交按钮时,Firebug会显示woa.php
(表单的目标)产生错误:
SyntaxError: unterminated string literal
alert("<p>Hi watsup<p>
我该如何解决这个问题?
带表单的页面:
<html>
<head>
<script src="./ckeditor/ckeditor.js"></script>
</head>
<body>
<form method="post" action="woa.php" id="myForm">
<textarea name='editor1'></textarea>
<script>
window.onload = function() {
var textContent = CKEDITOR.replace('editor1');
var button = document.getElementById("button");
button.onclick = function(event) {
event.preventDefault();
var userInput = document.createElement("input");
userInput.type = "hidden";
userInput.name = "userInput";
userInput.value = textContent.getData();
var myForm = document.getElementById("myForm");
myForm.appendChild(userInput);
}
}
</script>
<button id="button">finish edit</button>
<button>save</button>
</form>
</body>
</html>
表单的目标(woa.php
):
<html>
<head>
<script>
alert("<?php echo $_POST['userInput']; ?>");
</script>
</head>
<body>
</body>
</html>
答案 0 :(得分:1)
JavaScript不喜欢字符串中的换行符。您必须将换行符更改为转义序列。使用
alert("<?php echo strtr($_POST["userInput"], array("\r" => '', "\n" => '\n')); ?>");