I have a document called editprofile.php
and I have another one that is called action.php
. when the user submits their info using editprofile.php
. the information get POST
ed to action.php
where I process the information and send it to mysql. After sending it I want to show a message that everything has been successfully changed. I used this :
if $everythingisdone{
$smarty->assign('sucess', 'Your changes have been made');
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
I get redirected to the previous page, but there is no message even though I have this in my editprofile.tpl
{if $sucess}
<div class="sucess">
{$sucess}
</div>
{/if}
how can I assign the message when I redirect back?
you could do:
//custom smarty function to set session flash messages
function smarty_function_set_flash($params, $smarty) {
$flash = "";
if (isset($_SESSION['success'])) {
$flash = $_SESSION['success'];
$_SESSION['success'] = ""; //unset the session
}
return $flash; //return flash message
}
your code
....
if($everythingisdone) {
$_SESSION['success'] = 'Your changes have been made';
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
and view:
{if isset($smarty.session.flash) && $smarty.session.flash != ''}
<div class="sucess">{set_flash}</div>
{/if}
You can't redirect to another page and expect variables to persist. When you call $smarty->assign()
that change will persist only for that specific page request and no more, once you redirect its all gone.
You could do this with a GET parameter:
<?php
// action.php
if ($something) {
header ("Location: " . $_SERVER['HTTP_REFERER'] . "?success=1");
die;
}
else {
header ("Location: " . $_SERVER['HTTP_REFERER'] . "?success=0");
die;
}
Then in editprofile.php
you could check for that value:
<?php
// editprofile.php
if ($_GET['success'] == '1') {
// yes!
}
else {
// aww :(
}
This would not be necessary if you wouldn't redirect like that. Why not have the form on the editprofile.php
and submit it to the same page and check for errors there? Set a flag variable and show an error message if the form did not submit:
<?php
if ($ok) {
$smarty->assign ('form_processed', true);
}
else {
$smarty->assign ('form_processed', false);
}
Then simply use that in your form to check for an error.
Hope this helps