我正在開發一個網站,讓用戶只需保存一個便條,以便以後訪問。然而,我實際上在保存筆記時遇到了一些麻煩。我首先將它設置爲在每次按鍵時自動保存,但如果我讓用戶按下按鈕保存文件,則會更好。代碼中還會看到,筆記被保存爲用戶的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地址相同的文件。請記住,我仍然是一個擁有這些更高級功能的新手,所以請指出我做錯了什麼(但也請簡單說明一下:))。
感謝您的閱讀,
瑪特。
謝謝修復名稱部分。現在,當我運行它但是我得到的錯誤:未定義的索引:在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