Search code examples
phpyii2yii2-model

How to check if submitbutton detect click


Hello Please I try learn this tutorial

Yii2 tutorial 3

But not working, when I click on submit button not change... if edit it's empty or not...

I thing so <?= Html::submitButton('Submit',['class'=>'btn btn-success']); ?> not detect click, how can check it?

here are my code

in siteControler

public function actionUser()
    {
        $model = new UserForm;

        if($model->load(Yii::$app->request->post()) && $model->validate()){
            Yii::$app->session->setFlash('success', 'You have entered the data correctly');
        }
        return $this->render('userForm',['model'=>$model]);
    }

models UserForm.php

 <?php
namespace app\models;

use yii\base\Model;

class UserForm extends Model{
    public $name;
    public $email;


    public function rules(){
        return [
                    [['name','email'],'required'],
                    ['email','email']
                  ];
    }
}

site userForm.php

   <?php

    use yii\helpers\Html;
    use yii\widgets\ActiveForm;

?>

<?php
    if(Yii::$app->session->hasFlash('success')){
        echo Yii::$app->session->getFlash('success');
    }
?>

<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model,'name') ?>
<?= $form->field($model,'email') ?>

<?= Html::submitButton('Submit',['class'=>'btn btn-success']); ?>

I control from video but I thing so it's 1:1 on video work good but for me, no.. why?

Thanks


my code.. I put into name : adfs and into email: [email protected] and press Submit button, and I want shown on top 'You have entered the data correctly' but not shown... I thing so I can't detect when press button


Solution

  • Got it you have not closed $form obj, that why you're submit was not triggering. Try this view code site userForm.php

    <?php
    
    use yii\helpers\Html;
    use yii\widgets\ActiveForm;
    
    ?>
    
    <?php
    if(Yii::$app->session->hasFlash('success')){
        echo Yii::$app->session->getFlash('success');
    }
    ?>
    
    <?php $form = ActiveForm::begin(); ?>
    <?= $form->field($model,'name')->textInput() ?>
    <?= $form->field($model,'email')->textInput() ?>
    
    <?= Html::submitButton('Submit',['class'=>'btn btn-success']); ?>
    <?php ActiveForm::end(); ?>
    

    Let me know if it not working.