简体   繁体   中英

How to pass javascript value to controller action?

I've tried different ways to get my selected value from my Category.php file to controller file ( IndexController.php ). For this I have 3 files Category.php as renderer (is a dropdown list I added to a Grid.php in magento)

<?php

class Ns_Thorleif_Block_Adminhtml_Commerciaux_Edit_Form_Renderer_Category extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{   
     public function render(Varien_Object $row)
{

$category = Mage::getModel('thorleif/category')->getCollection();

$category_rows = $category->getData();
$html = '<select id="select_category" name="select_category" onchange="ChooseContact(this)">';
$sep = '';
?>
<input type="text" name="id_cat" id="id_cat" value="" />
<?php

foreach ($category_rows as $rows) {

    // put the correct key here

    if (!$rows['level']) {
        $html .= $sep . "<optgroup label='{$rows['name']}'>";
        $sep = '</optgroup>';
    } else {
        $nbsp = str_repeat('&nbsp;', ($rows['level'] - 1)* 5);
        $html .= "<option value='{$rows['id_linio_category']}' title='{$rows['name']}' selected>
        $nbsp {$rows['name']}</option>";
    }
}
$html .= '</select>';
return $html;
}
}
?>

Grid.php

 public function _prepareColumns()
        {
    $this->addColumn('lin',
                array(
                    'header' => 'Lin Category',
                    'align' => 'left',
                    'index' => 'lin',
                    'filter'    => false,
                    'sortable'  => false,
                    'renderer' => 'Ns_Thorleif_Block_Adminhtml_Commerciaux_Edit_Form_Renderer_Category'
                )
            );
            $this->addColumn('action',
                array(
                    'header'    =>  Mage::helper('customer')->__('Action'),
                    'width'     => '100',
                    'type'      => 'action',
                    'getter'    => 'getId',
                    'actions'   => array(
                        array(
                            'caption'   => Mage::helper('customer')->__('Sync'),
                            'url'       => array('base'=> '*/*/sync'),
                            'field'     => 'id',
                            'target'=>'_blank'
                        )
                    ),
                    'filter'    => false,
                    'sortable'  => false,
                    'index'     => 'stores',
                    'is_system' => true,
            ));
            return parent::_prepareColumns();
}

IndexController.php where I want to get/use the Selected "Javascript_value" below from Category.php

public function syncAction()
    {

    //var_dump($_REQUEST['id_cat']);
    //var_dump($_POST['id_cat']);
    //var_dump($_GET['id_cat']);
        $id = $this->getRequest()->getParam('id');
        $products = Mage::getModel('catalog/category')
                    ->load($id)
                    ->getProductCollection()->getData();

        $send = "<Request>\n";

        Foreach($products as $values){
            $code = Mage::getModel('catalog/product')->load($values['entity_id'])->getData();
            $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($values['entity_id']);


            $send .= "<Product>".$this->getProductXml($code, $commercial, ***JAVASCRIPT_VALUE***, $stock)."</Product>\n";

        }
        $send .="</Request>"; 
        $api = new Ns_Thorleif_Adminhtml_IndexController();
        $response = $api->postProductAction($send);
        echo "<xmp>$send</xmp>";
        echo "<xmp>$response</xmp>";
    }

The 3 files have the result on the same front page Category.php=select dropdownlist, Grid.php= full grid in the front including column with dropdown list, IndexController.php = Function I use to process some data.

Any suggestion please? the Idea I've tried the get/post/request, tried to save data in database and try to get it.

You are just redirecting the control not posting any form. thats why you are not able to get the data. You can do two of the things.

  1. In "ChooseContact" js function, rediect to syncAction with the selected value in get method and remove the action button.

  2. In the action button, instead of redirecting the control you can call a js function and get the dropdown selected value and then redirect. Please refer : Magento adminhtml grid with javascript action column

Hope this will help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM