Search code examples
javascriptphpsessionkineticjs

Create PHP session variable inside Kinetic.js/Javascript


I am trying to create a PHP session variable inside of Kinetic.js/Javascript.

stage.on('mousedown', function(evt) {
  var shape = evt.targetNode;
  if (shape) {
    if (shape.getFill() == 'green') { 
      //$_SESSION['room_name'] = shape.getAttr('key');
      window.location.href = 'create.php';
    }       
  }
});

When my shape is clicked, I want the session variable to store the 'key' attribute, which is a string, then go to 'create.php'.

How can I do this?

EDIT: Cerbrus, I tried you suggestion:

stage.on('mousedown', function(evt) {
  var shape = evt.targetNode;
  if (shape) {
    if (shape.getFill() == 'green') { 
      var img = new Image();
      img.src = "script.php?roomName=" + encodeURIComponent('hello');
      window.location.href = 'create.php';
    }       
  }
});

The user is sent to 'create.php', but when i tried to print it in 'create.php':

<?php
  echo $_GET['roomName'];
?> 

Nothing is echoed


Solution

  • You have to set roomName parameter in url so it can be read by your php script.

    window.location.href = 'create.php?roomName='+ encodeURIComponent('hello');
    

    You can do the same to send the id.

    window.location.href = 'create.php?roomName='+shape.getAttr('key');