1

I've created an array from a PHP session variable, and now I'm trying to use ajax (within jQuery) to remove an element from the array.
I've got the following code so far:

$val = $_SESSION['enquiry-basket'];
$array = explode($val);

foreach ($enquiries as $a => $q) {
    if ($q == $_POST['product_id']) {
        unset($array[$a]);
    }
}

The only problem is, it doesn't remove the item.
Can anyone explain why, and tell me how to fix it?

Edit

Sorry guys. The reason I mentioned jQuery is because I use a jQuery ajax call to process the PHP I displayed above.
The ajax query runs fine because it processes some javascript goodies (remove's a div from the HTML) once the ajax returns a success.
I've added the delimiter (can't believe I missed it) but the element doesn't get removed from the array still.
I've never been good at multi-dimensional arrays, so here's the array printed:

Array ( [0] => 6 [1] => 8 ) 

It looks right to me, but I'm an amateur in arrays. (6 and 8 are of course my strings I inserted)

4
  • 1
    what does jQuery has to do with this? Commented May 4, 2012 at 13:34
  • 1
    1. This is not javascript/jQuery. 2. You use explode but you don't say how to explode it. Use it like this explode('-',$val); to split on - Commented May 4, 2012 at 13:35
  • 1
    Can you please post your jquery code, so that we can see the variables being sent? Commented May 4, 2012 at 13:35
  • I'm so sorry. I was in a hurry when writing the question so completely forgot about the jQuery half way through. The reason I mentioned jQuery is because I'm calling an ajax query to the PHP file I displayed above. The ajax is all working ok because I make a div disappear when the ajax query suceeds, and the div disappears as it should. Commented May 4, 2012 at 13:51

5 Answers 5

8

explode is missing the first argument:

explode(',', $val);
Sign up to request clarification or add additional context in comments.

2 Comments

This is most likely the reason, such an easy thing to overlook, great catch!
I can't believe I missed that! Unfortunately, with the delimiter there the element still isn't removed from the array.
2

You are removing item from $array, not from $_SESSION['enquiry-basket'].

3 Comments

I felt stupid when I forgot to include the delimiter.. And now I just feel plain dumb. Thank you! (To everyone)
@RichardHedges I am not sure why was the above answer accepted,though the issue is different.
All the answers I received contributed to fixing my problem. After adding the delimiter again I still had the problem of the item not being removed. This is the answer that told me I wasn't removing the item from the session variable itself, so the items in question wouldn't change. If I could, I'd accept all the answers.
1

The explode function should have two parameters. But you given only the name of the array. explode(separator,string,limit);

Comments

1

If I understand correctly what you are trying to do, the problem is that JQuery runs client side, which means that your PHP arrays on the server side disappear between each request from Ajax. The only array that remains is $_SESSION.

If you want to use AJAX, you need to remove from $_SESSION directly. Anything else is just useless because the arrays and variables "disappear" between each call.

Comments

1

Mostly an issue with the explode function, the second parameter is missing:

Change from:

$array = explode($val);

To:

$array = explode('~',$val);  // ~ is a delimiter

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.