2013-05-11 89 views
1

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

感謝您的閱讀,

瑪特。

回答

1

首先,我會建議考慮是否將文件名作爲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; 
} 
+0

謝謝修復名稱部分。現在,當我運行它但是我得到的錯誤:未定義的索引:在index.php中的筆記我想我也必須放置全局的'筆記'的地方,但在哪裏? – Martyn 2013-05-11 03:03:51

+0

「undefined index」錯誤是指未定義的數組元素/鍵,所以實際上,「未定義索引」錯誤永遠不會通過添加「全局」聲明來解決。這可能是由'$ _GET ['note']'行引起的。它看起來像你的Javascript只有通過POST發送數據,所以你應該使用'$ _POST ['note']'來代替。 – 2013-05-11 03:13:07

+0

謝謝但是,現在使用POST,仍然是「未定義索引」錯誤。任何解決方案? – Martyn 2013-05-11 03:21:09