简体   繁体   English

PHP-标头重定向后未设置Smarty变量

[英]PHP - Smarty Variables unset after header redirect

I have a php file (form.php) that uses a template (form.htm) to display a form. 我有一个使用模板(form.htm)显示表单的php文件(form.php)。 The template file's form action is form.php. 模板文件的表单操作是form.php。 If the form is incomplete when submitted, I want to redirect back to the form WITHOUT losing information that the user has already input. 如果提交时表单不完整,我想重定向回表单,而不会丢失用户已经输入的信息。

I'm setting smarty variables using t->assign('varname', $_POST['var']) and setting the form values to these variables ( value="{$varname}" ), however when redirected using php's header() function, these Smarty variables are lost (which I've confirmed using Smarty {debug}). 我正在使用t-> assign('varname',$ _POST ['var'])设置聪明的变量,并将表单值设置为这些变量(value =“ {$$ varname}”),但是当使用php的header( )功能,这些Smarty变量会丢失(我已经使用Smarty {debug}确认了)。

Does anyone have any idea of how to make it so that these variables aren't unset when using a header redirect? 有谁知道如何做到这一点,以便在使用标头重定向时不会取消设置这些变量? Thanks! 谢谢!

Don't redirect the user. 不要重定向用户。 Just redraw the form. 只需重新绘制表格。 In general, this HTTP workflow should be used when dealing with forms: 通常,在处理表单时应使用以下HTTP工作流程:

  1. GET /form.php - Display the form to the user GET /form.php向用户显示表单
  2. POST /form.php - Process the form input POST /form.php处理表单输入
    1. If the form input is invalid, send back form.php 如果表单输入无效,则发送回form.php
    2. If the form input is valid, save to the database, now redirect the user 如果表单输入有效,请保存到数据库, 现在重定向用户

So basically you have: 所以基本上你有:

  1. GET 得到
  2. POST (invalid input from the user) POST(来自用户的无效输入)
  3. POST (valid input from the user) POST(来自用户的有效输入)
  4. REDIRECT 重新定向

Smarty is only template engine, so you cannot do here more than in PHP. Smarty只是模板引擎,因此您不能在PHP中做更多的事情。

But in your case you said you want to make header redirect after sending form (to be honest I have no idea why you want to do it. In normal case you simple set action in form and in controller you simple check data and if they are valid you do what you want - for example send email and if they are not valid you simple show the form again - that's it). 但是在您的情况下,您说过要在发送表单后进行标头重定向(说实话,我不知道为什么要这么做。通常情况下,您可以简单地在表单中设置操作,在控制器中可以简单地检查数据,如果它们是有效即可执行您要执行的操作-例如发送电子邮件,如果无效,则只需再次显示该表单即可-就是这样。

However if you really need to do this that way, what you can do is to use session data and save all data from post to session, make redirection and use that from session to display in Smarty. 但是,如果您确实需要这样做,则可以使用会话数据并将所有数据保存到发布到会话中,进行重定向并使用会话中的数据显示在Smarty中。

So in PHP you can do: 因此,在PHP中,您可以执行以下操作:

$_SESSION['post_data'] = $_POST;
// now you make redirection 

And in file you handle redirection you can simple do: 在文件中处理重定向,您可以简单地执行以下操作:

$smarty->assign('post_data',$SESSION['post_data']);
unset($SESSION['post_data']);

And in template file you can then use: 然后,您可以在模板文件中使用:

{$post_data.var}

EDIT 编辑

But as I said normally you do it this way in PHP file: 但是正如我通常所说的,您可以在PHP文件中这样进行操作:

  $isValid = false; 

  if (isset($_POST['submit']) {
     $isValid = validateData($_POST);

     if ($isValid) {
          // do something here - for example send email
     }
     else {
         $smarty->assign('error', 'There were errors in your form. Try again');
     }
  } 
  $smarty->assign('is_valid',$isValid);

And in Smarty you do: 在Smarty中,您可以:

{if $isValid}
   form was sent
{else}
   {if isset($error)}{$error}{/if}
   you display form here
{/if}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM