Search code examples
phpvariablessessionalertstatus

PHP sessions and session destroy


I have created a page where 'Job's' stored on a database are deleted. On a page called

delete.php 

the job to be deleted is selected. The user is then directed to deleteaction.php where the job is deleted. The user is then auto redirected back to

delete.php

This all works fine however once the user is returned to delte.php I would like a pop-up/ alert saying 'Job deleted'. However if the user enters the page not from

deleteaction.php

then I dont want this pop-up to appear. I have tried to use sessions where a variable $status indicates if the user has just been directed to

deleteaction.php

and a job has been deleted.

Code on deleteaction.php:

session_start();

 $id=$_GET['id'];

$sql= "DELETE FROM `Job` WHERE `Job`.`Job_Customer_id`='". $id."';";

 $stmt=$dbh->query($sql);

 $status = "deleted";
$_SESSION['delstat'] = $status;
 header("Location:delete.php");

Code from delete.php:

session_start();

$status = $_SESSION['delstat']; 

if ($status = "deleted"){
    echo '<script language="javascript">';
    echo 'alert("Job Deleted")';
    echo '</script>';
}
else {
    echo "No";
}

session_destroy();

........

The problem is the page delete.php always displays the alert that a job has been deleted every time the page is visited. Not sure if its something wrong with my loop or the session use? Thanks in advance!


Solution

  • You're presently assigning = instead of comparing == in

    if ($status = "deleted")
    

    being always TRUE

    change that to

    if ($status == "deleted")