[英]How to create a download file and not save it in server with javascript
[英]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.