Sunday, January 22, 2012

Re: redirect doesn't work when controller function is called from JS-function

you are calling POST call via AJAX request, so, you can't use $this->redirect() in controller
from controller method check if was ajax call, if it was, print code below in controller:

<meta http-equiv="refresh" content="0;url=<?php print Router::url($yourUrlArray, true); ?>" />

and after print this, use exit or die;

what will this do:
this will print this meta tag which will redirect with browser to your new url, you have to modify your javascript post method, that will handle result and display it in browser :)
<meta http-equiv="refresh" content="0;url=NEW PAGE URL" />

--
Lep pozdrav, Tilen Majerle



2012/1/22 Tomfox Wiranata <tomfox.wiranata@gmail.com>
Hi everyone,

I have used redirect a thousand times successfully. but this one is
weird.
This is my process. User wants to save his data and the system checks
if all necessary field are filled out. if so, save the data. if not,
give an JS-alert.

the whole thing works. the alert when data is missing
and
the saving when the form is complete. cake just wont redirect after
saving. here is my code:


the trigger in my view (the button)
========================

echo '<div class="small_button" onclick="save()"
style="float:left;cursor:pointer;">'."Save".'</div>';


javascript function in my view:
========================

function save()
{
       var title_content = $('#TitlePopup').val();


       if (!title_content)
               $.pnotify({     pnotify_title: 'Missing Fields.', pnotify_text: 'Please
fill out all fields.'});
       else
               $.post(installFolder + 'books/save', {});

}


books_controller:
========================

function save()
{

...some code here for saving the data....

$this->Session->setFlash('saving successful');
$this->redirect(array('controller' => 'books', 'action' => 'view',
$id));


}


the redirect works(!) when i call the controllers save() function
directly like:
echo $html->link('Save', '/books/save',
array('class'=>'small_button'));


is this some technology thing with no workaround possible? like JS and
cakePHP wont work together in this case? or am i missing some
fundamentals here?


appreciate your help :)

thanks!!



--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: