Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

confusion between form and chtml

i used to form to write form ans submit to database working fine, but for some particular places, according to google search used CHTML instead of form. but when i submitted that form to database

CHTML textfield value is not submitting to database

here goes my code

_Form.php

 <script language="javascript">

function firstlang(flang,slang,tlang,math,scien,soci)
{

var sflang=parseInt(flang)+parseInt(slang)+parseInt(tlang)+parseInt(math)+parseInt(scien)+parseInt(soci);
document.getElementById('totalmarks').value=sflang;
if(sflang>=300 && sflang<400)
{
var flang='C';
document.getElementById('grade').value=flang;
}
else if(sflang>=400 && sflang<500)
{
var flang='B';
document.getElementById('grade').value=flang;   
}
else if(sflang>=550 && sflang<=600)
{
var flang='A';
document.getElementById('grade').value=flang;   
}
}

</script>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'marks-form',
    'enableAjaxValidation'=>true,
     'enableClientValidation'=>true,
      'focus'=>array($model,'class'),
)); ?>
    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>
<table width="200" border="1">
  <tr>
    <td><span class="row"><?php echo $form->labelEx($model,'class'); ?></span></td>
    <td><span class="row">

   <?php echo CHtml::dropDownList('class','',CHtml::listData(class1::model()->findAll(),'class','class'),array('empty'=>'Choose one',

'ajax' => array(

'type'=>'POST', //request type
'url'=>CController::createUrl('Marks/dynamicstates'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#studentid', //selector to update
//'data'=>'js:javascript statement'
//leave out the data key to pass all form values through
)));

//empty since it will be filled by the other dropdown
?></span></td>
    <td><span class="row"><?php echo $form->error($model,'class'); ?></span></td>
  </tr>
  <tr>
    <td><span class="row"><?php echo $form->labelEx($model,'studentid'); ?></span></td>
    <td><span class="row"><?php echo CHtml::dropdownlist('studentid','',array()); ?></span></td>
    <td><span class="row"><?php echo $form->error($model,'studentid'); ?></span></td>
  </tr>
  <tr>
    <td><span class="row"><?php echo $form->labelEx($model,'examtype'); ?></span></td>
    <td><span class="row"><?php echo $form->textField($model,'examtype',array('size'=>30,'maxlength'=>30)); ?></span></td>
    <td><span class="row"><?php echo $form->error($model,'examtype'); ?></span></td>
  </tr>
  <tr>
    <td><span class="row"><?php echo $form->labelEx($model,'firsttlanguage'); ?></span></td>
    <td><span class="row"><?php echo $form->textField($model,'firsttlanguage',array('id'=>'firsttlanguage','value'=>'0','onkeyup'=>'firstlang(this.value,secondlanguage.value,thirdlanguage.value,mathematics.value,science.value,social.value)')); ?></span></td>
    <td><span class="row"><?php echo $form->error($model,'firsttlanguage'); ?></span></td>
  </tr>
  <tr>
    <td><span class="row"><?php echo $form->labelEx($model,'secondlanguage'); ?></span></td>
    <td><span class="row"><?php echo $form->textField($model,'secondlanguage',array('id'=>'secondlanguage','value'=>'0','onkeyup'=>'firstlang(this.value,firsttlanguage.value,thirdlanguage.value,mathematics.value,science.value,social.value)')); ?></span></td>
    <td><span class="row"><?php echo $form->error($model,'secondlanguage'); ?></span></td>
  </tr>
  <tr>
    <td><span class="row"><?php echo $form->labelEx($model,'thirdlanguage'); ?></span></td>
    <td><span class="row"><?php echo $form->textField($model,'thirdlanguage',array('id'=>'thirdlanguage','value'=>'0','onkeyup'=>'firstlang(this.value,firsttlanguage.value,secondlanguage.value,mathematics.value,science.value,social.value)')); ?></span></td>
    <td><span class="row"><?php echo $form->error($model,'thirdlanguage'); ?></span></td>
  </tr>
  <tr>
    <td><span class="row"><?php echo $form->labelEx($model,'mathematics'); ?></span></td>
    <td><span class="row"><?php echo $form->textField($model,'mathematics',array('id'=>'mathematics','value'=>'0','onkeyup'=>'firstlang(this.value,firsttlanguage.value,secondlanguage.value,thirdlanguage.value,science.value,social.value)')); ?></span></td>
    <td><span class="row"><?php echo $form->error($model,'mathematics'); ?></span></td>
  </tr>
  <tr>
    <td><span class="row"><?php echo $form->labelEx($model,'science'); ?></span></td>
    <td><span class="row"><?php echo $form->textField($model,'science',array('id'=>'science','value'=>'0','onkeyup'=>'firstlang(this.value,firsttlanguage.value,secondlanguage.value,thirdlanguage.value,mathematics.value,social.value)')); ?></span></td>
    <td><span class="row"><?php echo $form->error($model,'science'); ?></span></td>
  </tr>
  <tr>
    <td><span class="row"><?php echo $form->labelEx($model,'social'); ?></span></td>
    <td><span class="row"><?php echo $form->textField($model,'social',array('id'=>'social','value'=>'0','onkeyup'=>'firstlang(this.value,firsttlanguage.value,secondlanguage.value,thirdlanguage.value,mathematics.value,science.value)')); ?></span></td>
    <td><span class="row"><?php echo $form->error($model,'social'); ?></span></td>
  </tr>
  <tr>
    <td><span class="row"><?php echo $form->labelEx($model,'totalmarks'); ?></span></td>
    <td><span class="row"><?php echo $form->textField($model,'totalmarks',array('id'=>'totalmarks','size'=>5,'maxlength'=>5)); ?></span></td>
    <td><span class="row"><?php echo $form->error($model,'totalmarks'); ?></span></td>
  </tr>
  <tr>
    <td><span class="row"><?php echo $form->labelEx($model,'grade'); ?></span></td>
    <td><span class="row"><?php echo $form->textField($model,'grade',array('id'=>'grade','size'=>5,'maxlength'=>5)); ?></span></td>
    <td><span class="row"><?php echo $form->error($model,'grade'); ?></span></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><span class="row buttons"><?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?></span></td>
    <td>&nbsp;</td>
  </tr>
</table>





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

</div><!-- form -->

MarksController.php

<?php

class MarksController extends Controller
{
    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    public $layout='//layouts/column2';

    /**
     * @return array action filters
     */
    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules()
    {
        return array(
            array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions'=>array('index','view'),
                'users'=>array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array('create','update'),
                'users'=>array('@'),
            ),
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
                'actions'=>array('admin','delete'),
                'users'=>array('admin'),
            ),
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
                'actions'=>array('admin','dynamicstates'),
                'users'=>array('admin'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

    /**
     * Displays a particular model.
     * @param integer $id the ID of the model to be displayed
     */
    public function actionView($id)
    {
        $this->render('view',array(
            'model'=>$this->loadModel($id),
        ));
    }

    /**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate()
    {
        $model=new Marks;

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Marks']))
        {
            $model->attributes=$_POST['Marks'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('create',array(
            'model'=>$model,
        ));
    }

    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id the ID of the model to be updated
     */
    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Marks']))
        {
            $model->attributes=$_POST['Marks'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('update',array(
            'model'=>$model,
        ));
    }

    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'admin' page.
     * @param integer $id the ID of the model to be deleted
     */
    public function actionDelete($id)
    {
        if(Yii::app()->request->isPostRequest)
        {
            // we only allow deletion via POST request
            $this->loadModel($id)->delete();

            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if(!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array(

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
133 views
Welcome To Ask or Share your Answers For Others

1 Answer

Have a look here Creating Form. Since Yii 1.1.1 there is a widget called CActiveForm so when you use this widget you create the form inputs with $form->textField($model,'username')instead of using CHtml::activeTextField($model,'username')but both work in the same way.

So if that field is not being saved in the database it's probably because you haven't added it to the rules of your model.

public function rules() {
    return array(
        array('username', 'safe'),
    );
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...