簡體   English   中英

如何通過按Java中的按鈕將文件保存到服務器

[英]How to save a file to the server by pressing a button in Javascript

好的,所以我正在HTML5中進行實驗,並使用Javascript創建了一個簡單的“繪畫”應用程序,以繪制用戶鼠標在屏幕上的位置。 工作正常。

然后,我想將坐標保存到文件中。 我的程序已經有一個來自Javascript代碼的x坐標數組和y坐標數組。

當用戶按下按鈕時,onClick調用Javascript中的一個函數,該函數使用jQuery,如此處的最佳答案中所示。 如何將JavaScript函數數據獲取到PHP變量中,嘗試將其傳遞到php文件中進行保存。

但是,它不起作用。 我應該將數據傳遞回包含畫布的原始php文檔中嗎? 如果是這樣,那么我該如何做,以便在不加載文檔時運行PHP以保存代碼?

碼:

好的,這是在原始php文件中,該文件包含網頁的HTMl(包括畫布)。 這是相關的保存按鈕:

<button type="button" onclick="saveDrawing()" id="saveButton">Save</button>

這將在單獨的JS文件中調用以下內容

function saveDrawing(){
// First check that not drawing and have data
if (!readyToDraw && clickX!=null){  
    // If ready then pass back to the PHP file the data
    $url = 'file_save_test.php';
    $.get($url, {x_coords: getXCoords(), y_coords: getYCoords()});
    }
else {
    alert("Please add some coordinate points and press Finish before saving");
}
}

和file_save_test.php僅包含以下內容

<?php

// retrieve data from the JS
$buffer_data['x_coords'] = $_GET['x_coords'];
$buffer_data['y_coords']  = $_GET['y_coords'];

$x_s = $_GET['x_coords'];
$y_s = $_GET['y_coords'];

// first want to open a file
$file_name = "data_test.txt";
$file_handler = fopen($file_name, 'w');

// now to loop through arrays and write!
/*for ($i = 0; $i < sizeof($x_s); i++){
    fwrite($file_handler, "$x_s[i], ");
    fwrite($file_handler, "$y_s[i]\n");
} */


fclose($file_handler);


?>

在您的PHP文件中,您的fwrite代碼似乎已被注釋掉。 您是否希望它寫入該data_test.txt文件? 嘗試更改您的PHP文件以打印結果,並將其回顯到JavaScript,以查看數據是否正確通信。

$.get($url, {x_coords: getXCoords(), y_coords: getYCoords()},
    function(data){
        alert("Data Loaded: " + data);
    });

的PHP

print_r($_GET);

編輯

如果可以正確警告數據,請將您的PHP文件更改為如下所示(應將坐標附加到文件中):

<?php

// retrieve data from the JS
$x_s = $_GET['x_coords'];
$y_s = $_GET['y_coords'];

$file_name = "data_test.txt";
$file_handler = fopen($file_name, 'a');
fwrite($file_handler, "$x_s, $y_s \n");
fclose($file_handler);

?>

編輯2

for循環更新for原始代碼

for ($i = 0; $i < count($x_s); $i++){
    fwrite($file_handler, $x_s[$i] . ", ". $y_s[$i] . "\n");
}

我要做的是讓您的保存按鈕調用jQuery的$ .post()方法。 將數據發布到另一個PHP文件中,該文件將其插入數據庫或另存為文件。 我不建議使用原始文檔將數據發布到其中,因為客戶端將必須下載整個DOM,而服務器將運行您不需要的任何其他代碼。

在不看任何代碼的情況下,我可以為您提供真正的幫助。

我會將數據發送到一個名為saveCoords.php或諸如此類的新PHP腳本中。

您說您已經在JavaScript數組中有了坐標,所以看起來像這樣...

//JAVASCRIPT FUNCTION which will request php file to store info
function storeCoords(xCoordArray, yCoordArray){
  var xCoords = JSON.stringify(xCoordArray);
  var yCoords = JSON.stringigy(yCoordArray);

  var request = new XMLttpRequest(); //will need to change for older ie 
  request.onreadystatechange = function(){
    //functions to handle returning information from php file..
  }

  request.open("GET","saveCoords.php?xCoords="+xCoords+"&yCoords="+yCoords, true);
  request.send();
}

然后,saveCoords.php文件將如下所示:

<?php
  $xCoords = json_decode($_GET['xCoords']);
  $yCoords = json_decode($_GET['yCoords']);

  //now you have a php array of xCoords and yCoords, which you can store
?>

多數民眾贊成在骨架上,但我認為它很重要,但如有任何疑問,請置評。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM