views:

128

answers:

1

I'm trying to parse the rows in a table that I generate using javascript by adding items to a cart and then create a json object when the user hits save order of all the items and pass it to a php script using $.post in jQuery.

The only trouble I'm having is understanding JSON objects and how to push more items onto the object. I get an error in firebug telling me that devices[i] is undefined. Not really sure how else to accomplish this. I thought it was really just an array.

    function Save()
  {
    var devices = new Object();
    var i = 0;

$("#device_tbl tr:gt(0)").each(function(){
 var manufid = $(this).find("td").eq(0).find(".manuf_id").html();
 var modelid = $(this).find("td").eq(1).find(".model_id").html();
 var condition = $(this).find("td").eq(2).find("select").val();
 var carrier = $(this).find("td").eq(3).find("select").val();
 var imei = $(this).find("td").eq(4).find("input").val();
 var price = $(this).find("td").eq(5).html();
 alert(manufid+"\n"+modelid+"\n"+carrier+"\n"+imei+"\n"+price);

 devices[i].manufid = manufid;
 devices[i].modelid = modelid;
 devices[i].carrier = carrier;
 devices[i].imei = imei;
 devices[i].price = price;

 i++;

 });
document.write(devices);  //just for debugging


$("#final").show();

}

+4  A: 

You currently have devices declared as an object, but you're treating it like an array.

You need to declare it as an array of objects.

  function Save()
  {
    var devices = new Array();
    var i = 0;

$("#device_tbl tr:gt(0)").each(function(){
        var manufid = $(this).find("td").eq(0).find(".manuf_id").html();
        var modelid = $(this).find("td").eq(1).find(".model_id").html();
        var condition = $(this).find("td").eq(2).find("select").val();
        var carrier = $(this).find("td").eq(3).find("select").val();
        var imei = $(this).find("td").eq(4).find("input").val();
        var price = $(this).find("td").eq(5).html();
        alert(manufid+"\n"+modelid+"\n"+carrier+"\n"+imei+"\n"+price);

        devices[i] = new Object();
        devices[i].manufid = manufid;
        devices[i].modelid = modelid;
        devices[i].carrier = carrier;
        devices[i].imei = imei;
        devices[i].price = price;

        i++;

        });
document.write(devices);         //just for debugging


$("#final").show();
}

or something like that.

(Updated to show it in your code)

idrumgood
The "devices[i] = new Object();" part is critical, declaring it as an array is optional
Mike