【发布时间】:2015-09-14 01:33:01
【问题描述】:
我在我的 Chromebook 上使用 Cloud9IDE 并尝试用 PHP 编写一个新文件。
基本上,当我单击一个按钮时,我使用 JQuery 的$.ajax 来读取我的 php 文件并从 php 脚本创建 hello.txt 文件。我有一个警报设置,所以它正在调用函数并读取 php 文件,但我的新文件没有被制作。
我的问题是为什么这不起作用,我该如何解决这个问题
index.js
$("[data-action=writefile]").click(function(){
$.ajax({
url: 'save.php',
success: function(msg) {
alert('file saved: \n' + msg);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Some error occured");
}
});
});
save.php
<?php
$filename = 'hello.txt';
@ $fp = fopen("exports/".$filename, 'wb');
if (!$fp) {
echo '<p><strong>Cannot generate message file</strong></p></body></html>';
exit;
} else {
$outputstring = 'Hello, i have been generated by the button';
fwrite($fp, $outputstring);
Echo "Message inserted";
}
?>
index.html
<button class="btn--default" data-action="writefile">Write to File</button>
【问题讨论】:
标签: javascript php jquery ajax cloud9-ide