How do I use php in Construct 2?

  • The title says it all.

  • You can't use PHP in Construct 2, but you can use it with Construct 2.

    Using the Construct 2 AJAX object you can make a GET request to a PHP page, for example:

    "http://www.mysite.com/receiver.php?action=highscore&username=Tom&score=28000"

  • Example of using AJAX object in C2.

    WAMPserver a good software to make local webserver so that you can test your PHP scripts before hosting them online.

    W3Schools PHP tutorials and references.

    Forum ate my previous answer and Tom answered the question anyway.

  • great software. The best I have seen so far!

    BTW Kyatric how to make your example work?

  • Read the comments.

    Replace the url with an url from your server like Tom gave.

  • But how will I get the specific data. For e.j in

    mysite.com/receiver.php

    how will I get the score which is 280 and assign it on a text?

  • But how will I get the specific data. For e.j in

    http://www.mysite.com/receiver.php?action=highscore&username=Tom&score=280

    how will I get the score which is 280 and assign it on a text?

    When you make a request to the page, the output of the capturing page will stuff the result then send it back to the calling page. In this case, I assume your game.

    You can access the payload by using the AJAX object as in:

    AJAX.LastData

    You can then set that data to a text box or pass it over to another variable in your game.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • But the AJAX.LastData doesn't returning any data.

  • But the AJAX.LastData doesn't returning any data.

    If it doesn't appear to be returning data then you may want to check the page that is suppose to be returning the data. If the page is throwing an error or returning blanks, then yes, the AJAX.LastData will not return anything.

    If the page is returning or showing data, then it may be that the events you've set up in your game is not setting or assigning the AJAX.LastData payload to your target object. For example, a text box that keeps track of scores or users.

  • Can you show me step by step? That will be a great appreciation.

  • Can you show me step by step? That will be a great appreciation.

    I've been working on my game the past couple of days since picking up Construct 2 and it uses the AJAX object. When it's done, I'll submit a tutorial on how I did it.

    There may already be a tutorial in the works as I understand someone was working on one. Either way, I'm a couple of days away from getting my test game out and you can see it working I hope.

  • Setting up PHP and a database is a reasonably complex thing to do, needing some technical knowledge. I don't think Scirra is the place to go to learn about PHP and databases. There are already loads of free tutorials to find to learn about that - I think any tutorial on Scirra should assume you know about PHP and databases already, and just show the part where you access it via the AJAX object.

  • Yep. That's all the part I need. I already have the db and necessary php files set up (I think <img src="smileys/smiley5.gif" border="0" align="middle" />). I just need some insight on using AJAX to Post and Get info to/from my application.

  • It's actually not a hard concept to get your head around - an AJAX request is a bit like opening the page in a browser, and the returned data (LastData) is the text that appears in the browser page. Still, a tutorial would be useful.

  • I have the basics for the AJAX object working on my test game. It will post and show a score in a simple listing. I'll write a quick tutorial for everyone so I can remember how I did it too.

    Sample test game with AJAX object posting to database.

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