PHP重定向出错了

问题描述 投票:-1回答:2

我将我的网站的所有形式提交到特定页面页面form_submition.php,处理完表单数据后,我使用标题函数重定向到另一个页面index.php?page=some page,但在我的一个名为main_form的表单中,我得到了错误的重定向

    if(isset($_POST['form1']))
    {
        // process form data
        header('Location: ../index.php?page=some page')
    }
    if(isset($_POST['form2']))
    {
        // process form data
        header('Location: ../index.php?page=some page')
    }

    $to=$_GET['to']
    if (isset($_POST['main_form']) ) {
            // process form data
    }
    header("Location: ../index.php?page={$to}&error_loc_{$error_loc}=1&err_msj=" . $err_msj, true, 302);
    exit(); 

但它将我重定向到mywebsite.com/folders/form_submitions.php?to=all_p

而不是mywebsite.com/index.php?page=all_p

有任何想法吗?

更新1

我检查了很多代码,似乎标题根本没有重定向页面。奇怪的是mywebsite.com/index.php?page=all_p的页面被加载但是url不会改变并且css + js文件没有被加载,因为相对路径变得无效。

更新2

进一步检查我的代码,在将main_form提交给form_submission.php之前,我打开一个带有javascript的新窗口来打印一些数据。以下是表格:

<form enctype='multipart/form-data' action='./forms/form_submitions.php?to=all_p' onsubmit="return checkfiles('attachfiles')">
 .
 .
 .
    <button id="main_form" name="main_form" class="btn btn-primary">Save Changes</button>
</form>

单击#main_form时,我调用以下函数:

$("#main_form").on('click', function(event) {
newWin = window.open('printingpage.php?data='+ data);
setTimeout(function () { newWin.print(); }, 500);

});

除非关闭打印预览,否则打印功能不会返回。我认为这会产生问题,因为当我删除newWin.print()函数时,一切正常。有没有办法使这种打印异步?

php form-submit
2个回答
-1
投票

删除路径中的..部分并尝试。

所以代码将是:

 if(isset($_POST['form1']))
    {
        // process form data
        header('Location: /index.php?page=some page')
    }
    if(isset($_POST['form2']))
    {
        // process form data
        header('Location: /index.php?page=some page')
    }

    $to=$_GET['to']
    if (isset($_POST['main_form']) ) {
            // process form data
    }
    header("Location: /index.php?page={$to}&error_loc_{$error_loc}=1&err_msj=" . $err_msj, true, 302);
    exit(); 

-1
投票

试试这个。将您的Web链接放在重定向链接中。

$data = array(
      'page' => $to,
      'error_loc_'.$error_loc => 1,
      'err_msj' => $err_msj
);

header("Location: http://www.mywebsite.com/index.php?".http_build_query($data), true, 302);

希望这可以帮助。

© www.soinside.com 2019 - 2024. All rights reserved.