Basic PHP MVC setup for form submissions I'd like your thoughts on this basic implementation of MVC. The view outputs a form, and when the page is loaded, the controller checks for a form submission and then updates the model accordingly. Finally, the view outputs the form again, grabbing the new values from the model.

One problem with this is that if the user refreshes the page, a submission will still happen because the $_POST['submitted'] flag is still sent. Ideally, I'd like to avoid this - is there a suggestion for using AJAX to make this work?

&lt;?php

class EntryModel {
        public $date;
        public $amount;

        public function update_date($date) {
                $this-&gt;date = $date;
        }

        public function update_amount($amount) {
                $this-&gt;amount = $amount;
        }
}

class EntryController {
        private $model;

        public function __construct(EntryModel $model) {
                $this-&gt;model = $model;
        }

        public function get_submission() {
                if (!isset($_POST['submitted']))
                        return;

                foreach ($_POST as $g =&gt; $k)
                {
                        if ($g == 'form_date')
                                $this-&gt;model-&gt;update_date($k);
                        elseif ($g == 'form_amount')
                                $this-&gt;model-&gt;update_amount($k);
                }

                echo ("Sucessful submission");
        }

}

class EntryView {
        private $model;
        private $controller;

        public function __construct(EntryController $controller, EntryModel $model) {
                $this-&gt;controller = $controller;
                $this-&gt;model = $model;
        }

        public function output() {
                $form_output =
                '&lt;form action="index.php" method="POST"&gt;
                        &lt;div&gt;Date: &lt;input type="date" name="form_date" value="' . $this-&gt;model-&gt;date . '"&gt;&lt;/div&gt;
                        &lt;div&gt;Amount: &lt;input type="number" name="form_amount" value="' . $this-&gt;model-&gt;amount . '"&gt;&lt;/div&gt;
                        &lt;input type="hidden" name="submitted" value="yes"&gt;
                        &lt;input type="submit"&gt;
                &lt;/form&gt;';

                return $form_output;
        }
}

$model = new EntryModel();
$controller = new EntryController($model);
$view = new EntryView($controller, $model);

$controller-&gt;get_submission();

echo $view-&gt;output();

?&gt;
Templates

Your HTML code should be separated out into a template file. You should do your best to avoid mixing HTML into your PHP, as your HTML will inevitably grow, and your PHP will get messier as a result. 

Post/Get/Redirect

The pattern you are looking for is Post/Get/Redirect. You should always follow this pattern. When a user submits a form using POST, you should process the form, and then if it was successful, redirect them back to some page (possibly the form page again) with an added URL parameter.

E.g. http://example.com/contact-us?submitted=true

Then in your form code, you can check if the URL parameter is present, and show a success message.