How To Set Default Value From Database Into Hiddenfield?

firstly, sorry for my bad english and sorry if there are any questions like this one in the forum before. but I already search for it and found nothing helpful :(

well, in this case I’m using datapicker to pick some data, and put it into the form before it will be saved. the pop up datapicker dialog will gives us the list of the items that we choose, it’s include item name, blank textField for qty, blank textField for price and delete button to remove the choosen item. but I also need to put the ID of the choosen item for the benefit of the purchasing data, using a hiddenField. the question is how can I set this item ID value from database to the hiddenField?

thanks in advance ::)

Assuming that you’re using AR, you could use:




<?= CHtml::activeHiddenField($model, 'id'); ?>



Or the equivalent CActiveForm->hiddenField() or CHtml::hiddenField() options.

ah, finally an answer :D thanks in advance, I’m use that CHtml::hiddenField option. but I’m not sure if this is right or not. since I put this code in the picker view, this is my code :


 $(\"#hasil tr:last\").after(\"<tr><td>'.str_replace('"', "'",CHtml::hiddenField('Pembelian[id_item]', $data->id_item)).'$data->nama_item</td><td>'.str_replace('"', "'",CHtml::textField('Pembelian[qty]', '',array('size'=>5,'maxlength'=>5))).'</td><td>'.str_replace('"', "'",CHtml::textField('Pembelian[harga]', '',array('size'=>15,'maxlength'=>15))).'</td><td>'.str_replace('"', "'", CHtml::button('Delete', array('onClick'=>'$(this).parent().parent().remove();'))).'</td></tr>\");

all of that script just doing fine except for this hiddenField :(

That code hurts my brain. :D

Can you post your surrounding code? It might be possible to rewrite this in a clearer format, which will help us to figure out the problem. I suspect that you can break the code out into a separate javascript function.

haha, sorry for that. sure, this is my whole code from the picker.php view:


<?php

$this->beginWidget('zii.widgets.jui.CJuiDialog',

        array('id' => 'item_dialog',

            'options' => array(

                'title' => 'Daftar Item',

                'width' => 'auto',

                'autoOpen' => false,

            ),

));




$this->widget('zii.widgets.grid.CGridView',

        array('id' => 'item-grid',

            'dataProvider' => $item->search(),

            'filter' => $item,

            'ajaxUrl'=> array('item/picker2'),

            'columns' => array(

                'id_item',

                'nama_item',

                array(

                    'header' => '',

                    'type' => 'raw',

                    'value' => 'CHtml::Button("+",

                                              array("name" => "send_item",

                                                     "id" => "send_item",

                                                     "onClick" => "$(\"#item_dialog\").dialog(\"close\"); 

                                                     $(\"#hasil tr:last\").after(\"<tr><td>'.str_replace('"', "'",CHtml::hiddenField('Pembelian[id_item]', $data->id_item)).'$data->nama_item</td><td>'.str_replace('"', "'",CHtml::textField('Pembelian[qty]', '',array('size'=>5,'maxlength'=>5))).'</td><td>'.str_replace('"', "'",CHtml::textField('Pembelian[harga]', '',array('size'=>15,'maxlength'=>15))).'</td><td>'.str_replace('"', "'", CHtml::button('Delete', array('onClick'=>'$(this).parent().parent().remove();'))).'</td></tr>\");                                       

                                                    "))',

                ),

            ),

));


$this->endWidget('zii.widgets.jui.CJuiDialog');

echo CHtml::Button('Add Item',

        array('onclick' => '$("#item_dialog").dialog("open"); return false;',

));

?>



sorry, if it’s hard to understand and to read it. especially the long one, it’s because when I press enter to make it easier to read, it won’t run. so, I just kept it like that. anyway, thanks for your help :)

I think I just made it correct now. It’s working now :D I change this code :


$(\"#hasil tr:last\").after(\"<tr><td>'.str_replace('"', "'",CHtml::hiddenField('Pembelian[id_item]', $data->id_item)).'$data->nama_item</td><td>'.str_replace('"', "'",CHtml::textField('Pembelian[qty]', '',array('size'=>5,'maxlength'=>5))).'</td><td>'.str_replace('"', "'",CHtml::textField('Pembelian[harga]', '',array('size'=>15,'maxlength'=>15))).'</td><td>'.str_replace('"', "'", CHtml::button('Delete', array('onClick'=>'$(this).parent().parent().remove();'))).'</td></tr>\");

into


$(\"#hasil tr:last\").after(\"<tr><td>$data->nama_item</td><td>'.str_replace('"', "'",CHtml::textField('Pembelian[qty]', '',array('size'=>5,'maxlength'=>5))).'</td><td>'.str_replace('"', "'",CHtml::textField('Pembelian[harga]', '',array('size'=>15,'maxlength'=>15))).'</td><td>'.str_replace('"', "'", CHtml::button('Delete', array('onClick'=>'$(this).parent().parent().remove();'))).'</td>".str_replace(\'"\', "\'",CHtml::hiddenField(\'Pembelian[id_item]\', $data->id_item))."</tr>\");

I think it’s just about the placement of the hiddenField and how we write it.