-1
checkdata['attendance_batch'] = [
    {user_id_c: 'abcda', ispresent: 0},
    {user_id_c: 'abxcda', ispresent: 2},
    {user_id_c: 'abctda', ispresent: 1}
]

I want my array in the above mentioned format in javascript

MY HTML:

<label for='flip'>" + entry["name"] + "</label><div id='switch'><select name='user_id_c' id='flip2b' iduser='"+entry["id"]+"' data-role='slider'><option value='1'>Present</option><option value='0'>Absent</option></select>


$("#frmattendance").submit(function(event) {


event.preventDefault();
$form = $(this);

var a = {};

var paramString = [];
$($form).find(':input').each(function() {

    //console.log($(this).attr('name'));
    if ($(this).val() == 'Mark') {
    }

    else {

        a[{user_id_c: $(this).attr('iduser'), ispresent: $(this).val()}];
        //  console.log(a[$(this).attr('name')]);
    }

});

 var $sessiondata = sessionStorage.sessionid;


$.ajax
        ({
            type: "POST",
            url: saverecordurl + 'pcc_attendance',
            dataType: 'json',
            async: false,
            // session_id: $sessiondata,
            //json object to sent to the authentication url
            data: {checkdata: a, session_id: $sessiondata},
            success: function(response) {
                //console.log(response);
                if (response.id !== null)
                {

                    alert("You have successfully marked the attendance");

                    $.mobile.changePage("#eventdetails", {
                        transition: "slide",
                        reverse: true,
                        changeHash: true
                    });

                }

            },
            error: function(result) {
                $.mobile.changePage("#one", {
                    transition: "slide",
                    reverse: false,
                    changeHash: true
                });
            }

        })

});

Could someone help me with the format am i making a mistake?

6
  • What is the HTML doing there? Commented Oct 7, 2014 at 19:24
  • getting data from html and forming the array Commented Oct 7, 2014 at 19:26
  • 1
    That is an array of objects, not a two-dimensional array. I am not quite sure what you are asking though. When you say you want to "submit" this data, do you mean like in a POST? Commented Oct 7, 2014 at 19:26
  • No, I mean <label for='flip'>" + entry["name"] + "</label> produces a label with " + entry["name"] + " as the label text, for example. The JavaScript here isn't in a <script> tag so it'll never execute. Commented Oct 7, 2014 at 19:28
  • @MikeBrant Please chk my edited code Commented Oct 7, 2014 at 19:31

1 Answer 1

1

a[{user_id_c: $(this).attr('iduser'), ispresent: $(this).val()}]; is invalid syntax. You could do one of the following:

var a = {
   user_id_c: $(this).attr('iduser'),
   ispresent: $(this).val()
}

or

var a = {}
...
a.user_id_c = $(this).attr('iduser');
a.ispresent = $(this).val()
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks but woudn't it just append the last value and not append all of them. i have multiple elements with same name on my form to mark attendance
checkdata.attendance_batch.push(a); would add them to your checkdata array
attendance_batch.push(a); i did this it seems to be ovveriting my data
it looks like you are only passing the last a defined, rather than the collection of them (data: {checkdata: a, session_id: $sessiondata},), initialize the attendance_batch array prior to your input.each, then pass the attendance_batch var in your post call

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.