I have a textfield in yii, and want to insert data from database upon that page load. How can I achieve this?

I am able to successfully echo that database value, but not knowing how to insert it.

Lets say the textfield name is 'text', and stored value from db is 'val'.

 $this->text = $val //I tried this way 
有帮助吗?

解决方案

Try like this normal way

 CHtml::fileField('name' , 'value', array('id' => 'MuID','class'=>'MyClass'));

If you sending the model data to the View, you can use CActiveForm (check the basic CRUD operations in YII, Generate from GII)

Example: i have fetched user data which has id 0001 and i sent the model to the view

My View

   <?php
   $form = $this->beginWidget('CActiveForm', array(
    'id' => 'my-form',
    'enableAjaxValidation' => true,
    'enableClientValidation' => true,
    'clientOptions' => array(
        'hideErrorMessage' => true,
        'validateOnSubmit' => true,
       )
    ));
   ?>    

   //It will show user name in textbox
   <?php echo $form->fileField($model, 'user_name'); ?> 

  <?php $this->endWidget(); ?>

其他提示

The db value is probably in a model right? You can use activeTextField to display database values in a textfield

Take a look at:

http://www.yiiframework.com/doc/api/1.1/CHtml#activeTextField-detail

It will probably look like this:

<?php
    // simple textfield which will display the value
    echo CHtml::activeTextField($yourmodel, 'field_that_contains_value');
    // or if you're using a form:
    echo $form->textField($yourmodel, 'field_that_contains_value');
    // or if the value isn't in a model
    echo CHtml::textField('name_of_field', $value);
?>
<input type="text" name="input_name" value="<?=$val?>"/>

Using CHtml helper:

echo CHtml::textField('name',$val);

Using ActiveForm

echo $form->activeTextField($model, 'attribute', array('value'=>$val));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top