I want the page to display submission successful and then return to the same page that its on
I want the page to display submission successful and then return to the same page that its on
Here is my php code I just need to have the page say submit successful after the user clicks submit and then reload the same page that it was just on. Everything is working the way i want it to right now it just is not displaying the submit successful message before it reloads the page. If you could help me out that would be great. Thank you.
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "nick", "ramon", "lockout");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$lock_number = mysqli_real_escape_string($link, $_REQUEST['lock_number']);
$equipment_number = mysqli_real_escape_string($link,
$_REQUEST['equipment_number']);
$work_order = mysqli_real_escape_string($link, $_REQUEST['work_order']);
$date_out = mysqli_real_escape_string($link, $_REQUEST['date_out']);
$supervisor_out = mysqli_real_escape_string($link,
$_REQUEST['supervisor_out']);
$comments_out = mysqli_real_escape_string($link, $_REQUEST['comments_out']);
$date_in = mysqli_real_escape_string($link, $_REQUEST['date_in']);
$supervisor_in = mysqli_real_escape_string($link,
$_REQUEST['supervisor_in']);
// attempt insert query execution
$sql = "INSERT INTO form (lock_number, equipment_number, work_order,
date_out, supervisor_out, comments_out, date_in, supervisor_in) VALUES
('$lock_number','$equipment_number', '$work_order', '$date_out',
'$supervisor_out', '$comments_out', '$date_in', '$supervisor_in')";
if(mysqli_query($link, $sql)){
echo "Submit Successful";
sleep(1);
header("Location: {$_SERVER['HTTP_REFERER']}");
exit;
}
else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
?>
2 Answers
2
Please store your success message in session variable and get session variable on your main page. please make sure session_start() function in both files.
session_start();
if(mysqli_query($link, $sql)){
$_SESSION['successMSG'] = "Submit Successful";
sleep(1);
header("Location: {$_SERVER['HTTP_REFERER']}");
exit;
}
On your main page get session variable and echo like below
session_start()
if(isset($_SESSION['successMSG'])){
echo $_SESSION['successMSG']
}
You should learn about HTTP headers to fully understand this, but basically you send headers first, and only then your output - so it's not possible to do what you want with redirection via header. Instead, you can use JavaScript or HTML to do that:
Remove these lines:
sleep(1);
header("Location: {$_SERVER['HTTP_REFERER']}");
And replace it with:
echo "<meta http-equiv='refresh' content='1'>";
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Comments
Post a Comment