How do I use POST AJAX query string form?

0 favourites
  • 4 posts
From the Asset Store
We present to you “Post Apocalyptic Trailer” – our newest hard hitting, bass rumbling designed movie trailer collection.
  • How do you use AJAX POST to change the contents of a file on your web server?

    I've been looking through various topics trying to find out how to word a POST request correctly but I'm still unsure. There's some great examples out there but I'm not sure how to translate the examples into something I can use for my project.

    I want to write to a text file hosted on a web server.

    My project is hosted on the same web server.

    The data I want to overwrite the text file with is an array.

    The text file has write permissions enabled.

    I am able to "GET" the data from the text file and update my array.

    I am unable to update / upload data to the text file

    Sorry for not including a capx, probably not relevant seeing as none of this works in preview anyway. <-[solved]

    My code looks this this:

    Button_Download On clicked = AJAX request "" (tag "getdata")

    AJAX on "getdata" completed = Array Load from JSON string AJAX.LastData

    Button_Upload On clicked = AJAX Send Array.AsJSON to URL "" (method "POST", tag "savedata")

    I think the Data field needs to be written in query string form. I don't know how to do that or what that means.

    So for "data to post" it should be some php stuff around the Array.AsJSON ?

    Am i ment to have a php file somewhere?

    Just to be clear, I'm not trying to send pieces of data, I want the whole array being uploaded and downloaded.


    I'm continuing to research this while I wait for a reply.

    I've found some great info that will help others trying to figure this stuff out.

    What I've learned - May or may not be correct

    • If your setting up a project that uses AJAX to work with your website, your probably going to need to learn a little about PHP and setup a PHP file in your web hosting directory (eg. where your website is hosted). I don't think the AJAX Page in the manual makes this very clear.
    • To get AJAX stuff to work in preview you need to use "header('Access-Control-Allow-Origin: *');" Which is great... but how do you use it that line of code? Does it go in the C2 project somewhere? Once again, not abundantly clear to beginners like myself.

    Turns out, you put a PHP file on your website, eg anyfilename.PHP and you put this code in that file:

        header('Access-Control-Allow-Origin: *');
    [b]I think I got it working![/b]
    I've ended up with a text file called "mydata.txt" and a PHP file called "myphp.PHP".
    [ul][li] The text file holds the data that I upload and download.[/li]
    [li]The PHP file acts as an intermediary controller facilitating my C2 projects interaction with the text file![/li][/ul]
    header('Access-Control-Allow-Origin: *');
    /* GET */
    $data = $_GET['data'];
    file_get_contents("data.txt", $data);
    echo ( file_get_contents("data.txt", $data) );
    /* POST */
    $data = $_POST['data'];
    file_put_contents("data.txt", $data);
    echo ( file_get_contents("data.txt", $data) );
    [b]AJAX Request URL ( The GET function )[/b]
    Tag = anything
    URL = the url to your PHP file - "[i][/i]"
    [b]AJAX Post to URL ( The POST function )[/b]
    Tag = anything
    URL = the url to your PHP file - "[i][/i]"
    Data = "data="&TextBox.Text - [b]This is one of the main issues I had[/b]
    [ul] [li] "data="  < this is the Query String Form mentioned in C2 [/li]
    [li] data just refers to where data is written in the php file, 
    so it can be anything but [b]must [/b]correlate to whats in the php file![/li]
    [li] &TextBox.Text is the object I used to collect data to test with,
    It can be anything you like though, e.g. Array.AsJSON or a global variable etc[/li][/ul]Method = "POST"
    Keep in mind that I googled my way to finding this method without really understanding the technical aspect of what I'm doing. I've seen some really awesome technical stuff along the way that I have no idea how to achieve. The way I've done this is likely not the best way of doing it. But it worked, which is a big milestone for me as a beginner. 
    I'm keen to hear any extra explanation as to the correct process and any features or improvements that can be implemented.
    For some reason when I GET the data, i receive it fine, but it wipes the data from the text file!
    Can anyone see what went wrong with the PHP code?
  • You need to Append.


    file_put_contents("data.txt", $_GET['data'], FILE_APPEND);


  • Your script is both reading and writing the file on every request. When you make a GET request to read the file you still write the contents of $_POST['data'] (which is empty) to the file afterwards.

    You need to put in an if statement to check if $_GET['data'] or $_POST['data'] is set and act accordingly.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hi

    but this is not working for https So how we use this code for Https sites?

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