这是我的问题:我有我的 html 文档,我通过 ajax 发送两个值:“id”和“html 字符串”
<div class="myClass"><h1 class="myClass">this is my html string </h1></div>
我在 .php 文件中收到此数据,并将数据保存在 .html 文件中:
阿贾克斯代码:
$(".button").on("click", function(e){
e.preventDefault();
var string = $(".htmlString").html();
$.ajax({
url: "data.php",
type: "post",
data: {
ID: "correctID",
htmlString: string
},
success: function(){
alert('ok');
},
error:function(){
alert('error');
}
});
});
php代码:
if ($_POST['id'] == "correctID") {
$fileLocation = $_SERVER['DOCUMENT_ROOT'] . "/www/file.html";
$file = fopen($fileLocation,"w");
$content = $_POST['htmlString'];
fwrite($file,$content);
fclose($file);
}
输出的 .html 文件内容如下:
<div class=\"myClass\"><h1 class=\"myClass\">
您看到的问题是引号前的“\”:
如何以正确的 html 格式保存我的文件?
<div class="myClass"><h1 class="myClass">
非常感谢,正在寻找,我找到了 DOMDocument::saveHTML 但我无法使用它,我对 PHP 很陌生..,我真的需要我的 html 文件中的类。