i'm new to php and got a problem with php session. i have a form and when the user press submit button, i want to show an alert, then redirect it to the empty form page. but my code doesn't work:
session_start();
if (mysqli_query($connection, $sql)) {
if (isset($_SESSION['id'])) {
echo "Records added successfully.";
header("Location: index.php");
exit();
} else {
echo "ERROR: Could not able to execute $sql." . mysqli_error($connection));
header("Location: index.php");
}
Alert is a javascript function.
If you want to show a Javascript aler you can use this code:
echo '<script>alert("Records added successfully.")</script>';
But in you case if you will show alert page redirect will not work.
If you want to show a success message on index.php
page then store that message into session like $_SESSION['message']
.
On index.php
page check if session contains message then show message and remove it from session, so it can it not appear again. e.g:
if(isset($_SESSION['message'])){
echo $_SESSION['message'];
unset($_SESSION['message']);
}