我有一个HTML内容可编辑表,该表通过ajax和PHP fwrite写入文件。它的工作原理是,除非其中一个内容可编辑项中有空格<或>(
或<
或>
),否则不会进行书写,也不会显示错误代码。所有代码都在一个.php文件中。
var table = $('.table-editable')[0].outerHTML;
var data = 'data='+table+'?filename='+filename;
//var dataencoded = encodeURI(data);
$.ajax({
url: "index.php",
type: "post",
dataType:"html",
data: data ,
success: function (response) {
// you will get response from your php page (what you echo or print)
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
response.innerHTML='created';
}
<?php
error_reporting(E_ALL);
var_dump($_SERVER);
$post_data = $_POST['data'];
$datas = explode("?filename=", $post_data);
if (!empty($datas[0])) {
$filename = $datas[1].'.php';
$towrite = htmlspecialchars_decode($datas[0]);
$handle = fopen($filename, "w");
fwrite($handle, $towrite);
fclose($handle);
echo $filename;
}
?>