我的 textarea 不接受
并&
使用 AJAX 和 PHP 保存页面内容。
这是使用带有 ajax 的 textarea 的编辑器页面:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function save(){
var x = $("textarea").val();
var data = 'c='+x;
$.ajax({
type: 'POST',
url: 'save.php',
data: data,
success: function(e){
$("#s").html(e);
}
});
}
</script>
</head>
<body>
<textarea>
<?php
$fn = "blank.html";
//FILE TO BE EDITED (FILENAME EDITABLE)
$file = fopen($fn, "r+"); //OPENS IT
$fr = fread($file, 1000000); //READS IT
fclose($file); //CLOSE CONNECTIONS
echo $fr; //SHOWS THE EDITABLE FILE HERE
?>
</textarea><br>
<input onClick="save()" id="x" type="button" value="Save"><br><br>
<span id="s"></span><br>
<a href="blank.html" target="_new">view file</a>
</body>
</html>
这是save.php
代码:
<?php
$c = $_POST["c"];
//TEXT FROM THE FIELD
$f = 'blank.html';
//FILE TO SAVE (FILENAME EDITABLE)
$o = fopen($f, 'w+'); //OPENS IT
$w = fwrite($o, $c); //SAVES FILES HERE
$r = fread($o, 100000); //READS HERE
fclose($o); //CLOSES AFTER IT SAVES
//DISPLAYS THE RESULTS
if($w){
echo 'File saved';
} else {
echo 'Error saving file';
}
?>