0

In the php script when I echo the $_POST['AllItems '] then it shows only the last selected value instead of a whole string of values or array.

Below is the javascript am using :

$(function () {
    $('#senditems').click(function () {
        var items = $('input[name^="item"]:checked').map(function () {
            return this.value;
        }).get();

*****************************
  //here when i do alert(items); ,
 then it shows comma separated values - 1,22,321
****************************


        $.post("saveitems.php", {
            AllItems: items
        });
    });
});

Form is :

    <form>

    <input type="checkbox" name="items[1]" value="1" />
<input type="checkbox" name="items[22]" value="22" />
<input type="checkbox" name="items[321]"  value="321" />

    </form>
9
  • is AllItems an array of checkboxes ? can you post the code for your form too? Commented Aug 5, 2013 at 13:57
  • @Maximus2012 : dear sir, items is the collection of selected checkboxes , i have added the form code also. Commented Aug 5, 2013 at 13:58
  • @palaѕн : sir , was updating it , done now. Commented Aug 5, 2013 at 14:02
  • Your first part of js code seems to be correct as in this fiddle. Are you getting any error in the console? Try using the .done(function() {...} and .fail(function() {...} to see if there's any error or ajax call succeeded! Commented Aug 5, 2013 at 14:10
  • @billyonecan : no sir , am not , but directly posting it , can you please give it a try in your browser. also i think there could be a problem in comstructing 'items' ? i mean do i have to do this :' var items +=' instead of ' var items = ' Commented Aug 5, 2013 at 14:37

1 Answer 1

2

This is how your form should look.

<form id="my_form">

  <input type="checkbox" name="items[]" value="1" />
  <input type="checkbox" name="items[]" value="22" />
  <input type="checkbox" name="items[]"  value="321" />

</form>

Then in your php, you can iterate through all the $_POST['items']

If you want to use jQuery to post, you can use this, but is not necessary as you can add a method and action to the html in your form:

$('#senditems').on('click', function(){
    var form_data = $('#my_form').serialize();

    $.post({
        url: "your_php.php",
        data: form_data
    }); 
});
Sign up to request clarification or add additional context in comments.

3 Comments

It will be easier to iterate the way I posted it. Otherwise you have to manually check each, if it even works. I've never done it the way you did.
Removing the index will make no difference whatsoever
@billyonecan : am posting the items values through jquery , then in the php script am echoing the values but it shows only one value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.