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

0 favourites
  • 15 posts
From the Asset Store
Data+ is the best Data Management solution for Construct 3. It contains 4 Addons (Plugin & Behavior).
  • 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?

  • 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

  • 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?

  • Glad to hear <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile"> This page seems to be helpful for parsing the JSON in PHP: http://stackoverflow.com/questions/7986 ... est-in-php

  • 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

  • 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.

    $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
    
    [/code:visqtsuu]
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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

  • 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.

  • 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.

  • 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?

  • Years of BASIC programming have made this really confusing. It's all working fine, the PHP part at least, but I'm trying to do things BASIC ways and presumably it just doesn't work that way. For example, I'm trying to do tests on the new $output values, but it's kinda ignoring me... e.g

    if ($output[1] = 2) {

    $query = $db->prepare("UPDATE Players SET PositionRT = :NewPos WHERE ID = :ID");

    }

    if ($output[1] = 3) {

    $query = $db->prepare("UPDATE Players SET PositionYT = :NewPos WHERE ID = :ID");

    }

    This ALWAYS executes the 2nd statement, even if $output[1] doesn't equal 3

  • I think it's actually running both of those: http://www.php.net/manual/en/language.o ... arison.php

    PHP is kinda like C languages, you need to use == when doing a check (= sets a value) <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile">

    Still, I'd love if every language was a quick and simple as BASIC <img src="{SMILIES_PATH}/icon_razz.gif" alt=":P" title="Razz">

  • Ahhh cool yes. I've been learning non-stop for about 5 days and although I read about == and === a couple of days ago, it's already been filtered out by the extra stuff. In fairness, I do remember learning BASIC in the 80's and it took ages for it to really settle in, so it's the same thing. But yeah, it would be nice if they had BASIC syntax. I'm converting a game over from 1991, which was written in BASIC, long before visual or objects or anything like that. It was pure spaghetti and I was younger and less patient, so my program is an absolute unreadable mess. My plan was to convert it to a modern language, all nice and neat but PHP looks exactly like my 1991 program, only worse :p

    But I believe this is the way the programming world is headed and at least we have C2 to make the front-end UBER easy, which would be the hardest part. I just need to learn PHP and build the 'guts' of the game in that but it'll be a while before PHP sinks in methinks

    Thanks, as you say, it's currently running all of the 'if' statements, I'll change to ==

  • All working fine now, thanks guys. Whole weekend's work, but got it done, can sort've say I can now select/update/insert from C2 into mysql database on my server... now I can sleep

  • Any chance you could help me out with turning this code into a 2d array in PHP? I understand the basic PHP syntax for 2d arrays, i.e array[0] [0] instead of just array [0]. Just struggling to work out how this is chopping it up into the array in the first place. For example, right now I'm sending a 10 x 3 array asJSON.

    This was your code from above to turn it into a 1d array. How do I adjust it?

    foreach ($c2ArrayData as $z) {

    foreach ($z as $y) {

    foreach ($y as $x) {

    $output[count($output)] = $x;

    }

    }

    }

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