Posting to URL

1 favourites
  • I've read several posts about sending data to a URL...

    ... but I can't seem to make it happen.

    Above is the Construct3 event sheet. I have this 'game' running online (it's not a preview).

    The php it is trying to connect with is:

    <?php if (isset($_GET['score'])){ $score=$_GET['score']; $user=$_GET['user']; include ('connect_db.php'); $sql=mysqli_query($con, "INSERT INTO construct3 (user,score) VALUES ('$user','$score')"); if($sql){ //The query returned true - now do whatever you like here. echo 'Your score was saved. Congrats!'; }else{ //The query returned false - you might want to put some sort of error reporting here. Even logging the error to a text file is fine. echo 'There was a problem saving your score. Please try again later.'; } }else{ echo 'Your name or score wasnt passed in the request. Make sure you add ?name=NAME_HERE&score=1337 to the tags.'; } mysqli_close($con); ?>

    Nothing happens when I click the OK button... I can't see why!

    Can anyone help please?

  • Hi, the method you are using in C3 is POST, but in your PHP you are trying to retrive data from a GET request.


  • Thank you!

    What happens now is that the 'game' returns an 'All Done' update... but nothing is being added to the database...

    The get.php file isn't supposed to pop open in a browser is it? I'm a little unsure as to what is supposed to happen after the click on the OK button.

  • You really need to be doing some checking via the PHP return.

    So, in PHP check if the SQL method was successful or not and echo out either "done" or the error message.

    Then in construct do the AJAX request like you already are but get the return output from the PHP which will be either "done" or the error.

    Then you can do what you need to do.

  • I get that... but how do I trigger the php? I thought the OK click was triggering it... if it's not: how do I?

    I work with php regulary... but just can't get my head around how it is instigated from Construct!

    I know I look completely daft... but there we are!

  • I have created another html form and posted to the get.php

    It works.

    So... what does that mean in Construct?

    [je]Sub Header

  • Eh?

    [je]Sub Header

  • It's really hard to do server-side implementations neatly on Construct 2 or Construct 3 using either JS Sheets or AJAX requests alone. I tried before and it was alright at first but as things get complicated, it gets harder and messy. That's why I made the PlayFab Master Collection.

    I have these addons that might interest you if you are in for the clean and easy way.

    Have a nice day.

  • Cheers. Will have a look. 😉

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I will do a working example for you tomorrow if that will help? What I suggested above was so you can see why the php wasn’t inserting into the DB. Without trying it looks like the php is being executed but there is obviously something not working in the php. But as the Ajax request is completed and the php was executed successfully you are still receiving the ‘all done’.

    Even if the php script throws an error, the Ajax request was complete so you will get the ‘all done’.

    Whilst the PlayFab Master Collection looks good it is not needed for what you are doing.

    What you are doing is very trivial, I have done a lot with construct, Ajax and php and never had any issues.


  • Yes - it really is trivial! That's what is making it so frustrating!

  • Stilts, I’d hope you could run and debug your PHP locally, so you can step through it and see what values you’re really getting from the POST.

  • Hehe.

    I've run the PHP outside of Construct. It works successfully.

    My problem is that I can't get Construct to make it happen!

  • Sure, you've tested your PHP but man it's helpful to run both the server (PHP page) and client (Construct 3 app) on the same machine and be able to step through each, to see what the server is really getting from the client.

    At a glance, I don't think you should be including a "?" in your POST data. (As you're not building a querystring for a GET.)

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