好的,所以我正在尝试使用 HTML5 并在 Javascript 中制作了一个简单的“绘画”应用程序来绘制用户鼠标在屏幕上的位置。工作正常。
然后我想将坐标保存到文件中。我的程序已经有一个来自 Javascript 代码的 x 坐标数组和一个 y 坐标数组。
当用户按下按钮时,onClick 调用 Javascript 中的函数,该函数使用 jQuery,如此处的最佳答案How to get JavaScript function data into a PHP variable尝试将其传递到 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);
?>