2014-01-06 82 views
0

嘗試按下按鈕時寫入到txt文件。不知道該怎麼做,我的代碼沒有工作。我開始想我可能需要AJAX?如果是這樣,有人能給我一個正確的方向快速指針?請提前謝謝你!在按下按鈕時寫入文件

這裏是我的代碼:

<?php 
//write to file on button event 

function buttonWrite() 
{ 
$filename = 'button.txt'; 
@ $fp = fopen("messages/".$filename, 'wb'); 
if (!$fp) 
{ 
    echo '<p><strong>Cannot generate message file</strong></p></body></html>'; 
    exit; 
} 

$outputstring = 'Hello, i have been generated by the button'; 

fwrite($fp, $outputstring); 
} 

?> 

<html> 

<head> 
<title>Button Writer</title> 
</head> 

<body> 
<button type="button" onclick="buttonWrite()">Write to File</button> 
</body> 

</html> 

更新! 這些答案都沒有真正的工作..不知道是否它的服務器即時通訊託管或什麼,但是是隻是不工作..任何想法傢伙?

+0

Ps。我完全是AJAX新手..所以請不要讓它太難 – amartin94

+0

你不能從HTML中調用PHP函數。正如你所說的,你需要對php文件進行AJAX調用,這又會超出代碼。 – Eric

+0

JavaScript是前端腳本,因此無法訪問服務器上的文件;所以是的,你需要Ajax調用PHP腳本,這將爲你做的工作。以下是您的首發:http://www.tizag.com/ajaxTutorial/ – Peon

回答

1

創建PHP文件 .Lets說 「abc.php」。它包含:

<?PHP 
$filename = 'button.txt'; 
@ $fp = fopen("messages/".$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"; 
} 
?> 

HTML文件

<html> 
<head> 
<title>Button Writer</title> 
<script type='text/javascript' src='//code.jquery.com/jquery-1.10.1.js'></script> 
</head> 

<body> 
<button id="button1" type="button">Write to File</button> 
</body> 
<script type='text/javascript'> 
$('#button1').click(function(){ 
$.ajax({ 
type: "POST", 
url: "abc.php", 
data: "", 
success: function(msg){ 
    alert(msg); 
}, 
error: function(XMLHttpRequest, textStatus, errorThrown) { 
    alert("Some error occured"); 
} 
}); 
}); 
</script> 
</html> 
1

整個代碼

Save.php

<?php 
     //write to file on button event 

     function buttonWrite() 
     { 
     $filename = 'button.txt'; 
     $outputstring = "Hello, i have been generated by the button'\n"; 
     file_put_contents($filename, $outputstring, FILE_APPEND | LOCK_EX); 
     } 

     ?> 

的index.php或index.html

<html> 

     <head> 

     </head> 

     <body> 
<form name="input" action="save.php"> 
<input type="submit" value="Write to File"> 
</form> 

     </html> 

這應該會使用的$ outputstring寫入button.txt file_put_contents功能

+0

和js函數上的點擊回調描述在哪裏? –

+0

那麼我在哪裏把file_put_contents? – amartin94

+0

我轉貼它應該工作 – Ghermans41

1

你做錯了 - JavaScript和PHP不能執行到在HTML中彙集。 你需要做的是創建:

1)「onclick」按鈕事件,執行AJAX請求到您的服務器。爲了簡單起見,我將在我的示例中使用jQuery

<button id="btn">SAVE FILE</button> 

<script type="text/javascript"> 
    // bind click event to button 
    $('#btn').click(function() { 
    // send ajax request to save.php (using POST method) 
    $.post('save.php', { text: "sample text", function(data) { 
     // output response to console 
     console.log(data); 
    }); 
    }); 
</script> 

2)創建save.php文件 - 這將是負責爲您的數據保存到文件

<?php 
    // saving sample text to file (it doesn't include validation!) 
    file_put_contents('file.txt', $_POST['text']); 

    die('file has been saved'); 
?> 
+0

Does not work?任何想法是爲什麼?我是否需要更改服務器上的權限\ – amartin94

+0

您有什麼錯誤? – tuffkid

+0

沒有..我不在我的主要個人電腦上,所以我不能運行WAMP,因此即時通訊FTP到服務器,並直接運行它..所以沒有錯誤,但是我點擊按鈕和DIE消息甚至不執行,暗示它沒有使它那麼遠 – amartin94