How do I pass a 2D array from JavaScript into PHP -
i working on school project involves extracting product data multiple online retailers using various apis/ajax calls, , sorting data in php (i use 1 ajax call each retailer). snippet of code working on shown below. cannot figure out how
push temporary array containing product attributes ("average", "price", "name", "url", , "image") each product master array (array of arrays) ,
post master array php in such way can index values in sorting purposes.
function get_results() { $(document).ready(function() { var master_array = []; $.ajax({ type: "get", url: "http//:www.source1.com", datatype: "xml", success: function(xml) { $(xml).find('product').each(function() { var average = $(this).find('average').text(); var price = $(this).find('price').text(); var name = $(this).find('name').text(); var url = $(this).find('url').text(); var image = $(this).find('image').text(); master_array.push([average, price, name, url, image]); }); } }); $.ajax({ type: "get", url: "http//:www.source2.com", datatype: "xml", success: function(xml) { $(xml).find('product').each(function() { var average = $(this).find('average').text(); var price = $(this).find('price').text(); var name = $(this).find('name').text(); var url = $(this).find('url').text(); var image = $(this).find('image').text(); master_array.push([average, price, name, url, image]); }); } }); }); }
you should see example code jquery ajax function: http://api.jquery.com/jquery.ajax/. here example code:
$.ajax({ method: "post", url: "some.php", data: { name: "john", location: "boston" } }) .done(function( msg ) { alert( "data saved: " + msg ); });
your updated function can this:
function get_results() { $(document).ready(function() { var master_array = []; $(xml).find('product').each(function() { var average = $(this).find('average').text(); var price = $(this).find('price').text(); var name = $(this).find('name').text(); var url = $(this).find('url').text(); var image = $(this).find('image').text(); master_array.push([average, price, name, url, image]); }); $.ajax({ type: "post", url: "http//:www.source1.com", data: master_array, success: function(response) { alert('data posted'); }, fail: function(response) { alert('data not posted'); } }); }); }
in above code success , fail functions called when servers returns response. if response correctly sent success function called. if there error on server fail function called.
Comments
Post a Comment