これは私の問題です: ajax 経由で「id」と「a html string」という 2 つの値を送信する html ドキュメントがあります。
<div class="myClass"><h1 class="myClass">this is my html string </h1></div>
このデータを .php ファイルで受け取り、データを .html ファイルに保存します。
ajax コード:
$(".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 ファイルにクラスが本当に必要です。