So I want to make a website where when you delete a post it will show an alert msg that the post has been deleted successfully but the problem is that when I am reloading the alert is still there
Can't figure any fix for the issue
if I try to use header('location: ./wb_submission.php');
the echo will not load
code I am trying ->
if (isset($_GET['delete'])) {
$sno = $_GET['delete'];
$sql = "DELETE FROM `website_submit` WHERE `sno` = $sno";
$result = mysqli_query($conn, $sql);
echo '<div class="alert alert-danger" role="alert">
The submission was deleted successfully!
</div>';
}
You should remove delete
parameter from URL after page loaded. You can do it with js:
window.history.replaceState({}, '', location.href.replace(location.search, ""));
window.history.replaceState
{}
The state object is a JavaScript object which is associated with the history entry passed to the replaceState method. The state object can be null.
''
This parameter exists for historical reasons, and cannot be omitted; passing the empty string is traditional, and safe against future changes to the method.
location.href
) is complete URL value, like:
http://example.com/page.php?delete=10
.replace(location.search, "")
location.search
is everything after ? in URL (in this case: ?delete=10
)
If you have multiple parameters in your URL and you want to remove only delete parameter try this code:
function removeDeleteParam(){
// Create new URLSearchParams object
// location.search is something like "?delete=10&msg=hello&ok=true"
let params = new URLSearchParams(location.search);
// Remove delete parameter from URL parameters
params.delete("delete");
// Convert parameters to string
// e.g "msg=hello&ok=true"
let urlParams = params.toString();
// if urlParams is not empty add a question mark before it:
urlParams = urlParams.length > 0 ? "?" + urlParams : "";
// Replace new parameters
window.history.replaceState({}, '', urlParams);
}
removeDeleteParam();
A better approach is to redirect to a new URL directly from server using Location
header, before redirecting you need to remove 'delete' parameter, also for success message you can add another parameter to show a message. Here is the idea:
if( isset( $_GET["delete"] ) ) {
$id = $_GET["delete"];
// Delete actions here, for example:
$success = delete_item( $id );
// You need to get the full URL, e.g: "https://example.com/admin/?delete=10"
$full_url = get_current_url();
// Then you have to remove the 'delete' parameter
$new_url = remove_url_param( $full_url, "delete" );
// Now add another parameter for deletion state (success or failure)
if( $success ){
$new_url = add_url_param( $new_url, "delete-success" );
}
else{
$new_url = add_url_param( $new_url, "delete-failure" );
}
// Now redirect it
header( "Location: " . $new_url );
}
Also for more security, you should consider casting the ID to integer, sanitize the URLs, etc. See the full example:
functions.php
:
function delete_item( $id ){
// Be careful, id is already a number here, but can be used for SQL injection
// Always use mysqli_prepare before executing it
// Check if it's really a number and it's positive
$id = intval( $id );
if( $id <= 0 )
return false;
$sql = "DELETE FROM `website_submit` WHERE `sno` = $id";
// Execute the query
// $result = mysqli_query( $conn, $sql );
// Check here if it was successful and return it
// return $success;
// Just for test, a random state
return rand( 1, 9999 ) % 2 == 0;
}
function get_current_url() {
// Use HTTPS if set and not off
$scheme = ( !empty( $_SERVER["HTTPS"] ) && $_SERVER["HTTPS"] !== "off" ) ? "https" : "http";
// Get host and port
$host = $_SERVER["HTTP_HOST"];
// Get full request URI (path + query string)
$request_uri = $_SERVER["REQUEST_URI"];
// Build and sanitize
$url = $scheme . "://" . $host . $request_uri;
// Sanitize (remove invalid characters) to get a safe URL
return filter_var( $url, FILTER_SANITIZE_URL );
}
function build_url( $parts ) {
// HTTP or HTTPS
$url = isset( $parts["scheme"] ) ? "{$parts['scheme']}://" : '';
// Host URL
$url .= $parts['host'] ?? '';
// URL path
$url .= $parts['path'] ?? '';
// Query parameters
$url .= isset( $parts["query"] ) ? "?{$parts['query']}" : '';
return $url;
}
function add_url_param( $url, $key, $value ) {
$parts = parse_url( $url );
// Parse query string if exists
$query = [];
if( !empty( $parts["query"] ) ) {
parse_str( $parts["query"], $query );
}
// Add or update the key
$query[$key] = $value;
// Rebuild query string
$parts["query"] = http_build_query( $query );
// Reconstruct URL
return build_url( $parts );
}
page.php
:
if( isset( $_GET["delete"] ) ){
// Accepts only numbers
$id = intval( $_GET["delete"] );
// Check if it was deleted
$success = delete_item( $id );
// Get the full URL
$full_url = get_current_url();
// Remove 'delete' param
$new_url = remove_url_param( $full_url, "delete" );
// Add 'delete-success' or 'delete-failed'
$new_url = add_url_param( $new_url, $success ? "delete-success" : "delete-failed", $id );
// Redirect
header( "Location: $new_url" );
}
if( !empty( $_GET["delete-success"] ) ){
$id = intval( $_GET["delete-success"] );
echo "Item $id was deleted.";
}
else if( !empty( $_GET["delete-failed"] ) ){
$id = intval( $_GET["delete-failed"] );
echo "Could not delete $id item.";
}