Any limits on AJAX post-data?

0 favourites
From the Asset Store
We present to you “Post Apocalyptic Trailer” – our newest hard hitting, bass rumbling designed movie trailer collection.
  • I have a few hundred variables that I want to post to my PHP file, to update into a database. My plan is to add these to an array, then post them array.AsJSON. Are there any limits on the amount of data you can send (either strict limits or suggested limits?). Is a few kilobytes of data too much?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I dont think there is a limit. As a web developer I can say ive posted json strings that contain thousands of rows of array data back and forth between javascript and php. C2 should be no different. Just add it to the URI string what ever the length and you should be fine

  • In the 'data' part right? As array.AsJSON as data? It's going through fine, now I just need to work out how to alter your PHP code from the other day to break it into a 2-dimensional array instead of a 1D one... find the PHP syntax tricky still xD

  • Yeah.. just add it to the data string and assign it to a variable in the data string.

    In this example I packaged the array.AsJSON in the "c2Data" POST variable by typing in "c2Data="

    [attachment=0:znwa8ify][/attachment:znwa8ify]

    You can use this to parse an array with only the x dimension set

        //$_POST['c2Data'] = {"c2array":true,"size":[3,1,1],"data":[[[60]],[[10]],[[20]]]}
        $jsonString = $_POST['c2Data'];
        
        //Have to set to true so json is converted to array and not an object
        $jsonAsArray = json_decode($jsonString,true); 
        
        //$jsonAsArray['data'] = [[[60]],[[10]],[[20]]]
        $c2ArrayData = $jsonAsArray['data'];
        
        //$c2ArrayData = [[[60]],[[10]],[[20]]]
        $output = array();
        foreach ($c2ArrayData as $z) {
    	     foreach ($z as $y) {
    	            foreach ($y as $x) {
    		          $output[$y] = $x;
    	            }
    	     }
        }
        
        //$output[0] = 60
        //$output[1] = 10
        //$output[2] = 20
    
    [/code:znwa8ify]
    
    Edit: I see now you wanted a 2d example.. give me sec to type it up and ill be back
  • Ok so imagine you have a 2 dimensional array (x,y)

    [attachment=0:3lub0k77][/attachment:3lub0k77]

    AsJSON the string would look like this

    "{"c2array":true,"size":[2,2,1],"data":[[["Hello"],["World"]],[["Good"],["Bye"]]]}"

    So to decode it 2 dimensional in PHP you would do this

        //Just to see what it will look like 
        //$_POST['c2Data'] = "{\"c2array\":true,\"size\":[2,2,1],\"data\":[[[\"Hello\"],[\"World\"]],[[\"Good\"],[\"Bye\"]]]}";
        
        $jsonString = $_POST['c2Data'];
        
        //Have to set to true so json is converted to array and not an object
        $jsonAsArray = json_decode($jsonString,true); 
        
        $c2ArrayData = $jsonAsArray['data'];
        
        $output = array();
        foreach ($c2ArrayData as $zIndex => $zRow) {
    	      $output[$zIndex] = array();
    	      foreach ($zRow as $yIndex => $yRow) {
    	             foreach ($yRow as $xValue) {
    		          $output[$zIndex][$yIndex] = $xValue;
    	            }
    	      }
         }
    
        //$output[0][0] = "Hello"
        //$output[0][1] = "World"
        //$output[1][0] = "Good"
        //$output[1][1] = "Bye"
    
    [/code:3lub0k77]
  • Yay, thank you. I'm sure I would have worked this out myself eventually, but this will save me loads of time. I've programmed in BASIC for 30 years, so transitioning to PHP is taking some time due to the different syntax. I'm getting there, but even the simplest things are taking me hours as I try to work out the new syntax. As I said, this will save me hours, so thank you

  • troublesum, just wondering if you know if there's a good reason not to create my own strings and parse them in PHP myself? For example, when I started this last week, I simply made a string of my data with a pipe '|' separator. In my case, I'm making Soccer players for my game and they're stored as objects, with name/skills etc. So my string would look something like 'John Smith|300|400|100|200' etc. So I'd send that to the PHP file as data, then just 'explode' it in PHP and update my tables with the resulting array.

    Now the thing is, not only did I find that very easy to understand, but it's also easier at the C2 side, because the players are already stored as objects. So to create the string I could do something like 'Str = Str & Player.Name&'|'&Player.Age&'|' ' etc. Nice and easy. What I have to do now is, put the values into the array first, then send the array.AsJSON.

    Also on the PHP side of things, I found it very easy to just parse the string with explode, then put the separate elements into my table.

    When I got stuck and explained this on some PHP boards, they basically told me it wasn't a good method and I'm better of using JSON, but never really explained why. I'm not sure if they're worried that I'm 'reinventing the wheel', because if that's all it is, frankly I'd rather use my own wheel that I understand... and it's actually easier anyway. I suppose there's also the problem that maybe I'm adding extra bytes to my string with the separators, but the JSON string looks huge anyway and has loads of extra characters. Or is it a security issue? If it's security, I'd rather go for the most secure method. But if they're suggesting that JSON is the easier method, maybe it is for C++ programmers etc, but for C2 it doesn't seem to be, and building my own strings is something I'm comfortable with.

    Any ideas? Is my method ok in your eyes?

  • I use json is because you can json_decode it into straight into arrays without having to parse anything either end (keys if you use dictionary) so if your moving alot of complex arrays around then it makes sense. It also takes out the human element (bugs), it's less code to write and it's easier to debug/validate.

    Not to mention that if you ever share something it can be easily understood. Nothing worse than having to read through someone's 'special' functions list or 'easy' class wrapper.

  • Ok that's cool and I thought perhaps that was the reason. As far as sharing it, never going to happen as this game is my business and is run/operated entirely by me. I suppose it's feasible that some time in the future I may be dead and somebody else may wish to run the game for me, but even then, it's not like my code is that hard to understand (I do comment for example, only for my own sake though).

    In terms of ease and bugs, I personally find my method better since I need to move my objects into an array first anyway. And for me, it's easier to understand, although at some point, obviously I'll start to understand the PHP code better, but right now it's all completely unreadable to me and I'm relying on help to code even small code such as the bits above. Though of course, I need to use PHP so the more code I play around with, the better.

    Plus surely you do need to parse the JSON array at the PHP end? That's what the code above is for that Troublesum has written out for me. Is there a more straightforward way to get the C2 array into a PHP array?

  • farflamex So there is nothing wrong with making your own strings and exploding them into arrays on the PHP side if that's what your comfortable with. The Problems they didn't explain is that if for any reason one of your values happens to contain a character that is the same as your delimiter (in this case a "pipe" | ) then it would break your method. Obviously you would make sure that never happens but its possible and a limitation. Also by using JSON you keep the value type such as is its a string, int boolean, etc.. for now your always going to have strings by exploding your custom "pipe" delimited string. Again there is nothing wrong with creating your own custom strings and in truth we all start out doing it that way (Every beginning PHP developer probably starts out doing that) but as you get better and can do more you will suddenly need more and that's when you start using JSON to pass information. I say go with what your comfortable with until it no longer provides enough flexibility and then move up JSON when your ready. The only thing is that if you perfectionist you will want to go back and fix all your legacy code that uses out dated methods once you decide to start using JSON

  • Yeah that's true, thanks for the explanation. If JSON is better, I'd rather just stick with it (I've grown very patient with old age) until I'm comfortable with it. All new languages/concepts are difficult at first anyway.

    Certainly I'd never need a pipe in any normal circumstance, so I don't see that being a problem.

    Wow, I've been using C2 for years and for some reason, I'd never noticed that you can mix types in an array, so that does help make JSON more appealing. I was thinking I'd have to send the strings and integers in 2 seperate arrays, but apparently not.

    This is almost tempting me to KEEP everything in an array during my C2 processing, but I absolutely hate arrays nowadays (used to swear by them until I became comfortable with objects). My old program (written in 1991) is all arrays and it's very difficult trying to work out what's happening when you read Rating(2,21) etc, it just makes no sense. So I always parse everything into an object when receiving in C2, then load it into an array to send it back to PHP, then split it again using your code and save it. Does sound long-winded when I could just keep it all in the array all of the time. But arrays are so hard to read and bug-ridden, I don't like that idea at all.

    Plus, when returning the data to PHP, I don't intend to send all of the data anyway, only selected bits (i.e the bits that have changed). So I'm effectively creating a small temporary custom-array, just to post it as my JSON string. That seems sensible instead of constantly sending the whole object back and forth when only a single value might have changed, though it also means more PHP scripts. Hmmm, lots to think about, but for now I'll stick with the JSON code until I understand it better (actually I understand the JSON part now, just the PHP is still confusing me).

  • farflamex I dont know how you feel about using 3rd party plugins but I also found that I didnt like the way Dictionaries and Arrays were stored in C2 so I created my own Table plugin that works with raw JSON data.

    [attachment=0:1bsug4ht][/attachment:1bsug4ht]

    Example:

    [attachment=1:1bsug4ht][/attachment:1bsug4ht]

    You can see in this example I just set my values and then can pass the entire table AsJSON how ever unlike C2 Arrays its pure JSON data with no additional overhead and is a named array storage instead of indexed integer values. Its basically the same as any array (hash table for those that prefere correct my symantics) your familure with working with in PHP.

    $jsonString = $_POST['c2Data'];
    
    $array = json_decode($jsonString,true);
    
    //$array['Player1']['Name'] = 'Troublesum' (string)
    //$array['Player1']['Score'] = 1000 (int)
    //$array['Player1']['Health'] = 100 (int)
    
    //$array['Player2']['Name'] = 'Not Troublesum' (string)
    //$array['Player2']['Score'] = 500  (int)
    //$array['Player2']['Health'] = 20 (int)
    [/code:1bsug4ht]
    
    You can see its much easier to work with and the value types (string),(int) are maintained. If your interested in using the plugin your more than welcome to. I have attached it above with a few examples on how to use it.
  • Cool, this looks really interesting and seems to cut out much of the work. It's not really the work that bothers me, just that I don't like messy code, and this looks nice and neat. Especially since the code looks similar on both sides, which will really help with readability.

    Going to give it a try. Only downside I can see is that I'll need the add-on pretty much forever and I suppose that there's a danger of losing access to it in the future (mind you, I could store a copy on my server with all my data or something, just in case).

  • Looks excellent, you've done lots of work on that, seems to be very comprehensive too with every option I can think of. It did report a couple of errors when I loaded it up, but then went ahead and worked fine anyway, maybe something to do with newer versions of C2 or something?

  • Possibly... I do only install the stable releases of C2 soim not on the latest (174).. try saving and re-opening and see if the error comes up. I don't have any errors when I open it so if you still get them upload a screen shot for me if you can. Thanks

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)