7

Here is the code. I want it this way --

Form submission --> page2.php --> redirect --> page1.php (Here is the message. Pop-up or whatever)

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');

}



?>
sofa_maniac
  • 1,659
  • 2
  • 12
  • 21

2 Answers2

12

Where you have:

header('location:page1.php');

append a variable on the location, like:

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

And on page1.php, do something like:

if( $_GET['status'] == 'success'):
    echo 'feedback message goes here';
endif;
pbs
  • 248
  • 3
  • 9
  • This is fine. It worked, thanks. But when I refresh the "page1.php?status=success" page, is there any way to head to "page1.php"? – sofa_maniac Jul 25 '15 at 17:43
  • Sure, depends on what you want.. maybe store the status in a session, or maybe just do a simple redirect with javascript, or display the feedback in a alert or modal window, that links to page1.php.. – pbs Jul 25 '15 at 17:53
3

This way your flash message will not show up again and again after refresh.

<?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>

And

<?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');        
    } 
?>
Rimble
  • 873
  • 8
  • 22
Tarun Upadhyay
  • 724
  • 2
  • 7
  • 16