服务器的Ajax保存功能无法正常工作
问题描述:
我正在开发一个网站,让用户只需保存一个便条,以便以后访问。然而,我实际上在保存笔记时遇到了一些麻烦。我首先将它设置为在每次按键时自动保存,但如果我让用户按下按钮保存文件,则会更好。代码中还会看到,笔记被保存为用户的IP地址,当用户再次访问该站点时,他会看到相同的笔记(如果他再次拥有相同的IP)。服务器的Ajax保存功能无法正常工作
我现在得到点击保存按钮时的错误是:
PHP Warning: file_put_contents() [<a href='function.file-put-contents'>function.file-put-contents</a>]: Filename cannot be empty in /home/martmart/public_html/index.php on line 41
我的index.php:
<?php
$note_name = 'note.txt';
$uniqueNotePerIP = true;
if($uniqueNotePerIP){
// Use the user's IP as the name of the note.
// This is useful when you have many people
// using the app simultaneously.
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
$note_name = 'notes/'.$_SERVER['HTTP_X_FORWARDED_FOR'].'.txt';
}
else{
$note_name = 'notes/'.$_SERVER['REMOTE_ADDR'].'.txt';
}
}
if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])){
// This is an AJAX request
if(isset($_POST['note'])){
// Write the file to disk
file_put_contents($note_name, $_POST['note']);
echo '{"saved":1}';
}
exit;
}
$note_content = 'Write something here :D';
if(file_exists($note_name)){
$note_content = htmlspecialchars(file_get_contents($note_name));
}
function saveNow() {
// Write the file to disk
file_put_contents($note_name, $_GET['note']);
echo '{"saved":1}';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Marty Testweb</title>
<!-- Our stylesheet -->
<link rel="stylesheet" href="assets/css/styles.css" />
<!-- A custom google handwriting font -->
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Courgette" />
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script src="assets/audiojs/audio.min.js"></script>
<script>
audiojs.events.ready(function() {
var as = audiojs.createAll();
});
</script>
</head>
<body>
<div id="pad">
<h2>Note</h2>
<textarea id="note"><?php echo $note_content ?></textarea>
</div>
<!-- Initialise scripts. -->
<script>
function saveNow()
{
alert("<?php saveNow(); ?>");
}
</script>
<button id="save" onclick="saveNow()">Save Note</button>
<!-- JavaScript includes. -->
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="assets/js/script.js"></script>
</body>
<div id="footer">
<footer>
<a href="">
<div id="footer_right">
Version 0.1.2
</div id="footer_right">
</a>
<audio src="assets/audiojs/music.mp3" preload="auto"></audio>
<div id="footer_left">
Save function not working yet
</div id="footer_left">
</footer>
</div id="footer">
</html>
的的script.js:
$(function(){
var note = $('#note');
var saveTimer,
lineHeight = parseInt(note.css('line-height')),
minHeight = parseInt(note.css('min-height')),
lastHeight = minHeight,
newHeight = 0,
newLines = 0;
var countLinesRegex = new RegExp('\n','g');
// The input event is triggered on key press-es,
// cut/paste and even on undo/redo.
note.on('input',function(e){
// Count the number of new lines
newLines = note.val().match(countLinesRegex);
if(!newLines){
newLines = [];
}
// Increase the height of the note (if needed)
newHeight = Math.max((newLines.length + 1)*lineHeight, minHeight);
// This will increase/decrease the height only once per change
if(newHeight != lastHeight){
note.height(newHeight);
lastHeight = newHeight;
}
}).trigger('input'); // This line will resize the note on page load
function ajaxSaveNote(){
// Trigger an AJAX POST request to save the note
$.post('index.php', { 'note' : note.val() });
}
});
我不我真的知道如何解决这个问题,所以任何帮助都非常感谢。当他点击按钮时,我只想保存与用户IP地址相同的文件。请记住,我仍然是一个拥有这些更高级功能的新手,所以请指出我做错了什么(但也请简单说明一下:))。
感谢您的阅读,
玛特。
答
首先,我会建议考虑是否将文件名作为IP地址是一个好主意......许多工作场所和其他组设置共享相同的IP地址,因此任何在这样的工作场所的用户都会看到该笔记由同一工作场所的任何其他用户留下。
至于你的错误,我认为问题可能是你没有在你的函数中声明$note_name
作为全局变量。尝试将其更改为这样:
function saveNow() {
global $note_name;
// Write the file to disk
file_put_contents($note_name, $_GET['note']);
echo '{"saved":1}';
}
在PHP中,如果你想使用全局变量的函数内(即没有一个函数或类内声明的),你总是要使用global
关键字如图所示以上。如果你用不同的方式构造你的代码,那么完全可以避免使用全局变量,但这是另一个话题。
我不知道为什么你没有得到关于它没有被定义的,虽然......,而你还在开发你可能想要把这个在你的代码的顶部通知:
error_reporting(E_ALL);
附:虽然您的代码不会这样做,但出于安全原因,在从PHP输出JSON之前指定JSON MIME类型会很好,例如:
function saveNow() {
global $note_name;
// Write the file to disk
file_put_contents($note_name, $_GET['note']);
header('Content-type: application/json');
echo '{"saved":1}';
exit;
}
谢谢修复名称部分。现在,当我运行它但是我得到的错误:未定义的索引:在index.php中的笔记我想我也必须放置全局的'笔记'的地方,但在哪里? – Martyn 2013-05-11 03:03:51
“undefined index”错误是指未定义的数组元素/键,所以实际上,“未定义索引”错误永远不会通过添加“全局”声明来解决。这可能是由'$ _GET ['note']'行引起的。它看起来像你的Javascript只有通过POST发送数据,所以你应该使用'$ _POST ['note']'来代替。 – 2013-05-11 03:13:07
谢谢但是,现在使用POST,仍然是“未定义索引”错误。任何解决方案? – Martyn 2013-05-11 03:21:09