Jump to content

passing a message / variable on a header redirect


goatdog

Recommended Posts

ok i want to know the best way to do this.

Now this is without using forms which would make it easy.

 

let's say there's a directory listing, you view one of the items in the directory and you want to delete it, so you hit the delete button, it deletes the item and redirects you back to the directory listing, but at the top of that listing i want it to show a message to the user saying, the item has been successfully deleted

 

now the only way that i can think of doing this is with sessions, but is that the best way?

set the session in the delete clause

 

if(isset($_REQUEST['delete_item'])){

//delete query here

$_SESSION['message'] = "item successfully deleted";

header("location:directory listing");


//then on the main page

if(isset($_SESSION['message']){

echo $_SESSION['message'] ;

unset($_SESSION['message'] );

 

does that look like a proper way to do it or do you have a better suggestion?

 

thanks

yup, sessions is the way i usually do it. the only other other good option is to pass a key that refers to a message as a GET variable. so, keep a list of messages somewhere in an associative array like this:

<?php
  $messages = array(
    'delete_success' => 'Item successfully deleted',
    'delete_fail' => 'Failed to delete item',
  );
?>

 

then in your redirect:

header("location:directorylisting.php?msg=delete_success");

then on your directory listing you can look up the text for the message.

 

again, i prefer the SESSION option, but this is an alternative

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.