Php 重定向回消息
我有一个名为Php 重定向回消息,php,smarty,Php,Smarty,我有一个名为editprofile.php的文档,还有一个名为action.php。当用户使用editprofile.php提交信息时。信息被POSTed到action.php,我在那里处理信息并将其发送到mysql。发送后,我想显示一条消息,说明一切都已成功更改。我用了这个: if $everythingisdone{ $smarty->assign('sucess', 'Your changes have been made'); header('Location: ' . $_SER
editprofile.php
的文档,还有一个名为action.php
。当用户使用editprofile.php
提交信息时。信息被POST
ed到action.php
,我在那里处理信息并将其发送到mysql。发送后,我想显示一条消息,说明一切都已成功更改。我用了这个:
if $everythingisdone{
$smarty->assign('sucess', 'Your changes have been made');
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
我被重定向到上一页,但即使我的editprofile.tpl中有此消息,也没有消息
{if $sucess}
<div class="sucess">
{$sucess}
</div>
{/if}
{if$success}
{$suces}
{/if}
重定向回页面时如何分配消息?您不能重定向到另一个页面并期望变量保持不变。当您调用$smarty->assign()
时,该更改将仅针对该特定页面请求而保留,一旦您重定向它的所有内容,将不再保留
可以使用GET参数执行此操作:
<?php
// action.php
if ($something) {
header ("Location: " . $_SERVER['HTTP_REFERER'] . "?success=1");
die;
}
else {
header ("Location: " . $_SERVER['HTTP_REFERER'] . "?success=0");
die;
}
如果您不这样重定向,那么就没有必要这样做。为什么不把表单放在editprofile.php
上,然后提交到同一个页面,并在那里检查错误?如果表单未提交,请设置标志变量并显示错误消息:
<?php
if ($ok) {
$smarty->assign ('form_processed', true);
}
else {
$smarty->assign ('form_processed', false);
}
您可以执行以下操作:
//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
}
你的代码
....
if($everythingisdone) {
$_SESSION['success'] = 'Your changes have been made';
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
和视图:
{if isset($smarty.session.flash) && $smarty.session.flash != ''}
<div class="sucess">{set_flash}</div>
{/if}
{if-isset($smarty.session.flash)&&$smarty.session.flash!='''}
{set_flash}
{/if}
是否每次有人访问页面时都会运行该功能?这会影响服务器速度吗?@shnisaka不,它不会对服务器速度产生太大的影响,因此您需要重新考虑它。。。此功能仅在您有会话闪存消息的情况下运行。。
{if isset($smarty.session.flash) && $smarty.session.flash != ''}
<div class="sucess">{set_flash}</div>
{/if}