Magento – how to pass a variable from one phtml to another

blocksmagento2parameterphtmlvariables

I have two phtml which have its own unique block file. I need to pass variable from one phtml to another.

PHTML 1

<?php 
$option_id = 20;
?>

<a href="<?php echo $baseUrl ?>/brandsmodules/index/demo" ></a>

PHTML 2

In this page is where I need to get the variable. I don't want to pass variables in URL as parameter due to security concerns.

Best Answer

You can send variable in URL like this to pass variable in another .phtml

In your PHTML 1

<?php $option_id = 20; ?>

<a href="<?php echo $this->getUrl('brandsmodules/index/demo', ['optionId' => $option_id]) ?>

In your PHTML 2

And you will get your optionId in another phtml by using $_GET['optionId'].

Please check and let me know if any question.

Related Topic