0

I am new to phalcon and trying to learn this framework. I have created a simple form. Now, When I fill up the form, I am able to store the data in database. After submitting the form I want to be on the same page. Now the issue is, form is still have the filled data it is not clearing up. I have googled and found the clear() method to clear the form data but it seems that its not working. What to do?

Here is my code //Controller

use Phalcon\Mvc\Controller;
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 {

        }


    }
}

//views

<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>
braX
  • 11,506
  • 5
  • 20
  • 33

2 Answers2

0

Once you save the data to database, redirect to the controller instead of forwarding the request because forwarding the request won't reload the page.

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

Read this what's the difference between redirect and dispatch in phalcon? for more information

Sajan
  • 81
  • 2
  • 9
0

You must create form class to have ability using $form->clear() method.

See the doc https://docs.phalconphp.com/cs/3.4/forms

Denys Kurbatov
  • 143
  • 2
  • 10