Controller renders page but the url shows the controller function name (2.0)

Hi. I’m using Cake PHP 2.0 ,and I did manage to get my controller to re-render the page after a form submits, however , the page the form is on is called contact.ctp, and the url shows /contact at the end. But when I do a $this->render(‘/Pages/contact’); in my controller, it does re-render the page so it stays on the same page, but the url shows the controller function at the end and not contact. /leads/add .

Could this be a routing thing I need to do ? Or a better way to stay on the same page after form submit ? Would be greatly appreciated Thanks , have been wrestling with this for a couple week now. Am new to cake but not to php.


Typically, you wouldn’t render a different template, but rather redirect to the different URL.

Thanks so much but I don’t want to go to a different url . when the form submits, I just want it to stay on the same page and show the same page again. Everyother thing I have tried it just shows the footer or goes to main index page
I can post code . the controller:

<?php error_reporting(E_ALL); ini_set('display_errors', '1'); App::uses('CakeEmail', 'Network/Email'); class LeadsController extends AppController { public $helpers = array('Html', 'Form', 'Session'); public $components = array('Session', 'Flash', 'Email'); public function add() { if ($this->request->is('post')) { $email = new CakeEmail('default'); $email->from('[](') ->to($this->request->data['Lead']['email']) ->subject('Thank you for your submission') ->emailFormat('html'); $emailContent = '

Thank you for your submission!

Please visit our website at []( for more information.

Best regards,

Your Company

'; $email->send($emailContent); if ($this->Lead->save($this->request->data)) { $this->Session->setFlash('Message Successfully Sent!.'); } } $this->render('/Pages/contact'); } } the form : <?php echo $this->Form->create('Lead', array('url' => array('controller' => 'leads', 'action' => 'add'))); ?>
<?php echo $this->Form->input('first_name', array('class' => 'f1')); ?>
<?php echo $this->Form->input('last_name', array('class' => 'f1')); ?>
<?php echo $this->Form->input('email', array('class' => 'f1')); ?>
<?php echo $this->Form->input('phone_number', array('class' => 'f1')); ?>
<?php echo $this->Form->input('message', array('class' => 'f2', 'label' => false, 'placeholder' => 'send us a message' )); ?>
<?php echo $this->Form->hidden('lead_type_id', array('value' => 1)); ?> <?php echo $this->Form->hidden('priority', array('value' => 2)); ?> <?php echo $this->Form->hidden('safe', array('value' => 1)); ?> <?php echo $this->Form->hidden('spam', array('value' => 0)); ?>
<?php echo $this->Form->submit('Submit', array('class' => 'btn btn-primary btn1')); ?>
<?php echo $this->Session->flash(); ?>
<?php echo $this->Form->end(); ?>


You are saying you don’t want to redirect to a different page, but you want the URL to indicate that you did, in fact, redirect to a different page. Why not redirect instead of render? It’s called “Post Redirect Get”, and it’s a very standard programming paradigm that solves a lot of problems you’re going to run into with your current structure.

no I start out from /contact. there is a form that is submitted and saves some data to database. I want to stay on page and show flash message , which it does in this code, however, when it’s all done the url says /leads/add. instead of. /contact
thjanks so much

But, you are NOT staying on the same page. You are posting that data to the add function. The general recommendation is that you Post to add, then that function Redirects to contact. If you want to never leave the contact page, then you need to use Ajax instead of a standard POST.

None of this is in any way Cake specific, it’s how web applications work no matter what language they’re implemented in. You can post with HTTP (as you’re doing), which loads a new page, or you can post with Ajax, which does not, but comes with other complexities.

ok what is the right way to do it then , I already tried ajax, it was a hassle. So once I sav e my data I am on leads/add, then I would do what to redirect back to /contact? and display the flash message this way seems to work and all other ways just ended up showing me the feooter only. What is the recommended way?please sir. And I have another situation. I have email code and config that has no errors at at biut doesnt send the email… Should I post the code with you or po=st it on the forum?

You just return a redirect. The flash message should be saved and show up in the next page that’s actually rendered, which will be when the user gets back to the contact page.

Any issue with emailing is an entirely separate question, and should be posted as a new topic.

your absolutely right I changed the word from render to redirect and it still works the same but shows the right page. Thanks