Passing Values from Storyline to PHP via Javascript

It has been an awesome week, since my last update. I did some tinkering on excluding the FLASH, as well as modifying the storage format part. Today, we shall discuss the use Javascript to call PHP and store data in .txt file and .csv file, without the use of FLASH.

To start with, let us assume that we need to store data from a Feedback Form in Storyline.

The variables created in the Storyline are: ‘name’, ‘rating’ and ‘comment’.


Step 1: Create a trigger to execute a javascript when submit button is clicked

Enter the Javascript:

—————————————————————————

var player = GetPlayer();

var name = player.GetVar(“name”)

var rating = player.GetVar(“rating”)

var comment = player.GetVar(“comment”)

window.location.href = “http://yoururl.com/feedback/main.php?w1=” + name + “&w2=” + rating + “&w3=” + comment;

 

——————————————————————————————

Explanation of the last line:

The last line redirects the current window to the location where the PHP is kept (http://yoururl.com/feedback/) and pushes the values of name, rating and comment as w1, w2 and w3 respectively into the PHP.

In case, you need to push another variable (say w4 – for email): just add
+ “&w4=” + email
before semicolon.

——————————————————————————————

 

Step 2: Create the PHP (main.php) at the url given (http://yoururl.com/feedback/)

Enter the code:

——————————————————————————————

For saving data in a .txt file:

<?php

$message = “n” .$_GET[‘w1’]. ” “.$_GET[‘w2’] . ” “.$_GET[‘w3’]. “n”;

$File = “data.txt”;

$Handle = fopen($File, ‘a+’);

fwrite($Handle, $message);

print “Data Written”;

fclose($Handle);

?>

 

Also create data.txt file and save it at the location of main.php.

——————————————————————————————

For saving data in a .csv file:

<?php

$cvsData .= $_GET[‘w1’].”,”.$_GET[‘w2’].”,”.$_GET[‘w3’].”,”.PHP_EOL; // Format to concatenate the data in one string.

$fp = fopen(“data.csv”, “a”);

 

if($fp)

{

fwrite($fp,$cvsData); // Write information to the file

fclose($fp); // Close the file

}

?>

Also create data.csv file and save it at the location of main.php.

——————————————————————————————

Now publish the .story file and upload it. This time*, there is no need to keep the data file (.txt/.csv) in the same folder where story.html is kept.

You can find the source files here.

  • From Colombia

    Really good reference and guidance. It´s maybe less complicated as seen. I do the storyline product and obtain every response of 17 questions on a csv file. Thanks.

  • David Boyle

    Interesting post. Please advise if the output to CSV creates a new entry for each user session and whether multiple attempts would override previous attempt data.

    • kawstov

      Dear David,

      The answer to this question lies in the PHP line:
      $Handle = fopen($File, 'a+');

      It adds new content as new line, to the last written content.

      Regards,
      Kawstov

      • kawstov

        PS: Multiple attempts will not override previous data