How to Set Dynamic Value for Session Variable in Magento 1

core-sessiongiftmessagemagento-1

Can any one help me why am not able to set a magento session variable with the following code.

Mage::getSingleton('core/session')->setGiftMessage("<script type='text/javascript'>jQuery('#gift_message').val();</script>"); 

If the above implementation is wrong then please tell me how can i get a value of a text area

<textarea id="gift_message" rows="2" cols="75" name="gift-message"></textarea>

and set that value in a magento session variable to use it later.

Many thanks in advance .

Best Answer

I assume that the value is sent through post as some controller.
Just add this in the controller that handles the request:

$var = Mage::app()->getRequest()->getPost('gift_message'); //$_POST['gift_message']
Mage::getSingleton('core/session')->setGiftMessage($var);

Later you should be able to get it like this:

$val = Mage::getSingleton('core/session')->getGiftMessage()

or if you want to retrieve it and remove it from session use this

$val = Mage::getSingleton('core/session')->getGiftMessage(true)
Related Topic