php- 更新 fwrite 提交后不刷新
php- update fwrite without any refresh after submit
我正在使用此 php 代码来编辑 .txt 文件并更新它。
<?php
$fichier=text.txt";
if(isset($_POST['update'])) {
$ouverture=fopen("$fichier","w");
fwrite($ouverture,"$_POST[modif]");
fclose($ouverture);
echo '<h2>Modification effectue</h2>'; // validation
}
?>
<form method="post" action="">
<textarea name="modif">
<?php
if(is_file($fichier)) echo file_get_contents($fichier);
?>
</textarea>
<input type="submit" name="update">update</input>
</form>
代码独立运行,但一旦在我的最终网站结构上实施,提交后的刷新效果将我带到网站的根目录(+脚本未保存)。
有没有不用刷新页面就可以用fwrite更新的方法?
/已找到解决方案/
从那个问题推导出
index.php
<script>
//when you click save changes, we get its id="save"
//and prevent default submission
$(document).on("click", "#save", function(e){
e.preventDefault();
//get the textarea data bu its id="demo"
var textdata = $('#demo').val();
mydata= 'testdata='+textdata;
$.ajax({
type:'POST',
data:mydata,
url:'update.php',
success:function(data) {
if(data){
alert('Saved!');
$("#demo").html(data);//load data from update.php
}else{
alert('Update failed');
}
}
});
});
</script>
<form method="POST">
<?php
$myfile = fopen('test.txt', 'r');
echo "<textarea id='demo'>";
// go through each line in the file, print its contents.
while(!feof($myfile)) {
echo fgets($myfile);
}
echo "</textarea><br>";
?>
<input type="submit" id="save" value="Save changes" />
</form>
update.php
<?php
$data_to_write = $_POST['testdata'];
$file_path = 'test.txt';
$file_handle = fopen($file_path, 'w');
fwrite($file_handle, $data_to_write);
fclose($file_handle);
$myfile = fopen('test.txt', 'r');
while(!feof($myfile)) {
echo fgets($myfile);
}
fclose($myfile);
?>
text.txt
"lorem ipsum"
我正在使用此 php 代码来编辑 .txt 文件并更新它。
<?php
$fichier=text.txt";
if(isset($_POST['update'])) {
$ouverture=fopen("$fichier","w");
fwrite($ouverture,"$_POST[modif]");
fclose($ouverture);
echo '<h2>Modification effectue</h2>'; // validation
}
?>
<form method="post" action="">
<textarea name="modif">
<?php
if(is_file($fichier)) echo file_get_contents($fichier);
?>
</textarea>
<input type="submit" name="update">update</input>
</form>
代码独立运行,但一旦在我的最终网站结构上实施,提交后的刷新效果将我带到网站的根目录(+脚本未保存)。
有没有不用刷新页面就可以用fwrite更新的方法?
/已找到解决方案/
从那个问题推导出
index.php
<script>
//when you click save changes, we get its id="save"
//and prevent default submission
$(document).on("click", "#save", function(e){
e.preventDefault();
//get the textarea data bu its id="demo"
var textdata = $('#demo').val();
mydata= 'testdata='+textdata;
$.ajax({
type:'POST',
data:mydata,
url:'update.php',
success:function(data) {
if(data){
alert('Saved!');
$("#demo").html(data);//load data from update.php
}else{
alert('Update failed');
}
}
});
});
</script>
<form method="POST">
<?php
$myfile = fopen('test.txt', 'r');
echo "<textarea id='demo'>";
// go through each line in the file, print its contents.
while(!feof($myfile)) {
echo fgets($myfile);
}
echo "</textarea><br>";
?>
<input type="submit" id="save" value="Save changes" />
</form>
update.php
<?php
$data_to_write = $_POST['testdata'];
$file_path = 'test.txt';
$file_handle = fopen($file_path, 'w');
fwrite($file_handle, $data_to_write);
fclose($file_handle);
$myfile = fopen('test.txt', 'r');
while(!feof($myfile)) {
echo fgets($myfile);
}
fclose($myfile);
?>
text.txt "lorem ipsum"