I've written an experiment that is pretty satisfactory until the point of writing the data to a local server (the mySQL preference panel indicates that the server is running) and I've prepared a database and a table with the headers needed to write jsPsych data into.
In the same folder as my experiment, I also have i) database_connect.php and ii) savedata.php, and a save_data function defined in the js section.
I'm trying to write data to a mySQL database on a local server, before moving on to an actual university server that runs php. I believe I've followed the instructions to a T, but I am unclear how to catch the error. The code below runs all the way to save the data locally (as a .csv file download), so I believe the 'save_data' function is being evaluated; alas, I have no indication at *which* point the mySQL connection is failing. Any help would be much appreciated!
Below is my js snippet:
/* save data code */
function save_data(data){
var data_table = "peacock"; // change this for different experiments
$.ajax({
type:'post',
cache: false,
url: 'savedata.php', // change this to point to your php file.
data: {
table: data_table,
json: JSON.stringify(data),
},
success: function(output) { console.log(output); } // write the result to javascript console
});
}
/* start the experiment */
jsPsych.init({
timeline: timeline,
on_finish: function(data) {
var alldata = jsPsych.data.getData();
jsPsych.data.displayData('CSV');
save_data(alldata);
jsPsych.data.localSave('mydata.csv', 'csv');
}
--
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/91f21573-aabf-4e7c-9d4f-11d8143c7a1c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
function save_data(data){
var data_table = "rhythm_data"; // change this for different experiments
$.ajax({
type:'post',
cache: false,
url: 'http://research.petervavra.com/rhythm/save_data.php', // change this to point to your php file.
data: {
table: research_rhythm_data,
json: JSON.stringify(data),
},
success: function(output) { console.log(output); } // write the result to javascript console
});
}<?php
// the $_POST[] array will contain the passed in filename and data
// the directory "data" is writable by the server (chmod 777)
$filename = "data/".$_POST['filename'];
$data = $_POST['filedata'];
// write the file to disk
file_put_contents($filename, $data);
?><?php
// Submit Data to mySQL database
// Josh de Leeuw
// Edit this line to include your database connection script
//
// The script you link should contain the following two lines:
//
// $dbc = mysql_connect('localhost', 'username', 'password');
// mysql_select_db('databasename', $dbc);
//
include('http://research.petervavra.com/rhythm/database_connect.php');
// You should not need to edit below this line
function mysql_insert($table, $inserts) {
$values = array_map('mysql_real_escape_string', array_values($inserts));
$keys = array_keys($inserts);
return mysql_query('INSERT INTO `'.$table.'` (`'.implode('`,`', $keys).'`) VALUES (\''.implode('\',\'', $values).'\')');
}
// get the table name
$tab = $_POST['table'];
// decode the data object from json
$trials = json_decode($_POST['json']);
// get the optional data (decode as array)
$opt_data = json_decode($_POST['opt_data'], true);
$opt_data_names = array_keys($opt_data);
var_dump($trials);
// for each element in the trials array, insert the row into the mysql table
for($i=0;$i<count($trials);$i++)
{
$to_insert = (array)($trials[$i]);
// add any optional, static parameters that got passed in (like subject id or condition)
for($j=0;$j<count($opt_data_names);$j++){
$to_insert[$opt_data_names[$j]] = $opt_data[$opt_data_names[$j]];
}
$result = mysql_insert($tab, $to_insert);
}
// confirm the results
if (!$result) {
die('Invalid query: ' . mysql_error());
} else {
print "successful insert!";
}
?>To view this discussion on the web visit https://groups.google.com/d/msgid/jspsych/39ba4edd-6f7c-4792-964a-06f851743abb%40googlegroups.com.