I have built a contact form using CakePHP on my site. The controller logic is as follows:
<?php
class ContactController extends AppController
{
var $helpers = array ('Html','Form');
var $components = array ('Email','RequestHandler');
var $name = 'Contact';
function beforeFilter()
{
parent::beforeFilter();
$this->Auth->allow(array('*'));
}
function index()
{
if ($this->RequestHandler->isPost())
{
$this->Contact->set($this->data);
if ($this->Contact->validates())
{
$this->Email->to = '###';
$this->Email->subject = 'Contact message from ' . $this->data['Contact']['name'];
$this->Email->from = $this->data['Contact']['email'];
$this->Email->send($this->data['Contact']['message']);
$this->render('success');
}
}
}
}
?>
What I want to do is when a user submits the form is show another view file such as success.ctp but what happens is even though they have a new view they could refresh the page and send the data again and again. How do I stop this…
Can anyone help? Thanks
after you process their form data, redirect them to the same contact page (to avoid the refresh problem)
If you’re not aware, you can setFlash to show a success message. But if you want to customize it the way you want, you can write a variable to Session to signal the view.
And in the view: