Lekhulal Mathalipara
Lekhulal Mathalipara

Reputation: 57

How to get form element's into controllers in yii

How to get form element's into controllers in yii

views/TblRegistration/_form.php

<div class="row">
    <?php echo $form->labelEx($model,'director'); ?>
    <?php echo $form->textField($model,'director',array('size'=>50,'maxlength'=>50,'name'=>'txtDirector')); ?>
    <?php echo $form->error($model,'director'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'experience'); ?>
    <?php echo $form->textField($model,'experience',array('name'=>'txtExp')); ?>
    <?php echo $form->error($model,'experience'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'language'); ?>
    <?php echo $form->textField($model,'language',array('size'=>50,'maxlength'=>50,'name'=>'txtLang')); ?>
    <?php echo $form->error($model,'language'); ?>
</div>

<div class="row buttons">
<?php echo CHtml::submitButton('Accept', array('name' => 'btnSubmit')); ?>

</div>

controllers/TblRegistrationController.php

public function actionRegister()
{
    $model=new TblRegistration;

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

            if(isset($_POST['btnSubmit']))
            {
                      $dir=isset($_POST['txtDirector']); 
                      $exp=isset($_POST['txtExp']); 
                      $lan=isset($_POST['txtLang']);                                  

                      $cmd=Yii::app()->db->createCommand();
                      $cmd->insert('tbl_registration',
                              array('director'=>$dir,'experience'=>$exp,'language'=>$lan));
                    }       

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

In database it inserted the values as 1 1 1. Why I am having this problem?

Upvotes: 1

Views: 58

Answers (1)

Kumar V
Kumar V

Reputation: 8838

You will get the form data in $_POST as :

$_POST["MODEL_NAME"]["FIELD_NAME"]

In your case:

$_POST["TblRegistration"]["director"]

Also, You are not assigning values to variable properly. Try below code

$dir=$_POST['TblRegistration']['txtDirector']; 
$exp=$_POST['TblRegistration']['txtExp']; 
$lan=$_POST['TblRegistration']['txtLang'];  

Upvotes: 1

Related Questions