-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
  • 681
  • 2
  • 12
  • 39
  • 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,409
  • 13
  • 21
  • thanks for your responds i going to do this one ... – Naresh Jul 31 '13 at 10:02
  • why i'm getting this error ReferenceError: isset is not defined if(isset($_POST['options']) && !empty($_POST['options'])) – Naresh Jul 31 '13 at 11:53
  • Try if($_POST['options'] && !empty($_POST['options'])) – cartina Jul 31 '13 at 12:12
  • $_POST is not defined i am getting this – Naresh Jul 31 '13 at 12:19
  • ok. Can you please print the output of this statement here after posting the form: print_r($_REQUEST); Also please add name to submit button like I did in my answer – cartina Jul 31 '13 at 12:23
  • yeah i tried like that also still i'm getting same error message – Naresh Jul 31 '13 at 12:29
  • @Jhon What is the output of print_r($_REQUEST); ? – cartina Jul 31 '13 at 12:29
  • thanks for every thing @ cartina sorry nothing doing that code i have same error $_POST is not defined if($_POST['data'] && !empty($_POST['data'])) – Naresh Jul 31 '13 at 12:32
  • Put print_r($_REQUEST); above everything else at top of the page and paste the output here. To remove the error you mentioned, all form input elements should have name attribute. – cartina Jul 31 '13 at 12:36
  • yeah got it i have the problem here $products=array; if i replace this with $options = Array(); its working for me – Naresh Jul 31 '13 at 12:40
  • Sorry. My mistake. It should be $products=array(); That is the correct way to define an array. – cartina Jul 31 '13 at 12:41
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,620
  • 1
  • 17
  • 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