views:

66

answers:

2
var subtbl = $(this).attr('data-counter');
                 var stunbr = $(this).attr('data-stunbr');
                var m = 0;
                var checkedlines = new Array();
            $.each($("#sub-table"+subtbl+"  "+"input:checked"),function (m){            
            var chk_value = $("#chk_"+stunbr+"_"+m).attr('value');
                var txt_value = $("#txt_"+stunbr+"_"+m).attr('value');
              //alert("Txt value --->"+ txt_value +" Chk Value --->"+ chk_value);           
            checkedlines[m] = { STUNBR: stunbr, SNO: chk_value, NAME: txt_value }; 
                m++;          
        });

How do iattach the "checkedlines" to form request in jquery and send it to php .I mean do we need to use $.ajax can you show in attaching "checkedlines[]" Can we produce this array n Json any method available in Jquery to produce JSON How can i consume that in php if it is a plain array .One more thin How do i Iterate through the "checkedlines[]" array outside of the each loop without using the "var r=0 " variable

    var r=0;
    $.each(checkedlines,function(r){
        alert("ASNURNNBR==>"+checkedlines[r]['STUNBR']+"SEQNO==>"+checkedlines[r]['SNO']+"RCVDATY==>"+checkedlines[r]['NAME']);
        r++;
    });
+1  A: 

One option would be to serialize the object into a JSON string and write the result into a hidden field inside of a form the user can submit.

<form>

 ...

<input id="json_array" type="hidden" name="json_array" />

 ...

</form>


<script type="text/javascript">

function stringify(obj) {
    var t = typeof (obj);
    if (t != "object" || obj === null) {
        // simple data type
        if (t == "string") obj = '"'+obj+'"';
        return String(obj);
    }
    else {
        // recurse array or object
        var n, v, json = [], arr = (obj && obj.constructor == Array);
        for (n in obj) {
            v = obj[n]; t = typeof(v);
            if (t == "string") v = '"'+v+'"';
            else if (t == "object" && v !== null) v = stringify(v);
            json.push((arr ? "" : '"' + n + '":') + String(v));
        }
        return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
    }
};

var checkedLines = {};

//
// Your code populating the checkLines object goes here
//

var jsonResult = stringify(checkedlines);

$('#json_array').val(jsonResult);

</script>

The stringify() function is taken from the JSON.org library.

If you don't want to use a form then yes, $.ajax(); is a way to go. Simply add jsonResult to the data property with a relevant variable name:

$.ajax({
  method: "POST",
  url: 'http://example.com',
  data: "json_array=" + jsonResult,
  success: function(data) {
    $('.result').html(data);
    alert('Load was performed.');
  }
});
Saul
@Saul:Iam getting error "JSon is undefined" in IE6
Someone
@Someone: Oops, it was a typo on my part. Give that function a new try.
Saul
@saul: Do i need to download from the JSON and put it over in my source
Someone
@Someone: If you use the updated version of the function then no.
Saul
@Saul :Updated version of which function one stringify(). I dont have any
Someone
@Someone: Take a look at the answer. It contains a function called stringify(). You can use that one.
Saul
@Saul :Is this your written custom Function or taken from Json.org
Someone
@Someone: It's taken from JSON.org.
Saul
+1  A: 

ok so here is what you can do. I created an example to show you how it works. It is actualy not using the serializeArray, but the JSON object:

///// if using IE6, you will have to include the following js file into the header of your html document : http://json.org/json2.js /////

// JAVASCRIPT $(document).ready(function() { var names = ["John", "Mike", "Joe"]; var positions = ["flash", "php", "javascript"];

var arr = new Array();
for (var i = 0; i < positions.length; i++){
    arr.push({"firstname": names[i], "position": positions[i] });
};

var json = JSON.stringify(arr);

$.ajax({
  url: "data.php",
  method:"GET",
  data: "json="+json,
   success: function(data){alert("data: "+data);},
  dataType: "text"
});

});

// DATA.PHP $decoded = json_decode($_GET['json']); echo $decoded[0]->firstname;

that works!

Erwan
@Erwan: when i say checkedlines[m].serializeArray(); it is giving me error "Saying object doesnot support this property or method .And i also said var items = checkedlines.serializeArray(); It is alos giving the same error
Someone
@Erwan:It gives a error saying "JSON" is undefined
Someone
@Erwan: Iam using IE6
Someone
under ie6, you have to include the file http://json.org/json2.js
Erwan