Saving data as a file to a web server

1,557 views
Skip to first unread message

shivamp...@gmail.com

unread,
Jan 16, 2018, 2:53:50 PM1/16/18
to jsPsych
Hello,

I have the completed code for a lexical decision task that I am trying to host on a website. The actual experiment runs, and I can save files locally, but I’m not able to save the output to the server (this was also the case when I tried to test it on a local server using XAMPP).

Here is the final part of the code that links to the .php file:
/* start the experiment */
jsPsych.init({
timeline: timeline,
on_finish: function() {



saveData(ID+'.csv',jsPsych.data.get().filter({test_part: 'test'}).csv());
jsPsych.data.displayData();
}
});

/*save the data to the database*/

function saveData(name, data){
var xhr = new XMLHttpRequest();
xhr.open('POST', 'something.php'); // 'write_data.php' is the path to the php file described above.
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({filename: name, filedata: data}));
}

ID in the saveData function is itself a string to indicate the response ID of the person doing the experiment (this works when saving locally).

And this is the .php file itself:
<?php
// the $_POST[] array will contain the passed in filename and filedata
// the directory "data" must be writable by the server
$filename = "data/".$_POST['filename'];
$data = $_POST['filedata'];
// write the file to disk
file_put_contents($filename, $data);

?>

When the experiment ends, I am able to see the results via the displaydata function, but nothing is saved to my data folder. I’ve made sure that the permissions on both the web server’s and XAMPP server’s data folders are set so that they can be written to by anything, but for some reason can’t get a CSV output to the folder. Am I missing something simple?

Thanks in advance for any help and please let me know if there is any other information I need to give.

Josh de Leeuw

unread,
Jan 16, 2018, 10:27:12 PM1/16/18
to shivamp...@gmail.com, jsPsych
Hi,

I don't see anything obviously wrong at first glance. Can you verify that the PHP script is being called? What's the status code of the xhr request in the JavaScript?

Cheers,
Josh

--
You received this message because you are subscribed to the Google Groups "jsPsych" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jspsych+u...@googlegroups.com.
To post to this group, send email to jsp...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jspsych/bf7381cc-f61b-4b71-9c9a-b50eef1a0378%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

shivamp...@gmail.com

unread,
Jan 17, 2018, 8:34:30 AM1/17/18
to jsPsych
Hi Josh,

Thanks for your reply! Would I do this using the XMLHttpRequest Object Properties statusText function to print out whether the php is found or not?

Josh de Leeuw

unread,
Jan 17, 2018, 9:20:07 AM1/17/18
to shivamp...@gmail.com, jsPsych
You could attach an event handler to the onreadystatechange and then look at xhr.status. Check out the examples here: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/onreadystatechange


On Wed, Jan 17, 2018 at 8:34 AM <shivamp...@gmail.com> wrote:
Hi Josh,

Thanks for your reply! Would I do this using the XMLHttpRequest Object Properties statusText function to print out whether the php is found or not?

--
You received this message because you are subscribed to the Google Groups "jsPsych" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jspsych+u...@googlegroups.com.
To post to this group, send email to jsp...@googlegroups.com.

shivamp...@gmail.com

unread,
Jan 17, 2018, 4:45:45 PM1/17/18
to jsPsych
Hi Josh,

Thanks for this - I tried the script from the website and got the following error messages:
https://www.dropbox.com/s/jokzusxddnf2qgh/console.png?dl=0

So it looks like the php file is being found and called but nothing is happening?

Josh de Leeuw

unread,
Jan 17, 2018, 4:53:05 PM1/17/18
to shivamp...@gmail.com, jsPsych
I think that's right. PHP is kind of a pain to debug, but you may want to add echo statements or use var_dump, and then look at the responseText of the xhr variable. There may be better ways to debug PHP, but I'm not an expert.


--
You received this message because you are subscribed to the Google Groups "jsPsych" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jspsych+u...@googlegroups.com.
To post to this group, send email to jsp...@googlegroups.com.

Mara Haslam

unread,
Jan 25, 2018, 5:50:21 PM1/25/18
to jsPsych
I have the same problem... xhr doesn't seem to work properly for me.  The jsPsych code runs as if everything's fine, but it doesn't actually write the data.

This version was working properly under jsPsych 5.0.3 but 6.0.1 doesn't like it.  This is really pushing the edges of my knowledge so I'm not sure why:

function saveData(filename, filedata){
   $.ajax({
      type:'post',
      cache: false,
      url: 'write_data.php',
      data: {filename: filename, filedata: filedata}
   });
}

I get this message from the console: Uncaught ReferenceError: $ is not defined

I don't actually know what $ stands for, so I don't know how to fix this problem.  Does anyone have code that works with 6.0.1 and doesn't use xhr?
Mara

Mara Haslam

unread,
Jan 26, 2018, 11:06:43 AM1/26/18
to jsPsych
I figured out that ajax is tied to jQuery so I added jquery at the beginning of the file and this works now.
M

adithyan...@gmail.com

unread,
May 24, 2018, 4:36:30 AM5/24/18
to jsPsych
Hey, did someone figure out how to do this? I have the same problem. The experiment runs but the data file is not created in the directory but it gets displayed at the end of the experiment.

Please do let me know if there's any solution.

Andreas Falck

unread,
May 24, 2018, 8:51:19 AM5/24/18
to jsPsych
It seems that something has changed in how php receives POST:s in recent versions.

For me, adding in the beginning of the php code of write_data.php:

$post_data = json_decode(file_get_contents('php://input'), true);

and then substitute all instances of

$_POST

with

$post_data

in the rest of the code from the tutorial, then it works.

I found the solution somewhere on stackexchange, but I don't really know what has changed. But if this is due to a change in how php works it should perhaps be updated on the jsPsych website.

adithyan...@gmail.com

unread,
May 25, 2018, 3:42:45 AM5/25/18
to jsPsych
Hey there,
Thanks a lot of the reply,
I tried your solution, but it still doesn't work. How do I confirm that the php file is being called?
I tried to use the onreadystatechange() as mentioned in the link above (added it to the saveData function)It returned a 404(not found). I've found out where the error is, since this is what the xhr.responseText returned:
"cannot POST/write_data.php"

Any idea how to solve this?
Thanks in advance

adithyan...@gmail.com

unread,
May 25, 2018, 3:53:30 AM5/25/18
to jsPsych
@Mara Haslam, you mentioned that the Ajax is tied to jQuery, how exactly did you add jquery to the beginning of the file?
Thank you.

Mara Haslam

unread,
May 25, 2018, 8:04:41 AM5/25/18
to adithyan...@gmail.com, jsPsych
I added the following line near the top of the file, together wtih all the plugins:


Mara

--
You received this message because you are subscribed to the Google Groups "jsPsych" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jspsych+unsubscribe@googlegroups.com.

To post to this group, send email to jsp...@googlegroups.com.

Andreas Falck

unread,
May 28, 2018, 10:10:36 AM5/28/18
to adithyan...@gmail.com, jsPsych
Have you checked that any php code is executed? It sounds like an error that could come from php not being functional on the server.

For debugging php previously I used error_log() (https://www.w3schools.com/php/func_error_log.asp), but checking it requires access to the server's log file or similar.

--
You received this message because you are subscribed to the Google Groups "jsPsych" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jspsych+unsubscribe@googlegroups.com.
To post to this group, send email to jsp...@googlegroups.com.

Rodrigo L.

unread,
May 29, 2018, 11:46:45 AM5/29/18
to jsPsych

I had the same problem and solved using the old saving to file method that Mara Haslam is describing. Just add the jquery script and use the $ajax call. That solved it for me.
Reply all
Reply to author
Forward
0 new messages