Any limits on AJAX post-data?

Get help using Construct 2

Post » Thu Jul 03, 2014 1:22 am

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?
B
26
S
6
G
4
Posts: 456
Reputation: 5,853

Post » Thu Jul 03, 2014 3:18 am

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
B
20
S
7
G
1
Posts: 221
Reputation: 2,077

Post » Thu Jul 03, 2014 3:45 am

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
B
26
S
6
G
4
Posts: 456
Reputation: 5,853

Post » Thu Jul 03, 2014 4:05 am

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="
help1.png


You can use this to parse an array with only the x dimension set
Code: Select all
    //$_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



Edit: I see now you wanted a 2d example.. give me sec to type it up and ill be back
You do not have the required permissions to view the files attached to this post.
B
20
S
7
G
1
Posts: 221
Reputation: 2,077

Post » Thu Jul 03, 2014 4:31 am

Ok so imagine you have a 2 dimensional array (x,y)

help1.png


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
Code: Select all
    //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"

You do not have the required permissions to view the files attached to this post.
B
20
S
7
G
1
Posts: 221
Reputation: 2,077

Post » Thu Jul 03, 2014 11:29 am

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 :)
B
26
S
6
G
4
Posts: 456
Reputation: 5,853

Post » Thu Jul 03, 2014 5:11 pm

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?
B
26
S
6
G
4
Posts: 456
Reputation: 5,853

Post » Thu Jul 03, 2014 6:59 pm

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.
B
15
S
4
G
5
Posts: 42
Reputation: 3,617

Post » Thu Jul 03, 2014 7:20 pm

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?
B
26
S
6
G
4
Posts: 456
Reputation: 5,853

Post » Thu Jul 03, 2014 7:35 pm

@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 :)
B
20
S
7
G
1
Posts: 221
Reputation: 2,077

Next

Return to How do I....?

Who is online

Users browsing this forum: MadSpy and 7 guests