I am able from call block function using php code from phtml like below :

  $blockObj= $block->getLayout()->createBlock('Inchoo\Helloworld\Block\Helloworld');
  $myArr = $blockObj-> getTag(); 
  echo $myArr; 

But now I got a onclick function from a checkbox as below

 <script>function functionSubCatCB(cb){ 
    var favorite = [];
            jQuery.each(jQuery(".SubCatCB:checked"), function(){    
                //alert("Hi Checked");
            var valCB =favorite.join("', '");


            //var blk = "$blockObj= $block->getLayout()->createBlock('Inchoo\Helloworld\Block\Helloworld');$myArr = $blockObj-> getTag(".valCB."); echo $myArr; ?>";
            //jQuery("#ShortDescription").val(<? php blk);}

Now I need to pass the value of valCB to the function getTag

I cannot do like $block->getTag(valCB);

Do I have any other option to do this ???


Assuming that $myArr returns this: '1' => 'val1','2' => 'val2','3' => 'val3'


<?php $myArr = array('1' => 'val1','2' => 'val2','3' => 'val3'); ?>
<?php $code_array = json_encode($myArr); ?>

    var jarray = <?php echo $code_array; ?>;
    $.each(jarray , function(index, val) {
        alert(val) // val1, val2, val3
    console.log(arraycode); //Object { 1: "val1", 2: "val2", 3: "val3" }
  • Thank you for your valuable information, But can you tell me how to pass a value from <script> block to <php>, In the above example you have show beautifully how to pass the value from <php> to <script>, but I need it to be other way around, can you help in that ? – neelav barai Jun 5 '18 at 19:49
  • You need to do that with AJAX, javascript is a client side, php is in server side – PЯINCƏ Jun 6 '18 at 8:30
  • Hi @Prince, I figured it out that i will require AJAX, Can you point me towards any good examples on how to do it or at least the process. It will be of great help. – neelav barai Jun 6 '18 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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