0

好的,所以我正在尝试使用 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);


?>
4

3 回答 3

1

在您的 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 ($i = 0; $i < count($x_s); $i++){
    fwrite($file_handler, $x_s[$i] . ", ". $y_s[$i] . "\n");
}
于 2012-03-02T23:15:00.120 回答
0

我要做的是让你的保存按钮调用 jQuery 的$.post()方法。将数据发布到另一个 PHP 文件,该文件将其插入数据库或将其保存为文件。我不建议使用原始文档来发布数据,因为客户端必须下载整个 DOM,而服务器会运行您不需要的任何其他代码。

这就是我可以在不查看您的任何代码的情况下真正为您提供的帮助。

于 2012-03-02T22:57:01.613 回答
0

我会将数据发送到一个名为 saveCoords.php 或其他东西的新 php 脚本中。

你说你已经在 J​​avaScript 数组中有坐标,所以它看起来像这样......

//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
?>

Thats a skeleton but I think it hits on the major points, but comment with any questions.

于 2012-03-02T23:24:13.307 回答