BASIC AJAX TUTORIAL

  • I found out myself how to build a simple ajax send and get program to retreive the data from a text file.

    YOU HAVE TO USE PHP FILES! in fact 2: a .php to send and a .php to get

    0~~~~~~~~~~~~~~~~0

    I do it like this:

    0~~/

    -Create 2 .php file, one called "send.php" and one called "get.php"

    -inside send.php you write:

    <?php file_put_contents("data.txt", $_GET['data']);?>

    -inside get.php you write:

    <?php header('Access-Control-Allow-Origin: *'); ?>

    <?php $data = $_GET['data'];

    file_get_contents("data.txt", $data);

    echo ( file_get_contents("data.txt", $data) ); ?>

    -And add a file named data.txt (empty or not it doesnt matter)

    So in total you'v just created 3 files, that you'll put in the same folder than your index.html file, once your project exported.

    0~~~~~~~~~~~~~~~~0

    FINALLY:

    In construct 2:

    0~~/

    (create 2 buttons, or any event to trigger theses actions:)

    -to send (on button 1):

    You add the action 'AJAX send to url':

    ~TAG "done-send"

    ~URL "./send.php?data=" & VAR

    (you put your own variable instead of VAR, like textbox.text for exemple)

    -to get(on button 2):

    You add the action 'AJAX request from url':

    ~TAG "done-get"

    ~URL "./get.php?data"

    -Then you add the event 'AJAX: on tag "quest-done" completed'

    Who trigger the action 'set textbox.text to ajax.lastdata'

    or even 'set variable to ajax.lastdata'

    (It's purpose is to retreive the "ajax.lastdata", the text you just asked your php to get,in your data.txt file)

    Then you can export!

    0~~~~~~~~~~~~~~~~0 /!\

    -Dont forget to add send.php, get.php and data.txt in the same folder than index.html once you exported your game!

    -Ajax dont work on preview mode! You have to publish it on your website to try it out!

    0~~~~~~~~~~~~~~~~0

    Like this you'll be able send and get what's in the file "data.txt" of your website!

    I'v tried a lot of ways to do it, but for now it's the only one who works for me.

    Working exemple:

    humain.tech/ajaaa

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)