My data is succesfully entering into database. But, not redirecting to register.php page after inserting.
Showing Notice like:
PHP Notice – yii\\base\\ErrorException
Array to string conversion
But, I Noticed that as soon i'm replacing this line return $this->refresh($post);
from SiteController.php, All working fine.
So, why this refresh($post)
not working.. as it was working fine in other page.
I'm not getting how to resolve this issue. I need help. Please help me.
register.php
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
?>
<?php $form = ActiveForm::begin(['id' => 'register-form']); ?>
<?= $form->field($model, 'fname')->textInput()->label('First Name') ?>
<?= $form->field($model, 'lname')->textInput()->label('Last Name') ?>
<?= $form->field($model, 'email')->textInput()->label('Email') ?>
<?= $form->field($model, 'password')->passwordInput()->label('Password') ?>
<div class="form-group">
<?= Html::submitButton('Register', ['class' => 'btn btn-success', 'name' => 'register-button' ]) ?>
</div>
<?php ActiveForm::end(); ?>
SiteController.php (Controller)
<?php
namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
class SiteController extends Controller
{
public function actionRegister()
{
$model = new RegisterForm();
if ($model->load(Yii::$app->request->post()))
{
$post = Yii::$app->request->post('RegisterForm');
Yii::$app->db->createCommand("INSERT INTO members SET FirstName=:fname, LastName=:lname, EmailID=:email, Password=:password",
array(':fname' => $post['fname'],':lname'=>$post['lname'],':email'=>$post['email'],':password'=>$post['password']))->execute();
Yii::$app->session->setFlash('registerFormSubmitted');
return $this->refresh($post);
}
return $this->render('register', [
'model' => $model,
]);
}
}
RegisterForm.php (model)
<?php
namespace app\models;
use Yii;
use yii\base\Model;
class RegisterForm extends Model
{
public $fname;
public $lname;
public $email;
public $password;
public function rules()
{
return [
[['fname','lname', 'email', 'password'], 'required'],
['email', 'email'],
];
}
public function register()
{
if ($this->validate())
{
/*
$form= new Form();
$form->name=$this->name;
$form->email=$this->email;
$form->passowrd=$this->passowrd;
$form->save();
*/
return true;
}
return false;
}
}
You can easily see on the error screen you have shown us that $anchor
being sent to the method named refresh()
is an array, and you are trying to concatenate it as if it was a string. That is it.
Since the documentation on that screen says $anchor
is expected to be a string in that method, the only reason left is the fact that you are sending it incorrect data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.