0

I Would like to remove a variable from a PHP session array when the user chooses 'Remove'.

But when I unset, ALL the variables in the array are deleted.

How I can delete just one variable?

    <?php

session_start();

if (!array_key_exists('products', $_SESSION) || !is_array($_SESSION['products'])) {
    $_SESSION['products'] = [];
}

$productName = array_key_exists('productName', $_POST) ? (string) $_POST['productName'] : '';

if(isset($_GET['delparam'])){
   unset($_SESSION['products'][$productName]);
}
if(isset($_GET['addparam'])){
    $_SESSION['products'][$productName] = $productName;
}
?>

  <?php foreach ($_SESSION['products'] as $product): ?>
    <div class="echo-product"><?php echo htmlspecialchars($product); ?>
     <button type="submit" class="delete-product" value="Remove" >[x]</button>
    </div>
  <?php endforeach;?>

My code is a bit different from the other posts I found on this site. But I really need help with this code...

Update

$('.add-product').click(function() {
    var productName = $(this).data('product');
        $.post('http://examp.nl/reload.php?addparam', {productName: productName}, function(data) {
            $('.echo').html(data);
    })
});

$('.delete-product').click(function() {
    var productName = $(this).data('product');
        $.post('http://examp.nl/reload.php?delparam', {productName: productName}, function(data) {
            $('.echo').html(data);
    })
});

Update If I use the following dump var_dump((unset) $productName); var_dump($productName);

I get this output. Maybe it is of help... NULL string(6) "Item Z"

Steggie
  • 525
  • 8
  • 31

2 Answers2

1

When you create the session use

$_SESSION['user']=array();

The array contains the full data as your more than one values with key and values combinations then remove as follows:

unset($_SESSION['user']['index_name']);
PHP Geek
  • 3,949
  • 1
  • 16
  • 32
0

An altenative would be to define a boolean 'productOption' variable, 0 = remove and 1 = add, then your .delete-product would look lie this:

$('.delete-product').click(function() {
var productName = $(this).data('product');
var option = 0; 
    $.post('http://examp.nl/reload.php?delparam', {productName: productName, productOption: option}, function(data) {
        $('.echo').html(data);
})

Then inside your reload.php script, you verify if the variable isset and then unset the specific session variable you are after.

I've not tried this code yet but please try it and let me know if it works;

EDIT

if you have defined a $_session['product'] variable, then it will hold all the products listed. Think of it as a multidimensional array.

Just Ice
  • 179
  • 1
  • 1
  • 11
  • Where do I get the `[$key]` from? `array(3) { ["Boot X"]=> string(6) "Boot X" ["Boot Y"]=> string(6) "Boot Y" ["Boot Z"]=> string(6) "Boot Z" } ` @Just Ice – Steggie Jul 26 '16 at 12:36
  • Don't I already know what the `$key` should be with this code: `$productName = array_key_exists('productName', $_POST) ? (string) $_POST['productName'] : '';` – Steggie Jul 26 '16 at 12:43
  • So how do I target the specific product name to be removed from the full list of selected products. – Steggie Jul 26 '16 at 12:58