首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >表单提交后调用当前id

表单提交后调用当前id
EN

Stack Overflow用户
提问于 2016-09-03 02:01:59
回答 2查看 46关注 0票数 0

我有一个数据库,允许用户通过编辑页面更新记录。提交编辑后,我希望浏览器自动返回到他们更新的页面以查看结果。我考虑过使用JS来实现这一点:

代码语言:javascript
复制
if(mysqli_query($con, $sql)){
    echo "<script> alert('You have successfully updated.');
            window.location.href='javascript:history.go(-2)';
          </script>";

 }else{
    echo "An error has occurred. Please go back and check the code: ".mysqli_error($con);
 }

但这只会强制浏览器返回页面,而不是显示更改的刷新页面。我需要手动刷新页面才能以这种方式查看结果。

我也试过了:

代码语言:javascript
复制
if(mysqli_query($con, $sql)){
    echo "<script> alert('You have successfully updated.');
            window.location.href='01T.php?id=$id';
          </script>";

 }else{
    echo "An error has occurred. Please go back and check the code: ".mysqli_error($con);
 }

因为01T.php是显示信息的页面,但这不起作用。

如何使用更合适的PHP解决方案将浏览器重定向到编辑过的相同id以查看结果?

EN

回答 2

Stack Overflow用户

发布于 2016-09-03 02:12:47

使用header重定向

代码语言:javascript
复制
if(mysqli_query($con, $sql)){
    header("Location: [url]?id=$id");

 }else{
    echo "An error has occurred. Please go back and check the code: ".mysqli_error($con);
 }
票数 0
EN

Stack Overflow用户

发布于 2016-09-03 02:48:39

我将使用PHP Sessions来保存页面之间的消息,然后在加载新页面时检查消息是否存在。如果有,则显示它。

在您的示例中,如果查询运行成功,则保存消息,然后重定向用户。然后,当新页面加载时,检索该消息并显示它。

代码语言:javascript
复制
<?php
session_start();
...
if(mysqli_query($con, $sql)){
    $_SESSION['message'] = "success";
    header("Location: 01T.php?id=$id");
    exit;
}else{
    echo "error";
}

然后在您想要显示消息的每个页面上:

代码语言:javascript
复制
<?php
session_start();
$msgHtml = null; //if it stays null, no message will be shown

//if there is a message. Capture it, then clear it from the session
if(!empty($_SESSION['message'])){
    $message = $_SESSION['message'];
    unset($_SESSION['message']);
    //Build the content you want the user to see. Add CSS classes to style it
    $msgHtml = "<div>$message</div>"
}

现在,如果有给用户的消息,就显示它。如果没有消息,则不会输出任何内容。

在PHP中:

代码语言:javascript
复制
echo $msgHtml;

或者在HTML中:

代码语言:javascript
复制
<body>
    <?= $msgHtml ?>
</body>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39298353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档