How do I use POST AJAX query string form?

Get help using Construct 2

Post » Tue Dec 29, 2015 6:32 pm

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
  1. 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.
  2. 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:
Code: Select all
    header('Access-Control-Allow-Origin: *');

I think I got it working!
I've ended up with a text file called "mydata.txt" and a PHP file called "myphp.PHP".
  1. The text file holds the data that I upload and download.
  2. The PHP file acts as an intermediary controller facilitating my C2 projects interaction with the text file!
Code: Select all
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) );

AJAX Request URL ( The GET function )
Tag = anything
URL = the url to your PHP file - ""
AJAX Post to URL ( The POST function )
Tag = anything
URL = the url to your PHP file - ""
Data = "data="&TextBox.Text - This is one of the main issues I had
  1. "data=" < this is the Query String Form mentioned in C2
  2. data just refers to where data is written in the php file,
    so it can be anything but must correlate to whats in the php file!
  3. &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
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.

:oops: Ok... never mind, it's not working... :oops:
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?

Posts: 43
Reputation: 513

Post » Mon Feb 29, 2016 7:10 pm

You need to Append.


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

Posts: 203
Reputation: 3,653

Post » Mon Feb 29, 2016 8:45 pm

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.
Posts: 1,520
Reputation: 25,305

Return to How do I....?

Who is online

Users browsing this forum: just2pale and 5 guests