0
var items=[];

items.push(data);

I am using an array to store my data, and then submit using ajax. For the first time submission, it works fine. But when submit subsequently, it will submit null value together with data passing to server.

I am using the method below to clear the array, but i found that the previous data just set to null. For example, for the first time i passed value 1, successful. For the second submission, i clear the array and store new value, e.g. i pushed value 2 to the items[], when submit it will submit null and 2.

items.splice(0);

How can i fix it? I need to reset the array every time submit the value.

Code:

var items=[];
$("#selection").click(function(e) {
       $(":checkbox").each(function() {
            if(this.checked === false) {
                this.checked = true;
                items.push(this.id);
            }
        });
});

$('#grid').submit(function(e){
            e.preventDefault();
            var formdata = {
                data:items
            };
            $.ajax({
                type: "POST",
                url: "/add",
                data: formdata ,
                dataType:'html',
                success: function(data) {
                    alert("added");
                }
            });
        });
7
  • 1
    add all relevatn code Commented Jul 7, 2017 at 3:31
  • Would be helpful to see relevant code as guradio mentioned but what's stopping you from reassigning the variable? Commented Jul 7, 2017 at 3:32
  • 2
    items.length = 0 or just reassign a new, empty array items = []. (Regardless, this isn't a jQuery problem, it's a plain JavaScript thing.) Commented Jul 7, 2017 at 3:35
  • I think the problem occurred due to pagination ;( Commented Jul 7, 2017 at 3:52
  • 1
    Your existing code that calls items.push() from inside a click handler seems a bit strange - wouldn't that mean that the same values get added to the array multiple times if the user clicks that element multiple times before submitting? Commented Jul 7, 2017 at 3:56

2 Answers 2

2

You simply set the array to a empty array after the ajax

success: function(data) {
    alert("added");
    items=[];
}

or better empty it at each click

$("#selection").click(function(e) {
items=[];
// rest of the code
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to reset all array till leave it empty you can do this :

var array = [1,2,3,4,5,6];
array.length = 0;

it that way you can reset your array

Updated

when you use :

array.splice(0) // this will delete items from index that you passed, only if you passed the first parameters

Hope it helps you.

Thank you for the note @nnnnnn

2 Comments

"this will clone the entire array" - The .splice() method doesn't clone, it modifies, and .splice(0) does empty an array.
@nnnnnn OMG your right I got confused with array.slice(0) method that will clone the entire array, sorry I will update the answer thank you.

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.