Nathan White
Nathan White

Reputation: 75

Best method of passing large array to PHP

I am trying to find the best method for passing a large array of IDs from one page to the next.

I've built a simple downloadable image site that allows users to select any number of images to download. Right now, when a user selects an image its ID is stored in a JSON string inside a cookie. The problem I've been having is finding the best way to pass the JSON to the review before downloading page.

Currently, I'm sending the JSON as a URL parameter but I'm not sure if this is the smartest solution since the number of IDs in the JSON could reach into the hundreds.

I've looked in PHP sessions but I don't really understand how can I enable the user ability to add/subtract from the variable once the page has been loaded.

$(function(){
$('.download_cart').click(function(){
    var urlArray = [];
    obj = JSON.parse(getCookie("downloads"));
        if(obj != null){
            if(obj.items.length !== 0){
                $.each( obj.items, function( i, value ) {
                    urlArray.push(obj.items[i].id);
                }); 
            }
        }                           
    window.location = cart_url+'?array='+urlArray;
})
});

Upvotes: 0

Views: 1925

Answers (3)

Ankit Agrawal
Ankit Agrawal

Reputation: 6124

Assign urlArray to a hidden field then submit the form, it can parse large no of array to another page

Upvotes: 0

ChunkyBaconPlz
ChunkyBaconPlz

Reputation: 580

Try POSTing your data to the new PHP page:

var data = 'urlArray='+urlArray
$.ajax({
  type: "POST",
  url: 'new_php_page.php',
  data: data
});

Now you'll be able to get your variable at $_POST['urlArray'] on your new page.

http://api.jquery.com/jQuery.post/

Upvotes: 1

user680786
user680786

Reputation:

Consider to pass them in a set of ajax-requests, by 200 (for example, to not overflow input limit) IDs in each request.

Upvotes: 0

Related Questions