My ajax script below, takes json_encoded values from php script. The PHP script contains single value and also arrays. For single values I've no issue as I use the first loop to loop through them. For array values, I've no idea on how to loop through.
As far as i know, comma separated values can be put into array by split(',').But for my case, it doesn't output anything. Where's my mistake in the second for loop?
My full script:
$("#receipt").on("click",function()
{
var ele = $(".header_tbl tbody tr").children().length;
if(ele !=0)
{
$("#after_cash_cart_form").submit(function(){
var data = {
"action": "test"
};
data = $(this).serialize() + "&" + $.param(data);
$.ajax({
type: "POST",
dataType: "json",
url: "submit_cart.php",
data: data,
success: function(data) {
for(var i=0;i < data.length; i++)
{
//alert(data[i].price.length);//outputs '3'
//data[i].price; //outputs 120,200,150
var array = data[i].price.split(",");
for (var j=0;j < array.length; j++)
{
alert(array[j]);//doesn't output anything
}
}
}
});
return false;
});
}else
{
alert("Your cart is empty.");
}
});
data[i].priceoutputs120,200,150, thendata[i].price.lengthshould not be2.120, 200, 150in an alert, and it's length is3, it's an arraytypeof data[i].priceto find out