0

Possible Duplicate:
Using jQuery to compare two arrays

I have to send the form data via ajax to server on say every 30 second. Now I want it to be checked at client end and ajax been hit only in case there is any change in form. I am using:

var lastData = "";
function saveFormInDB(){
    var $form = $("#fb_divBl0ck1").closest('form');
    var isSame = false;
    data = $form.serializeArray();
    if(lastData == ""){
        lastData = data;
    }else{
        if(lastData == data){
            isSame = true;
        }
        lastData = data;
    }
    if(!isSame){
        jqxhr = $.post("/SPSKMEForm/FormSaveServlet", data)
        .success(function() {
            if(jqxhr.responseText != ""){
                alert(jqxhr.responseText);
                alert($("#fixedFooter"));
                alert($("#fixedFooter").html());
                $("#fixedFooter").text(jqxhr.responseText).css("display", "");
                $("#fixedFooter").fadeOut("slow");
            }
        });
    }
}

but isSame is coming to be false always.

halfer
  • 19,824
  • 17
  • 99
  • 186
Varun
  • 5,001
  • 12
  • 54
  • 85

2 Answers2

1

That is because your variable lastData is considered as a String whilst data is an Object. So your check will always fail and you will always have isSame = false.

EDIT:

The .serializeArray() method creates a JavaScript array of objects, ready to be encoded as a JSON string.

Bogdan Emil Mariesan
  • 5,529
  • 2
  • 33
  • 57
  • i dont think so... m assigning data to lastData.. so it will be an object also `var` can be used for all the stuff(i guess). – Varun Apr 20 '12 at 04:27
  • just check your code Varun, you will do the check and if it is true you will say isSame = true and no matter what the result of the check you will always assign lastData to data, but isSame will always be false. – Bogdan Emil Mariesan Apr 20 '12 at 04:28
  • +1: The .serializeArray() method creates a JavaScript array of objects, ready to be encoded as a JSON string. - http://api.jquery.com/serializeArray/ – Ryan Fernandes Apr 20 '12 at 04:29
  • I know there is some issue but i doubt the reason `lastData` been given as `string` will be issue... though not sure – Varun Apr 20 '12 at 04:30
  • so what will be the best solution to check it? – Varun Apr 20 '12 at 04:31
  • 2
    @Varun: http://stackoverflow.com/questions/1773069/using-jquery-to-compare-two-arrays - consider yourself spoonfed :) – Ryan Fernandes Apr 20 '12 at 04:31
1

I would approach this problem differently:

In your form input tags add an attribute containing the saved (original) value:

<input type="text" name="name" value="" original="Brian">

Now in your jQuery, check if the values match:

if($('[name="name"]').attr('value') == $('[name="name"]').attr('original')) {
  //perform .post here
  //post success sets attr original to posted value
 }

Each time you submit data, make sure to update the "original" attribute.

Brian P Johnson
  • 703
  • 6
  • 17
  • will it increase efficiency? wont it be better to simply put a loop on global `data` variable and check its value – Varun Apr 29 '12 at 16:35