How to fill a textbox with a database value in Yii?

I want to fill one of the text fields with a value from the database.

Usually this code is recommended:

<div class="row">
  <?php echo $form->labelEx($model,'field1'); ?>
  <?php echo Model1::model()->FindByPk($model->id)->field1;?>
  <?php echo $form->error($model,'field1'); ?>
</div>

      

But the one I'm looking for is this value is populated with a textbox on the form.

Can anyone help me? Thank..

+3


source to share


2 answers


Try the following:



<?php echo Chtml::textField('txtFieldName', Model1::model()->FindByPk($model->id)->field1); ?>

      

+1


source


Have you looked at the Yii Blog Demo ? It is also available in a download package.

It is usually recommended:

<?php
echo $form->labelEx($model, 'field', 'label text');
echo $form->textField($model, 'field');
echo $form->error($model, 'field');
?>

      



If the model contains data from the database, it will be displayed when the view is loaded. Your code in a controller action might look like this:

// ...
$model = Model1::model()->findByPk($id); // $id has to be the primary key of the model you want to load
$this->render('viewfile', array(
    'model'=>$model, // this $model is then the same $model as in the view
));

      

+1


source







All Articles