【问题标题】:Redirecting after completion of form action表单操作完成后重定向
【发布时间】:2012-03-11 07:25:55
【问题描述】:

我正在使用以下代码进行表单操作,

// includes




require_once('inc/mysql_wrapper.php');
$DB=get_db_connection(); 
require_once('inc/defines.inc.php');
require_once('lib/functions.php');
require_once('lib/framework.php');
require_once('lib/specific.php');
require_once('lib/table_editor.php');
require_once('lib/user.php');
require_once('models/basic.php');
require_once('models/list.php');
require_once('swiftmailer/lib/swift_required.php');
require_once('recaptcha/recaptchalib.php');
// end includes

$q="SELECT value FROM $T_SETTINGS
WHERE name='register'";
$register=$DB->oq($q);

$optin=get_setting("optin");

// select custom fields if any
$q="SELECT * FROM $T_FIELDS 
WHERE list_id='$_GET[list_id]'
ORDER BY id";
$efields=$DB->aq($q);

// select mailing list
$q="SELECT * FROM $T_LISTS WHERE id='$_GET[list_id]'";
$list=$DB->sq($q);

if(!empty($_POST['ok']))
{
    // validate recaptcha if required
    if($list['require_recaptcha'])
    {
        $resp = recaptcha_check_answer (RECAPTCHA_PRIVATE,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

        if (!$resp->is_valid) 
        {
            $recaptcha_error="Looks like the words you entered are not correct!";        
            $template="templates/register.html";
            require_once('templates/master.html');
            exit;
        }                        
    }

    User::register($_POST, $efields, $optin);

    if(!empty($list['redirect_to']))
    {
        redirect($list['redirect_to']);
        exit;
    }

    $template="templates/message.html";
}
else
{
    $template="templates/register.html";
}

require_once('templates/master.html');

不包括其他一些选项,主要是这个表单接收的表单数据是

name , email , ok (用于检查原始表单是否已提交的标志)

提交表单后如何将此页面重定向到thanks.php?

【问题讨论】:

  • 您在哪个页面发布表单?
  • 这里贴出代码的页面名称是abc.php,数据来自form.php,action url写成../abc.php?value=15 ,

标签: php forms post redirect http-headers


【解决方案1】:

如果一切顺利,您可以使用 php header 进入所需页面:

header( 'Location: thanks.php' );

【讨论】:

  • 第一件事,我应该把它放在哪里?第二,我最后做到了,但是出现了 header 已经发送的错误...
【解决方案2】:

当数据有效时,添加:

header('Location: http://www.example.com/thanks.php');

【讨论】:

    【解决方案3】:

    或javascript:

    ?>
    
    <script type="text/javascript">
    window.location = "your>"
    </script>
    
    <?php
    

    【讨论】:

      【解决方案4】:

      不能直接在thanks.php中发布吗?

      Thanks.php 应该控制数据格式和显示消息,或重定向(通过将字符串放入标题中:header('Location: urlOfForm-postedPage);

      因此,如果数据正确(并且如果需要控件,但我想是的),则不需要重定向。

      此外,如果对原始页面进行控制(简单的 JS 或 JQuery?),您可以节省一些重定向。 这样会更有效率!

      【讨论】:

      • 不,我不能将它直接发布到Thanks.php,因为那是多用途页面,我不能向它添加帖子
      • @NewBee 如果是一个多用途页面,只需在您的表单中发布一个可以识别引用页面的隐藏元素。通过这种方式,您可以在thanks.php 上添加一个控件,然后技巧就完成了。我不明白您为什么不能。如果你想直接发布它,那么,将你的表单发布到引用页面本身(所以 abc.php)。控制(作为对标题的第一次操作)如果您发布了某些内容然后重定向到thanks.php(但这样您必须进行重定向。在我建议的第一种方式中,您可以“折扣”一步)
      • 原因是,我使用相同的表单发布 3 个动作,我链接动作,form-> 1.php->2.php> 上面的代码是第三页正在发送表单数据,并且此页面用作管理页面(您可能已经猜到看到代码),为了完成更改,我想将其重定向到thanks.php
      • @NewBee :所以直接发布到 abc.php 页面或另一个中间页面并控制,进入标题,如果有一些数据发布。如果是,请使用我和其他一些用户向您建议的重定向字符串
      • 我们可以来聊天吗?空间不够,无法提供完整代码
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-12
      相关资源
      最近更新 更多