0

Ok so I'm experimenting with the in HTML5 and have made a simple "paint" application in Javascript to draw where the user's mouse is on the screen. Works fine.

I then wanted to save the coordinates to a file. My program already had an array of the x coordinates and an array of the y coordinates from the Javascript code.

When the user presses a button, the onClick calls a function in the Javascript, which using jQuery, as in the Top Answer here How to get JavaScript function data into a PHP variable attempts to pass this into a php file to save.

However it isn't working. Should I be passing the data back into the original php document that contains the canvas? If so how do I then get it to do the code to save as the PHP is run when the document is loaded no?

CODE:

Ok this is in the original php file which contains the HTMl for the webpage including the canvas. Here's the relevant save button:

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

This calls the following in a separate JS file

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");
}
}

and file_save_test.php contains only the following

<?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);


?>
Community
  • 1
  • 1
Stuart Lacy
  • 1,963
  • 2
  • 18
  • 30

3 Answers3

1

In your PHP file it looks like your fwrite code is commented out. Are you expecting it to write to that data_test.txt file? Try changing your PHP file to print the results and have it echoed back to your javascript to see if the data is getting communicated properly.

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

PHP

print_r($_GET);

EDIT

Change your PHP file to something like this if it's alerting the data properly (it should append the coords to your file):

<?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);

?>

EDIT 2

Update your for loop to your original code

for ($i = 0; $i < count($x_s); $i++){
    fwrite($file_handler, $x_s[$i] . ", ". $y_s[$i] . "\n");
}
Aaron W.
  • 9,254
  • 2
  • 34
  • 45
  • The javascript alert correctly showed the data so that's working. What should this be doing? print_r($_GET); I put it in the php file but not sure where to see the output – Stuart Lacy Mar 02 '12 at 23:23
  • Also I had the fwrite loop commented out when I was testing just using fwrite on a single string but that didn't work either – Stuart Lacy Mar 02 '12 at 23:24
  • I don't have a text file with that name, I was using write to try and create a new file. I created a text file with that name manually and tried to append the data to it but it wasn't working still. – Stuart Lacy Mar 02 '12 at 23:39
  • Ok I commented out all my code and just added in what you put in your first edit and I got it to finally write to the text file! I'll have a look later to see what I've done wrong in my code. However rather than writing the array values to the file it just outputs "Array, Array" – Stuart Lacy Mar 02 '12 at 23:49
  • Try `fwrite($file_handler, "X_COORDS:".print_r($x_s, true) . " Y_COORDS:". print_r($y_s,true)."\n");` since I guess you're passing array values...or use the original `for` loop you had to write using the `fwrite` example (See edit) – Aaron W. Mar 03 '12 at 00:24
  • Your Edit 2 works perfectly, thanks so much for helping me with this! – Stuart Lacy Mar 03 '12 at 00:37
  • Don't know if you'll see this but having issues now with saving this http://stackoverflow.com/questions/9839435/php-having-issues-saving-large-files and wondering why you used GET in your answer? Would POST make much of a difference? – Stuart Lacy Mar 23 '12 at 13:45
0

What I would do is have your save button call jQuery's $.post() method. Post the data to another PHP file that either inserts it into a database or saves it as a file. I don't recommend using the original document to post the data to because the client would have to download the entire DOM and the server would run any other code that you don't need.

That's as much as I can really help you without seeing any of your code.

HellaMad
  • 5,294
  • 6
  • 31
  • 53
  • Ah ok. I think I've tried doing that but with $.get() rather than post. I've included my code now. As you can see I'm trying to post it to a new document – Stuart Lacy Mar 02 '12 at 23:07
0

I would send the data into a new php script called saveCoords.php or something..

You say you already have the coordinates in a JavaScript array, so it would look something like this...

//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();
}

And then saveCoords.php file would look something like this:

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

dano
  • 1,043
  • 1
  • 6
  • 11
  • It's alright. What does the JSON.stringify() and json_decode() do? Are they needed before an array can be sent using GET? – Stuart Lacy Mar 02 '12 at 23:34