0

I'm doing some tests in Magento using AJAX and I'm having the following problem: I want to output periodically some content I've been generating on my php script on my actual page.

So when I click on a Button my js function in my page is called:

page.phtml:

function requestLoop(){

var url = '<?php echo $this->getUrl('mymodule/index/readLine') ?>';

new Ajax.PeriodicalUpdater('Output',url,{
    method: 'post',
    insertion: 'after',
    frequency: 3,
    decay: 2
    });

}

where 'output' is the ID of my table where I want to output my content. This AJAX request calls my separate php controller file:

   $private static $i=0;
   public function readLineAction(){

    self::$i+= 2;
    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode(self::$i));
    $this->getResponse()->setHeader('Content-type','application-json');
    }

Now I would expect that my

Table displays the numbers 2 4 6 8 ....
But instead I only get 2 2 2 2 ....

So there must be something wrong with either my Request oder my Variable $i. Can anyone give me an advise?

Thanks in advance

2
  • 1
    You cannot access a static variable from a non static context (your functino readLineAction is not static). That's why it's always 0 and then increased by 2 and your return is also 2 for every request. Is your countdown user/request specific? You could think about storing the value in the users session or send the latest value with your ajax request. Commented Mar 28, 2015 at 14:04
  • @AnnaVölkl/ @user3701098 please put this answer
    – Amit Bera
    Commented Mar 28, 2015 at 15:55

2 Answers 2

1

As mentioned in my comment:

You cannot access a static variable from a non static context (your function readLineAction is not static). That's why it's always 0 and then increased by 2 and your return is also 2 for every request. Is your countdown user/request specific? You could think about storing the value in the users session or send the latest value with your ajax request.

Magento does provides a lot of session handling:

Instead of setting the session varialbe via $_SESSION['testIndex'] = yourvalue; use:

Mage::getSingleton('core/session')->setTestIndex(value);

Instead of retrieving the session variable via $_SESSION['testIndex'] use:

$outputMessage = Mage::getSingleton('core/session')->getTestIndex();

session_start() is not needed in this case.

You can either use core/session or customer/session. 'core' is the bare session with visitor information, 'customer' is related to a specific customer. The cool thing is, that you can rely on the Magento sessions and your values being unset and handled and you don't need to take care of this on your own.

0

Okay So here's my solution: my.phtml:

<?php session_start();?>
.........
<script type="text/javascript">
function requestLoop(){
var url = '<?php echo $this->getUrl('mymodule/index/readLine') ?>';

new Ajax.PeriodicalUpdater('Output',url,{
method: 'post',
insertion: 'after',
frequency: 3,
decay: 2
});
</script>

And on the .php side:

   public function readLineAction(){

    $_SESSION['testIndex'] = $_SESSION['testIndex']+1;
    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($_SESSION['testIndex']));
    $this->getResponse()->setHeader('Content-type','application-json');

}

1
  • Hi, you might use the Magento functions for working with sessions, see my answer. Commented Mar 28, 2015 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.