清除 Phalcon 框架中表单的填写数据



我是phalcon的新手,正在尝试学习这个框架。我创建了一个简单的表单。现在,当我填写表格时,我能够将数据存储在数据库中。提交表格后,我想在同一页面上。现在的问题是,表单仍然有填充的数据,它没有清除。我已经用谷歌搜索并找到了清除表单数据的clear((方法,但似乎它不起作用。怎么办?

这是我的代码 控制器

use PhalconMvcController;
class ProfileController extends Controller
{
public function indexAction()
{
// Add some local CSS resources
$this->assets->addCss("css/styles.css");
$this->assets->addCss("css/bootstrap.min.css");
// And some local JavaScript resources
$this->assets->addJs("js/jquery.js");
$this->assets->addJs("js/bootstrap.min.js");
}
public function createAction(){
//create object of model class
$profile = new Tbl_profile();
$profile->fname= $this->request->getPost('firstname');
$profile->lname= $this->request->getPost('lastname');
$profile->email= $this->request->getPost('email');
$success=$profile->save();
if($success) {
$this->flash->success("Profile created!");
//redirect to same page.
$this->dispatcher->forward(['action' => 'index']);
}else {
}

}
}

//视图

<html>
<head>
<title>Title</title>
<?php $this->assets->outputCss(); ?>
</head>
<body>
<h4 class="ml-5 mt-2"> Create Profile</h4>
<?php echo $this->getContent(); ?>
<?php echo $this->tag->form("profile/create") ?>
<table class="table">
<tbody>
<tr scope="row">
<td>First Name</td>
<td><?php echo $this->tag->textField("firstname"); ?></td>
</tr>
<tr scope="row">
<td>Last Name</td>
<td><?php echo $this->tag->textField("lastname"); ?></td>
</tr>
<tr scope="row">
<td>Email</td>
<td><?php echo $this->tag->textField("email"); ?></td>
</tr>
<tr scope="row">
<td></td>
<td><?php echo  $this->tag->submitButton("Create");?></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>

将数据保存到数据库后,重定向到控制器而不是转发请求,因为转发请求不会重新加载页面。

$this->request->redirect('profile/index');

阅读这篇文章 phalcon 中的重定向和调度有什么区别?

您必须创建表单类才能使用$form->clear()方法。

请参阅文档 https://docs.phalconphp.com/cs/3.4/forms

最新更新