title: what is the difference between form and chtml in yii
body: <p>confusion between form and chtml</p>
  
  <p>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 </p>
  
  <p>CHTML textfield value is not submitting to database</p>
  
  <p>here goes my code</p>
  
  <p><strong>_Form.php</strong></p>
  
  <pre><code> &lt;script language="javascript"&gt;
  
  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&gt;=300 &amp;&amp; sflang&lt;400)
  {
  var flang='C';
  document.getElementById('grade').value=flang;
  }
  else if(sflang&gt;=400 &amp;&amp; sflang&lt;500)
  {
  var flang='B';
  document.getElementById('grade').value=flang;   
  }
  else if(sflang&gt;=550 &amp;&amp; sflang&lt;=600)
  {
  var flang='A';
  document.getElementById('grade').value=flang;   
  }
  }
  
  &lt;/script&gt;
  
  &lt;div class="form"&gt;
  
  &lt;?php $form=$this-&gt;beginWidget('CActiveForm', array(
      'id'=&gt;'marks-form',
      'enableAjaxValidation'=&gt;true,
       'enableClientValidation'=&gt;true,
        'focus'=&gt;array($model,'class'),
  )); ?&gt;
      &lt;p class="note"&gt;Fields with &lt;span class="required"&gt;*&lt;/span&gt; are required.&lt;/p&gt;
  
      &lt;?php echo $form-&gt;errorSummary($model); ?&gt;
  &lt;table width="200" border="1"&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;labelEx($model,'class'); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;
  
     &lt;?php echo CHtml::dropDownList('class','',CHtml::listData(class1::model()-&gt;findAll(),'class','class'),array('empty'=&gt;'Choose one',
  
  'ajax' =&gt; array(
  
  'type'=&gt;'POST', //request type
  'url'=&gt;CController::createUrl('Marks/dynamicstates'), //url to call.
  //Style: CController::createUrl('currentController/methodToCall')
  'update'=&gt;'#studentid', //selector to update
  //'data'=&gt;'js:javascript statement'
  //leave out the data key to pass all form values through
  )));
  
  //empty since it will be filled by the other dropdown
  ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;error($model,'class'); ?&gt;&lt;/span&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;labelEx($model,'studentid'); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo CHtml::dropdownlist('studentid','',array()); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;error($model,'studentid'); ?&gt;&lt;/span&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;labelEx($model,'examtype'); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;textField($model,'examtype',array('size'=&gt;30,'maxlength'=&gt;30)); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;error($model,'examtype'); ?&gt;&lt;/span&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;labelEx($model,'firsttlanguage'); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;textField($model,'firsttlanguage',array('id'=&gt;'firsttlanguage','value'=&gt;'0','onkeyup'=&gt;'firstlang(this.value,secondlanguage.value,thirdlanguage.value,mathematics.value,science.value,social.value)')); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;error($model,'firsttlanguage'); ?&gt;&lt;/span&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;labelEx($model,'secondlanguage'); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;textField($model,'secondlanguage',array('id'=&gt;'secondlanguage','value'=&gt;'0','onkeyup'=&gt;'firstlang(this.value,firsttlanguage.value,thirdlanguage.value,mathematics.value,science.value,social.value)')); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;error($model,'secondlanguage'); ?&gt;&lt;/span&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;labelEx($model,'thirdlanguage'); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;textField($model,'thirdlanguage',array('id'=&gt;'thirdlanguage','value'=&gt;'0','onkeyup'=&gt;'firstlang(this.value,firsttlanguage.value,secondlanguage.value,mathematics.value,science.value,social.value)')); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;error($model,'thirdlanguage'); ?&gt;&lt;/span&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;labelEx($model,'mathematics'); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;textField($model,'mathematics',array('id'=&gt;'mathematics','value'=&gt;'0','onkeyup'=&gt;'firstlang(this.value,firsttlanguage.value,secondlanguage.value,thirdlanguage.value,science.value,social.value)')); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;error($model,'mathematics'); ?&gt;&lt;/span&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;labelEx($model,'science'); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;textField($model,'science',array('id'=&gt;'science','value'=&gt;'0','onkeyup'=&gt;'firstlang(this.value,firsttlanguage.value,secondlanguage.value,thirdlanguage.value,mathematics.value,social.value)')); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;error($model,'science'); ?&gt;&lt;/span&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;labelEx($model,'social'); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;textField($model,'social',array('id'=&gt;'social','value'=&gt;'0','onkeyup'=&gt;'firstlang(this.value,firsttlanguage.value,secondlanguage.value,thirdlanguage.value,mathematics.value,science.value)')); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;error($model,'social'); ?&gt;&lt;/span&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;labelEx($model,'totalmarks'); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;textField($model,'totalmarks',array('id'=&gt;'totalmarks','size'=&gt;5,'maxlength'=&gt;5)); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;error($model,'totalmarks'); ?&gt;&lt;/span&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;labelEx($model,'grade'); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;textField($model,'grade',array('id'=&gt;'grade','size'=&gt;5,'maxlength'=&gt;5)); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row"&gt;&lt;?php echo $form-&gt;error($model,'grade'); ?&gt;&lt;/span&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&amp;nbsp;&lt;/td&gt;
      &lt;td&gt;&lt;span class="row buttons"&gt;&lt;?php echo CHtml::submitButton($model-&gt;isNewRecord ? 'Create' : 'Save'); ?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
  
  
  
  
  
  &lt;?php $this-&gt;endWidget(); ?&gt;
  
  &lt;/div&gt;&lt;!-- form --&gt;
  </code></pre>
  
  <p><strong>MarksController.php</strong></p>
  
  <pre><code>&lt;?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'=&gt;array('index','view'),
                  'users'=&gt;array('*'),
              ),
              array('allow', // allow authenticated user to perform 'create' and 'update' actions
                  'actions'=&gt;array('create','update'),
                  'users'=&gt;array('@'),
              ),
              array('allow', // allow admin user to perform 'admin' and 'delete' actions
                  'actions'=&gt;array('admin','delete'),
                  'users'=&gt;array('admin'),
              ),
              array('allow', // allow admin user to perform 'admin' and 'delete' actions
                  'actions'=&gt;array('admin','dynamicstates'),
                  'users'=&gt;array('admin'),
              ),
              array('deny',  // deny all users
                  'users'=&gt;array('*'),
              ),
          );
      }
  
      /**
       * Displays a particular model.
       * @param integer $id the ID of the model to be displayed
       */
      public function actionView($id)
      {
          $this-&gt;render('view',array(
              'model'=&gt;$this-&gt;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-&gt;performAjaxValidation($model);
  
          if(isset($_POST['Marks']))
          {
              $model-&gt;attributes=$_POST['Marks'];
              if($model-&gt;save())
                  $this-&gt;redirect(array('view','id'=&gt;$model-&gt;id));
          }
  
          $this-&gt;render('create',array(
              'model'=&gt;$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-&gt;loadModel($id);
  
          // Uncomment the following line if AJAX validation is needed
          // $this-&gt;performAjaxValidation($model);
  
          if(isset($_POST['Marks']))
          {
              $model-&gt;attributes=$_POST['Marks'];
              if($model-&gt;save())
                  $this-&gt;redirect(array('view','id'=&gt;$model-&gt;id));
          }
  
          $this-&gt;render('update',array(
              'model'=&gt;$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()-&gt;request-&gt;isPostRequest)
          {
              // we only allow deletion via POST request
              $this-&gt;loadModel($id)-&gt;delete();
  
              // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
              if(!isset($_GET['ajax']))
                  $this-&gt;redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
          }
          else
              throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
      }
  
      /**
       * Lists all models.
       */
      public function actionIndex()
      {
          $dataProvider=new CActiveDataProvider('Marks');
          $this-&gt;render('index',array(
              'dataProvider'=&gt;$dataProvider,
          ));
      }
  
      /**
       * Manages all models.
       */
      public function actionAdmin()
      {
          $model=new Marks('search');
          $model-&gt;unsetAttributes();  // clear any default values
          if(isset($_GET['Marks']))
              $model-&gt;attributes=$_GET['Marks'];
  
          $this-&gt;render('admin',array(
              'model'=&gt;$model,
          ));
      }
  
      /**
       * Returns the data model based on the primary key given in the GET variable.
       * If the data model is not found, an HTTP exception will be raised.
       * @param integer the ID of the model to be loaded
       */
      public function loadModel($id)
      {
          $model=Marks::model()-&gt;findByPk($id);
          if($model===null)
              throw new CHttpException(404,'The requested page does not exist.');
          return $model;
      }
  
      /**
       * Performs the AJAX validation.
       * @param CModel the model to be validated
       */
      protected function performAjaxValidation($model)
      {
          if(isset($_POST['ajax']) &amp;&amp; $_POST['ajax']==='marks-form')
          {
              echo CActiveForm::validate($model);
              Yii::app()-&gt;end();
          }
      }
  public function actiondynamicstates()
  {
  
        // $model1=new Examresults;
          /*$exam_type=(int)$_POST['exam_type'];
  
  
          if ($exam_type==0)
                  $data=array(0=&gt;'States:');
          else if ($exam_type==1)
                  $data=array(1=&gt;'Alaska', 2=&gt;'California');    
          else if ($exam_type==2)
                  $data=array(1=&gt;'Orleans', 2=&gt;'Bordeaux');
          else if ($exam_type==3)
                  $data=array(1=&gt;'Hokkaido', 2=&gt;'Okinawa');*/
  //$schoolfee = feesettings::model()-&gt;find("feetype='schoolfee' and class='".$_REQUEST['class']."'");
  
   $data=admission::model()-&gt;findAll('class=:class',
                    array(':class'=&gt;$_POST['class']));
  
  
     $data=CHtml::listData($data,'studentid','studentfname');
  
          foreach($data as $value=&gt;$name)
                  echo CHtml::tag('option', array('value'=&gt;$value), CHtml::encode($name), true);
  
  }
  
  }
  </code></pre>
  
  <p><strong>Marks.php</strong></p>
  
  <pre><code>&lt;?php
  
  /**
   * This is the model class for table "marks".
   *
   * The followings are the available columns in table 'marks':
   * @property integer $id
   * @property string $class
   * @property string $studentid
   * @property string $examtype
   * @property integer $firsttlanguage
   * @property integer $secondlanguage
   * @property integer $thirdlanguage
   * @property integer $mathematics
   * @property integer $science
   * @property integer $social
   * @property integer $totalmarks
   * @property string $grade
   */
  class Marks extends CActiveRecord
  {
      /**
       * Returns the static model of the specified AR class.
       * @param string $className active record class name.
       * @return Marks the static model class
       */
      public static function model($className=__CLASS__)
      {
          return parent::model($className);
      }
  
      /**
       * @return string the associated database table name
       */
      public function tableName()
      {
          return 'marks';
      }
  
      /**
       * @return array validation rules for model attributes.
       */
      public function rules()
      {
          // NOTE: you should only define rules for those attributes that
          // will receive user inputs.
          return array(
              array('class, studentid', 'required'),
              array('firsttlanguage, secondlanguage, thirdlanguage, mathematics, science, social, totalmarks', 'numerical', 'integerOnly'=&gt;true),
              array('class, studentid, examtype', 'length', 'max'=&gt;30),
              array('grade', 'length', 'max'=&gt;5),
              // The following rule is used by search().
              // Please remove those attributes that should not be searched.
              array('id, class, studentid, examtype, firsttlanguage, secondlanguage, thirdlanguage, mathematics, science, social, totalmarks, grade', 'safe', 'on'=&gt;'search'),
          );
      }
  
      /**
       * @return array relational rules.
       */
      public function relations()
      {
          // NOTE: you may need to adjust the relation name and the related
          // class name for the relations automatically generated below.
          return array(
          );
      }
  
      /**
       * @return array customized attribute labels (name=&gt;label)
       */
      public function attributeLabels()
      {
          return array(
              'id' =&gt; 'ID',
              'class' =&gt; 'Class',
              'studentid' =&gt; 'Studentid',
              'examtype' =&gt; 'Examtype',
              'firsttlanguage' =&gt; 'Firsttlanguage',
              'secondlanguage' =&gt; 'Secondlanguage',
              'thirdlanguage' =&gt; 'Thirdlanguage',
              'mathematics' =&gt; 'Mathematics',
              'science' =&gt; 'Science',
              'social' =&gt; 'Social',
              'totalmarks' =&gt; 'Totalmarks',
              'grade' =&gt; 'Grade',
          );
      }
  
      /**
       * Retrieves a list of models based on the current search/filter conditions.
       * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
       */
      public function search()
      {
          // Warning: Please modify the following code to remove attributes that
          // should not be searched.
  
          $criteria=new CDbCriteria;
  
          $criteria-&gt;compare('id',$this-&gt;id);
          $criteria-&gt;compare('class',$this-&gt;class,true);
          $criteria-&gt;compare('studentid',$this-&gt;studentid,true);
          $criteria-&gt;compare('examtype',$this-&gt;examtype,true);
          $criteria-&gt;compare('firsttlanguage',$this-&gt;firsttlanguage);
          $criteria-&gt;compare('secondlanguage',$this-&gt;secondlanguage);
          $criteria-&gt;compare('thirdlanguage',$this-&gt;thirdlanguage);
          $criteria-&gt;compare('mathematics',$this-&gt;mathematics);
          $criteria-&gt;compare('science',$this-&gt;science);
          $criteria-&gt;compare('social',$this-&gt;social);
          $criteria-&gt;compare('totalmarks',$this-&gt;totalmarks);
          $criteria-&gt;compare('grade',$this-&gt;grade,true);
  
          return new CActiveDataProvider($this, array(
              'criteria'=&gt;$criteria,
          ));
      }
  }
  </code></pre>