php 表单提交后php返回页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23035085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:26:45  来源:igfitidea点击:

php return to page after form submission

phphtmlmysqlforms

提问by Carla Dessi

I've created a html form

我创建了一个 html 表单

<form action="http://localhost/php/insert.php" method="post">
    Barcode: <input type="text" name="barcode" /><br><br>
    Serial: <input type="text" name="serial" /><br><br>
    <input type="submit" />
</form>

which saves into my database using this php:

使用这个 php 保存到我的数据库中:

<?php
$con = mysql_connect("example","example","");
if ( ! $con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);

$sql = "INSERT INTO asset (barcode, serial)
        VALUES ('$_POST[barcode]','$_POST[serial]')";

if ( ! mysql_query($sql, $con)) {
    die('Error: ' . mysql_error());
}
mysql_close($con)
?>

the form saves to the DB but the page just goes to http://localhost/php/insert.phpwhen you press submit, which is a blank page. how can I make it stay on the same page and just show a message or something?

表单保存到数据库中,http://localhost/php/insert.php但当您按下提交时,页面就会进入,这是一个空白页面。我怎样才能让它停留在同一页面上并只显示一条消息或其他东西?

采纳答案by Jared Farrish

Note that redirecting with HTTP_REFERERis flaky and untrustworthy in general, so the best way is to have a URL sent with the request or, if it's coming from another server, you could build a specific pass-through to use for specific sites/URLs. The best is to know the URL by having it be submitted with the request; the other two are probably less than optimal.

请注意,重定向HTTP_REFERER通常是不稳定且不可信的,因此最好的方法是将 URL 与请求一起发送,或者,如果它来自另一台服务器,您可以构建特定的传递以用于特定的站点/URL。最好是通过将 URL 与请求一起提交来了解 URL;其他两个可能不是最佳的。

After your form processing is done, do this before outputting anything (which will throw an error):

完成表单处理后,在输出任何内容之前执行此操作(这将引发错误):

header("Location: {$_SERVER['HTTP_REFERER']}");
exit;

Depending on what your processing outcomes could be, you could append an error to the URL:

根据您的处理结果可能是什么,您可以将错误附加到 URL:

if ($query_does_not_execute) {
    $errcode = "error_code=003";
}

$referer = $_SERVER['HTTP_REFERER'];

if ($errcode) {
    if (strpos($referer, '?') === false) {
        $referer .= "?";
    }

    header("Location: $referer&$errcode");
} else {
    header("Location: $referer");
}
exit;

Then on the page, you could show the error. Or, alternatively, you could redirect to a page to handler errors of different kinds. This is wide-open to a number of different interpretations.

然后在页面上,您可以显示错误。或者,您可以重定向到页面以处理不同类型的错误。这对许多不同的解释都是开放的。

回答by Ashley

You can use PHP's Header() functionlike so:

您可以像这样使用PHP 的 Header() 函数

header("Location: your-page.php");
exit;

You'll need to make sure there is no output (i.e. echoing anything, or any HTML etc) before the header call or it will not work.

您需要确保在标头调用之前没有输出(即回显任何内容或任何 HTML 等),否则它将无法工作。

回答by MattJamison

Another way I found works is redirecting with html's <meta>. Put it in your http://localhost/php/insert.phpscript . After processing redirect to desired page.

我发现有效的另一种方法是使用 html 的<meta>. 把它放在你的http://localhost/php/insert.php脚本中。处理后重定向到所需页面。

<?php 

switch ($INPUT['task'])
{
    case 'insert_and_redirect':
            insertForm();
            $h = '<html><head>';
            $h .=   '<meta http-equiv="refresh" content="0; URL=/adjustments/">';
            $h .=   '</head></html>';
            echo $h;
        break;
    case 'edit':
       # editForm();
        break;
    case 'delete':
        deleteRecord();
        break;
    default:
        break;
}

?>

Probably unsecure but if you're on a private LAN should be fine.
-Cheers

可能不安全,但如果您在专用 LAN 上应该没问题。
-干杯

回答by Parimal

Use this in your form action $_SERVER['PHP_SELF'];it will fulfill your requirement.

在您的表单操作中使用$_SERVER['PHP_SELF'];它,它将满足您的要求。