0
當我按提交按鈕注意到發生,甚至頁面不刷新。我已經檢查過很多次了。我不知道它有什麼問題。 我正在使用Yii2框架的基本應用程序和Apache服務器。yii2表格不提交
控制器:
<?php
namespace app\controllers;
use Yii;
use yii\web\controller;
use app\models\Form;
class FormController extends Controller{
public function actionForm(){
$model = new Form;
if($model->load(Yii::$app->request->post()) && $model->validate()){
Yii::$app->session->setFlash('success', 'You have entered data successfuly!');
}
return $this->render('view',['model'=>$model]);
}
}
?>
型號:
<?php
namespace app\models;
use yii\base\model;
class Form extends Model{
public $name;
public $email;
public function rules(){
return [
[['name','email'],'required'],
['email','email']
];
}
}
?>
查看:
<?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']); ?>
添加該行可解決問題。謝謝 –