简体   繁体   中英

Php pop up successfull message after redirect to another page

I'm currently working on the online booking system.In the script below, I have build a script which takes all the details and on success redirect it to another page. My question is how do i pop up a successful message on header page.

Here is the script

$statement = $db->prepare("INSERT INTO bookings (customerid, pname,cnumber, paddress, daddress, via, pdate, hours, mins, luggage, vtype, pnum, info) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");
      $statement->bind_param('issssssiiisis', $user_id, $pname, $number, $pickupaddress, $dropaddress, $via, $date, $hours, $minutes, $luggages, $vtype, $passengers, $additional);

      if($statement->execute()){
            header('Location: activebookings.php');
        }
      }else{
      die('Error : ('. $mysqli->errno .') '. $mysqli->error);
      }
      $statement->close();
      }

The easiest solution is to use sessions for that.

if($statement->execute()){
    $_SESSION['message'] = "Your message here";
    header('Location: activebookings.php');
}

(you may need to call session_start() first, if you're not using sessions yet at this point)

Then on your activebookings.php page read it from the session (again, you may need to call session_start() first):

if (isset($_SESSION['message'])) {
    echo '<script type="text/javascript">alert("' . $_SESSION['message'] . '");</script>';
    unset($_SESSION['message']);
}

You'll want to unset the message from the session, to make sure your visitors don't get the popup again if they refresh the page.

you can use a $_SESSION variable before the header, set a session variable called status to 1 or something like that and then on activebookings check for the session variable and if it exists you can echo the javascript needed to popup a modal.

on booking page

$_SESSION['status'] = 1;

on activebooking (where you want popup)

$status = $_SESSION['status']

if($status)
echo '<script>popup();</script>'

Use the echo to display a js alert like this:

echo "<script>alert('Success');</script>";

If you want to put session data into the alert do this just be sure that your message doesn't have any " or ' to not interfere in the syntax

Like this:

echo "</script>alert("'.$_SESSION['your data'].'");</script>";

Hope i have helped!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM