Php 使用ajax写入文件
我以前从来没有真正使用过AJAX,所以我试着熟悉一下。我有一个html页面:Php 使用ajax写入文件,php,ajax,Php,Ajax,我以前从来没有真正使用过AJAX,所以我试着熟悉一下。我有一个html页面: <html> <script type="text/javascript" src="jquery-1.12.3.min.js"></script> <script> function write(){ $.ajax({ type: 'POST', url: "write.php", data: "
<html>
<script type="text/javascript" src="jquery-1.12.3.min.js"></script>
<script>
function write(){
$.ajax({
type: 'POST',
url: "write.php",
data: "something",
success: function(result) {
alert('the data was successfully sent to the server');
}
})
}
</script>
Click to write some stuff
<a href="javascript:write()" class="write">Write</a>
</html>
函数写入(){
$.ajax({
键入:“POST”,
url:“write.php”,
数据:“某物”,
成功:功能(结果){
警报(“数据已成功发送到服务器”);
}
})
}
点击写一些东西
以及同一目录中的相关write.php文件:
<?php
error_reporting(E_ALL);
$data = $_POST['data'];
$f = fopen('file.txt', 'w+');
fwrite($f, $data);
fclose($f);
?>
当我单击链接时,我会收到成功消息,但该文件不是在服务器上创建的。不太确定要调查什么。我已经确认我有目录的写访问权了看,如果你要使用jQuery,请一直使用它。首先,你的链接不应该有任何内联JavaScript
<a href="#" class="write">Write</a>
,请改用console.log()
success: function(result) {
console.log('the data was successfully sent to the server');
}
});
});
</script>
现在文本文件应该包含“foo”,因为您实际上已经向PHP脚本发送了一些可以解析的内容。您需要做的另一件事是确保PHP脚本具有打开和写入服务器上文件的权限。看,如果要使用jQuery,请一直使用它。首先,你的链接不应该有任何内联JavaScript
<a href="#" class="write">Write</a>
,请改用console.log()
success: function(result) {
console.log('the data was successfully sent to the server');
}
});
});
</script>
现在文本文件应该包含“foo”,因为您实际上已经向PHP脚本发送了一些可以解析的内容。您需要做的另一件事是确保PHP脚本具有打开和写入服务器上文件的权限。然后查看控制台并将错误报告行发送给PHP。点击linkadd
ini\u set('display\u errors',1),在控制台中看不到任何错误代码>之后,$\u POST
不包含您认为它包含的内容。您需要养成帮助您解决问题的习惯。您将获得积分,并鼓励其他人帮助您。请看您的控制台,并将错误报告行添加到php。点击linkaddini\u set('display\u errors',1),在控制台中看不到任何错误代码>之后,$\u POST
不包含您认为它包含的内容。您需要养成帮助您解决问题的习惯。您将获得积分,并鼓励其他人帮助您。
<?php
error_reporting(E_ALL);
$data = $_POST['something']; // the key we sent was "something"
$f = fopen('file.txt', 'w+');
fwrite($f, $data);
fclose($f);
?>