2012-06-12 3 views
-1

путаницы между формой и CHTMLчем разница между формой и CHTML в YII

я использовал для образования, чтобы написать форму анса представить в базу данных работает нормально, но для некоторых конкретных мест, в соответствии с поиском Google используется CHtml вместо форма. но когда я представил эту форму в базу данных

CHTML значение TextField не подает в базу данных

здесь идет мой код

_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('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->render('index',array(
      'dataProvider'=>$dataProvider, 
     )); 
    } 

    /** 
    * Manages all models. 
    */ 
    public function actionAdmin() 
    { 
     $model=new Marks('search'); 
     $model->unsetAttributes(); // clear any default values 
     if(isset($_GET['Marks'])) 
      $model->attributes=$_GET['Marks']; 

     $this->render('admin',array(
      'model'=>$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()->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']) && $_POST['ajax']==='marks-form') 
     { 
      echo CActiveForm::validate($model); 
      Yii::app()->end(); 
     } 
    } 
public function actiondynamicstates() 
{ 

     // $model1=new Examresults; 
     /*$exam_type=(int)$_POST['exam_type']; 


     if ($exam_type==0) 
       $data=array(0=>'States:'); 
     else if ($exam_type==1) 
       $data=array(1=>'Alaska', 2=>'California');  
     else if ($exam_type==2) 
       $data=array(1=>'Orleans', 2=>'Bordeaux'); 
     else if ($exam_type==3) 
       $data=array(1=>'Hokkaido', 2=>'Okinawa');*/ 
//$schoolfee = feesettings::model()->find("feetype='schoolfee' and class='".$_REQUEST['class']."'"); 

$data=admission::model()->findAll('class=:class', 
        array(':class'=>$_POST['class'])); 


    $data=CHtml::listData($data,'studentid','studentfname'); 

     foreach($data as $value=>$name) 
       echo CHtml::tag('option', array('value'=>$value), CHtml::encode($name), true); 

} 

} 

Marks.php

<?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'=>true), 
      array('class, studentid, examtype', 'length', 'max'=>30), 
      array('grade', 'length', 'max'=>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'=>'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=>label) 
    */ 
    public function attributeLabels() 
    { 
     return array(
      'id' => 'ID', 
      'class' => 'Class', 
      'studentid' => 'Studentid', 
      'examtype' => 'Examtype', 
      'firsttlanguage' => 'Firsttlanguage', 
      'secondlanguage' => 'Secondlanguage', 
      'thirdlanguage' => 'Thirdlanguage', 
      'mathematics' => 'Mathematics', 
      'science' => 'Science', 
      'social' => 'Social', 
      'totalmarks' => 'Totalmarks', 
      'grade' => '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->compare('id',$this->id); 
     $criteria->compare('class',$this->class,true); 
     $criteria->compare('studentid',$this->studentid,true); 
     $criteria->compare('examtype',$this->examtype,true); 
     $criteria->compare('firsttlanguage',$this->firsttlanguage); 
     $criteria->compare('secondlanguage',$this->secondlanguage); 
     $criteria->compare('thirdlanguage',$this->thirdlanguage); 
     $criteria->compare('mathematics',$this->mathematics); 
     $criteria->compare('science',$this->science); 
     $criteria->compare('social',$this->social); 
     $criteria->compare('totalmarks',$this->totalmarks); 
     $criteria->compare('grade',$this->grade,true); 

     return new CActiveDataProvider($this, array(
      'criteria'=>$criteria, 
     )); 
    } 
} 
+0

почему эти люди вниз голосования меня, я написал свою реальную проблему, я новичок в Yii, если ур эксперт быть как эксперт не делают глупостей – Devendar

ответ

2

Посмотрите здесь Creating Form. Так как Yii 1.1.1 есть виджет CActiveForm , то при использовании этого виджета вы создаете входы формы с $form->textField($model,'username') вместо CHtml::activeTextField($model,'username'), но оба работают одинаково.

Так что если это поле не сохраняется в базе данных, это, вероятно, потому, что вы не добавили его в правила своей модели.

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

спасибо за помощь – Devendar

+0

добавил мой код также, пожалуйста, проверьте – Devendar

+0

Что ты хочешь меня проверить? – Puigcerber

Смежные вопросы