How do I post data using AJAX?

Get help using Construct 2

Post » Tue May 14, 2013 12:22 pm

I saw a tutorial on reading data from a web server, but nothing on saving data to an online server.

I tried using the AJAX Post To URL but I keep getting an error. (I am uploading my build to the same server so crossdomain security shouldn't be the issue)

All I am trying to do is to upload some text from a form to a text file. From that I hope then to be able to upload images too. The Construct 2 form bit is easy enough, I just need to know to know what the AJAX parameters should be and/or what PHP do I need to call?

Cheers
B
6
Posts: 10
Reputation: 422

Post » Tue May 14, 2013 12:52 pm

Construct 2 Event:

Button : "on clicked" => AJAX : Request URL ( "http://myserver.com/test.php?myVar=myContent)


PHP Script (test.php):

<?php

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

?>
B
7
S
1
G
1
Posts: 32
Reputation: 1,016

Post » Tue May 14, 2013 2:24 pm

Cool, but i still cant get it working. (It still errors on the server)
My path is:

"http://myserver.com/test.php?myVar="&URLEncode(TextBox.Text)

Is something wrong with that?
CheersGrimmy2013-05-14 14:27:14
B
6
Posts: 10
Reputation: 422

Post » Tue May 14, 2013 2:38 pm

Hm, what is your exact error?

Your data.txt have to be created once before you run your script. Just upload an empty "data.txt" to your server.
B
7
S
1
G
1
Posts: 32
Reputation: 1,016

Post » Tue May 14, 2013 2:44 pm

Yeah, I already have a data.txt file there (in the same folder as the test.php file) but it doesn't update.

I don't have any error information other than I am receiving an AJAX:On error message.
B
6
Posts: 10
Reputation: 422

Post » Tue May 14, 2013 2:53 pm

It should work.

Try to run your php script with your browser to check for errors.

You can also upload your .capx file so i could take a look at it.
B
7
S
1
G
1
Posts: 32
Reputation: 1,016

Post » Tue May 14, 2013 3:28 pm


Here is the dropbox link to the file. The php on it's own works fine.

https://www.dropbox.com/s/nnpq4aixls3hyz6/NoUpload.capx
B
6
Posts: 10
Reputation: 422

Post » Tue May 14, 2013 3:43 pm

You dont need the "&" after myVar, but that is not the error.

So your capx looks right. Test the following
Open the URL http://www.pigobo.com/UploadTest/test.php?myVar=TEST1234

After that check your "data.txt". If it dosen't contain "TEST1234" your PHP Script don't work correctly or you dont have permission to write to the file.

B
7
S
1
G
1
Posts: 32
Reputation: 1,016

Post » Tue May 14, 2013 3:59 pm

The data file does contain the TEST1234 string so I do have permission and the PHP is fine.

When I try the same with Construct 2 calling the php file it doesn't work. I even removed the & but still no success.

B
6
Posts: 10
Reputation: 422

Post » Tue May 14, 2013 4:11 pm

Maybe a cross-domain security error ;-)
Whats the URL you uploaded your exported html and js files?
B
7
S
1
G
1
Posts: 32
Reputation: 1,016

Next

Return to How do I....?

Who is online

Users browsing this forum: 99Instances2Go, mnashwan, shinkan, stylie1, tarek2 and 11 guests