PHP表单提交后如何显示成功消息?

时间:2015-07-25 16:59:39

标签: php ajax forms redirect post

这是代码。我想这样 -

表单提交 - > page2.php - >重定向 - > page1.php(这是消息。弹出或其他)

page1.php中

<form action="page2.php" method="post" enctype="multipart/form-data" class="form-inline subscribe-form">

                    <input type="name" name="name" placeholder="Jack">
                </div>
                <button type="submit" name="sub" value="sub" >Submit</button>

            </form>

使page2.php

    <?php
//include necessary

if(isset($_POST['sub'])) {

    $nameget = mysqli_real_escape_string($dbconnect, $_POST['name']);
    $sqlentry = .....bla bla......//insert into DB
}

$getsql = mysqli_query($dbconnect, $);


if($getsql){
    mysql_close($dbconnect);
    header('location:page1.php');

}



?>

2 个答案:

答案 0 :(得分:4)

你在哪里:

header('location:page1.php');

在位置附加变量,例如:

header('location:page1.php?status=success');

在page1.php上,执行以下操作:

if( $_GET['status'] == 'success'):
    echo 'feedback message goes here';
endif;

答案 1 :(得分:2)

这样,刷新后闪光信息就不会反复出现。

<?php session_start();
if(isset($_SESSION['msg']) && $_SESSION['msg'] != ''){
    echo $_SESSION['msg'];
    unset($_SESSION['msg']);
}
?>
    <form action="page2.php" method="post" 
enctype="multipart/form-data" class="form-inline subscribe-form">

                    <input type="name" name="name" placeholder="Jack">
                </div>
                <button type="submit" name="sub" value="sub" >Submit</button>    


           </form>

并且

<?php
session_start();
//include necessary

if(isset($_POST['sub'])) {

    $nameget = mysqli_real_escape_string($dbconnect, $_POST['name']);
    $sqlentry = .....bla bla......//insert into DB
}

$getsql = mysqli_query($dbconnect, $);
  if($getsql){
        mysql_close($dbconnect);
        $_SESSIOM['msg'] = 'Value Inserted or whatever';
        header('location:page1.php');        
    } 
?>