Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a success message after PHP form submission?

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

}



?>
like image 862
sofa_maniac Avatar asked Jul 25 '15 16:07

sofa_maniac


People also ask

How do you display success message after submitting a submission?

Display Success Message After Form Submit- MetForm # At first, click any old page or post or create new. After that, create a new form or add any existing form to it. Just enable the Response Message Type to Sweet Alert while editing or creating a form with MetForm and you are done.

How show success message from another page in PHP?

The best way to solve this problem is set a session message after the success of your operation in the process page. Then in the redirected page check whether the session message is set or not. If it is set then simply echo that message. The below code may help you.

How do you get the success message in the same page after submitting the contact form?

If you want the same form page displayed again after submission with results, you will make the action the same page and include the processing script at the beginning of the file. Instead of echoing the result message within the processor as you have, store it as a variable which can be output in the html form page.


2 Answers

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;
like image 66
pbs Avatar answered Sep 19 '22 18:09

pbs


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');        
    } 
?>
like image 28
Tarun Upadhyay Avatar answered Sep 16 '22 18:09

Tarun Upadhyay