How do I get my session to be unset?

How do I get my session to be unset?

My form looks like this:

foreach ($joined as $i => $qty){
echo "<form action='' method='get' name='yourForm'>";
echo "<button type='submit' name='remove_" . $i . "' value='remove_" . $i . "' class='deletebtn' >X</button>";
echo "</form>";
}

My SESSION is defined like this:

for($i=0; $i<=5; $i++){
if (isset($_REQUEST["element_id_$i"]) ) {
$_SESSION["element_id_$i"] = $_REQUEST["element_id_$i"];
$id = $_SESSION["element_id_$i"];	
array_push($_SESSION["element_id"],$id);
}
$id = $_SESSION["element_id"];
}

and the form submits to:

if (isset($_REQUEST["remove_$i"]) ){

unset($_SESSION["quantity[$i]"]);
unset($_SESSION["element_id[$i]"]);
var_dump($_SESSION["element_id"]);
var_dump($_SESSION["quantity"]);
echo "Received variable " . $_REQUEST["remove_$i"];
echo 'TARGET INDEX TO BE REMOVED: ' . $_SESSION["element_id[$i]"] . '<br><br>';
}

The output is:

array(1) { [0]=> string(1) “1” } array(1) { [0]=> string(1) “1” } Received variable remove_1
Notice: Undefined index: element_id[1] in C:\xampp\htdocs\TopView\cart5.php on line 147
TARGET INDEX TO BE REMOVED:

The code I posted (which you now posted here in a new thread?) was working just fine to remove the index from the $_SESSION array.

now your snippet where you create your $_SESSION data makes no sense?

You need to understand the difference between a $_SESSION variable… and a $_SESSION array…

You are using both…

defining a session variable (incrementally)… then adding that same value to a session array… why? Whats the purpose?

for($i=0; $i<=5; $i++){
	if (isset($_REQUEST["element_id_$i"]) ) {
		//saving to a session variable named:  $_SESSION['element_id_0'], then: $_SESSION['element_id_1'], then: $_SESSION['element_id_2']....etc..  this is NOT an array  (pick an approach and stick to it)
		$_SESSION["element_id_$i"] = $_REQUEST["element_id_$i"];
		
		//now you are attempting to save data to a session variable -array- named $_SESSION['element_id'].. which would be accessed like:
		//$_SESSION["element_id"][0], $_SESSION["element_id"][1], $_SESSION["element_id"][2]...etc..etcc
		$id = $_SESSION["element_id_$i"];	
		array_push($_SESSION["element_id"],$id);
		
		//so in summary:  $_SESSION['element_id_0']  !=  $_SESSION['element_id'[0]
	}
	$id = $_SESSION["element_id"]; //not even sure what this is supposed to do?  or why you are re-using the same variable name?
}

for your last snippet you cant remove/unset something… and then try to display it (when nothing is there)

Thank you @whispers for all of your input

Sponsor our Newsletter | Privacy Policy | Terms of Service