How do I send data to my php script? (AJAX question)

Get help using Construct 2

Post » Sat Jun 28, 2014 3:19 pm

I get the basics. I have a php script which will update my database and the file is on my server. I know how to use AJAX to request the file and how to retrieve data, but I'm not quite sure how to send data for updates. Does my data need to be embedded in the url and retrieved with 'isset'? Is there another way, or does it always get embedded with the url this way? What would happen if for example I needed to send 50 pieces of data at once, would I just create a very long url and extract them individually with php?
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Sat Jun 28, 2014 7:53 pm

With an AJAX post the data is a parameter in the action, not part of the URL. As for dealing with it once the data gets to PHP you can look at: http://www.ajax-tutor.com/post-data-server.html

The manual has some info on sending POST from Construct 2 as well: https://www.scirra.com/manual/107/ajax
"Construct 4 lets YOU make advanced games! (but not play them)" Construct Classic - Examples Kit Dropbox is a pile of trash and if you need my old files PM me! :)
B
116
S
41
G
17
Posts: 2,204
Reputation: 19,545

Post » Sun Jun 29, 2014 1:01 pm

Cheers. I've got it working fine. The PHP forums are telling me it's best to use JSON so I'm working on that now. For my test I was just sending 3 ints, separated by pipes, e.g 60|5|1 , then splitting it with 'Explode' in my PHP script. Works absolutely fine and is adequate for my needs but maybe JSON is better for some reason? I'm not quite sure why, because to send it as JSON, I need to put my 3 numbers into a temporary array, send the Array.AsJSON, then do...

$postdata = file_get_contents("php://input");
$postdata = json_encode($postdata);

Right now, I'm not sure how to make that into an array and access the values, but I'm sure it's pretty simple. Ploughing through tutorials and manuals and learning as I go.

In fairness, it's pretty easy and tidy this way too. It just means I need to have a temporary array in C2, alter it's size, put the data into an array and send the array, as opposed to just creating and sending a string. While my method seems easier and I understand it more fully, maybe I should put the effort into learning the JSON way as it's more standard?
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Sun Jun 29, 2014 1:13 pm

Glad to hear :) This page seems to be helpful for parsing the JSON in PHP: http://stackoverflow.com/questions/7986 ... est-in-php
"Construct 4 lets YOU make advanced games! (but not play them)" Construct Classic - Examples Kit Dropbox is a pile of trash and if you need my old files PM me! :)
B
116
S
41
G
17
Posts: 2,204
Reputation: 19,545

Post » Sun Jun 29, 2014 1:50 pm

Hmm, looking through the various other JSON to PHP searches on the C2 forum, I don't seem to be alone in struggling with this. Not quite sure why, but after retrieving my JSON data in PHP, none of the methods from tutorials etc seem to give me a true PHP array. Probably doing something wrong. What I get at the PHP end is .... "{\"c2array\":true,\"size\":[3,1,1],\"data\":[[[60]]],[[1]],[[25]]]}".

I assume that's correct, but when I use the various methods to break it down into a PHP array, I never seem able to access the individual elements. To be honest, I'm probably just doing it wrong, so I'll come back to it later. If anybody knows a simple way in PHP to retrieve a C2 JSON encoded array and then output/access the elements (e.g Array[0], Array[1]) etc, please let me know.

In the meantime, my manual method was working fine and seems simpler to me, so I'll come back to this later :)
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Sun Jun 29, 2014 5:12 pm

Not sure what you have tried but assuming your returning the C2 array AsJSON to php then this should decode it and put it in to a usable array.

Also I found a syntax error in your string you provided, you have an extra bracket after the 60 that is breaking the decoding. I fixed it in my example.
Code: Select all

$JsonC2ArrayString = '{"c2array":true,"size":[3,1,1],"data":[[[60]],[[1]],[[25]]]}';

//Have to use the true condition to output as a array and not object
$jsonArray = json_decode($JsonC2ArrayString, true);

//The array data is located in the "data" object of the first level of the JSON string
$c2ArrayData = $jsonArray['data']; 

$output = array();

//Generate out put of data.
foreach ($c2ArrayData as $z) {
      foreach ($z as $y) {
            foreach ($y as $x) {
             $output[count($output)] = $x;
       }
      }
}

//$output[0] = 60
//$output[1] = 1
//$output[2] = 25

B
20
S
7
G
1
Posts: 221
Reputation: 2,077

Post » Sun Jun 29, 2014 5:46 pm

Ah thanks, this is what I was looking for. I should point out that the output that I printed above was actually typed in so I must've made a typo - I was just dumping it to screen in a C2 text object and typed it out. It looks like I'm missing some steps in constructing my array. It's not quite working, I start with..

$JsonC2ArrayString = file_get_contents("php://input");

Which, as far as I can see, gives me exactly the same as the array you've typed above. Hmm, anyway cheers, I can see I've missed a few steps that are important so I should be able to fathom it from here :)
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Sun Jun 29, 2014 5:55 pm

Ok cool got it, thanks. What is the nested foreach loop actually doing? Presumably parsing the array or something? This is where I was stuck, because I just assumed that after...

$c2ArrayData = $jsonArray['data'];

(actually I didn't know about the 'data' string either, so that wasn't helping). But anyway, I just assumed that after that, I could do a

echo $c2ArrayData[0]; or maybe after $output = array(); I would be able to get $output[0]. It seems surprisingly complex but at least it works now. I assume I can just copy this code into my future snippets with any little changes needed.
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Sun Jun 29, 2014 6:17 pm

@farflamex Glad it helped.. The reason for the nested for-each loops is because the c2 array object is 3 dimensional and your based on the string the data values were in the 3rd dimension of the array so you have to dig into each level to find it and separate it.
B
20
S
7
G
1
Posts: 221
Reputation: 2,077

Post » Sun Jun 29, 2014 6:31 pm

Ah right, good point. There's no way to avoid this when using a C2 array right? For example you can't actually set a C2 array to 3,0,0 so I guess this is the only way?
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Next

Return to How do I....?

Who is online

Users browsing this forum: datiel12, tarek2 and 60 guests