-3

I've got this problem that I can't solve. Partly because I can't explain it with the right terms. I'm new to this so sorry for this clumsy question.

Below you can see an overview of my goal.

i have the products along with check boxes.if i checked that check box that index value should store in new array(should perform push() operation here),if in case again unchecked that check box means it should perform pop() operation.

<form method="post">
<input type="checkbox" name="options[]" value="Politics"/> Politics<br/>
<input type="checkbox" name="options[]" value="Movies"/> Movies<br/>
<input type="checkbox" name="options[]" value="World "/> World<br/>
<input type="submit" value="Go!" />

may be this one is duplicate question for give me sorry

any ideas ?

Naresh
  • 671
  • 2
  • 12
  • 37
  • 3
    and where is your related HTML code... please post that too.. – bipen Jul 31 '13 at 09:40
  • 1
    And the _new array_ is going to be defined where exactly? Is there anything you have tried? – dbf Jul 31 '13 at 09:44
  • Sorry for this actually this is not my goal this the heart of my task.... :) – Naresh Jul 31 '13 at 09:45
  • @dbf i'm very new to this technology up now i didn't try any thing ? – Naresh Jul 31 '13 at 09:46
  • 1
    Being new to _a_ technology is not something you should use as an excuse in every question you ask here. You've mentioned it multiple times on all other questions you've asked so far on SO. If you do not try, how can you learn? – dbf Jul 31 '13 at 09:50
  • Yes, this is a duplicate post, Please check fiddle http://jsfiddle.net/MYjRf/ – Nitin Chaurasia Jul 31 '13 at 09:52
  • @dbf yeah boss i tried so many time with different different technologies like JS,Jquery but no use thats why i mentioned like that – Naresh Jul 31 '13 at 09:55
  • Actually my task is similar to this http://istockphp.com/demo/deleting-multiple-records/?action=0 – Naresh Jul 31 '13 at 09:58
  • @Nitin Chaurasia Thanks boss i don't have any idea about this based on your link i can do that.... – Naresh Jul 31 '13 at 10:00
  • @Nitin Chaurasia this one is not working for me – Naresh Jul 31 '13 at 11:17

2 Answers2

0

The products will be posted from form in $_POST['options'] array. Try this.

  $products=array();

    if(isset($_POST['submit'])
    {
    if(isset($_POST['options']) && !empty($_POST['options']))
    {
      $products=$_POST['options'];

       print_r($products);

    }

} The array $products will be overwritten everytime form is posted. There is no need for push or pop operations here if I understand your problem correctly.

<form method="post">
<input type="checkbox" name="options[]" value="Politics"/> Politics<br/>
<input type="checkbox" name="options[]" value="Movies"/> Movies<br/>
<input type="checkbox" name="options[]" value="World "/> World<br/>
<input type="submit" name="submit" value="Go!" />
cartina
  • 1,403
  • 12
  • 20
0

You're using check-boxes, so you can't just use pop, since they might be unchecked in a different order.

So maybe use jQuery to store a link to the JavaScript object on your DOM check-box object using .data() That way you can match the object later using a for loop and use .splice() to remove it from the array.

EDIT 1: So you want to validate the state of the check-box?

if (document.getElementById('mycheckbox').checked){
          myarray.push(data);
}
Grallen
  • 1,550
  • 1
  • 15
  • 16
  • Thanks for your information @Grallen Actually what i'm saying if i checked the check box then only it should store in an array or else no need... – Naresh Jul 31 '13 at 09:51
  • I think I understand. Added "Edit 1:" to answer. – Grallen Jul 31 '13 at 10:04