<?php $this->redirect(array('controller' => 'Users', 'action' => 'login', ));
I would like to redirect and prom开发者_StackOverflowpt and alert box saying your registration was successfully.
How should I modify this code?
You can use $session->setFlash("message");
. In your Layout/View use $session->flash();
to get the output.
Example:
// Controller
$session->setFlash("message");
// View
print $session->flash("flash", array("element" => "alert"));
// views/Elements/alert.ctp
<script type="text/javascript">
alert("<?php print $message; ?>");
</script>
For more details see this Page
You can't display content and redirect using $this->redirect()
at the same time because it uses http headers to do the redirect. You have to display the message either on the target page or use JavaScript for the redirection instead.
You could save a session variable at the same time you save the user data ($this->Session->write( 'newUser', 1 )
) and check the variable in the login form. If the variable is set, show the alert box and clear the variable.
The other way is to use an extra parameter in the link that tells the login page that it should show the alert (users/login/newUser:1). The downside is that if the user bookmarks the page right after registering they'll see the message every time they visit the page.
If you show the alert and then redirect using JavaScript instead of headers, it of course works only if the user has JS enabled so in this case you should also provide a clickable link that leads to the login page.
精彩评论