Ajax requesting from php, lag or other issue?

Get help using Construct 2

Post » Wed Oct 12, 2011 6:25 am

I've been using this tutorial -
- to try and get an understanding of ajax/php calling to create
a high-score table for a game, however I haven't even been able to
get past the understanding of Ajax due to some, confusing issues?

The tutorial is that you press a button, and hey-presto, the text
is suppose to pop-up.
I've been using the supplied files at the end of that tutorial,
(obviously changing the .php file path).

For some, unknown reason, it simply isn't functioning for me like it should.
I've clicked the button, and nothing happens, so I have kept clicking
it, and clicking it, and clicking it a lot and then the text randomly
pops up for I have no idea what reason.
I've tried multiple times, and it's eventually popped up at different
times, sometimes when I'm sure I wasn't clicking on the image.
Also I have clicked the image a single time and waited 10+ minutes,
the text never changed.

Does anyone know what might cause this, has anyone had the same problem
and found a solution, please I'd really appreciate whatever help or
experiences can be shared.
Posts: 58
Reputation: 1,166

Post » Wed Oct 12, 2011 12:29 pm

Can you post your .capx file?

AJAX requests are asynchronous, meaning the game carries on running while the request happens in the background. When it completes, it triggers the 'on completed' event. If the server took 5 seconds to respond, 'on completed' might only trigger 5 seconds after the AJAX request. If the server had an error and didn't respond at all, 'on completed' will never trigger at all.
Scirra Founder
Posts: 22,822
Reputation: 176,618

Post » Thu Oct 13, 2011 12:39 am

Thank you very very much for responding ^^;

Why does reality hate me so =_=

Today, with 0 changes, it's just decided 'oh hey, he has asked for help, now that we have screwed with him lets start working k?'

Though, it doesn't seem to always want to work still, sometimes it'll
act like it did yesterday where I'll have to be clicking it for more
than a minute before it decides it wants to pop-up (about 1 in 20).
Here's the file

I should also note that, as you mentioned about it only acting once
it's been able to receive the requested information, it may be considered
as just a delay, but I have clicked it multiple times today with it
not working, then waiting 5 minutes and still nothing, then clicking
a few more times and it eventually displaying the text right after
that.sparkfeather2011-10-13 00:45:20
Posts: 58
Reputation: 1,166

Post » Thu Oct 13, 2011 1:29 am

Try changing your php page to something simple like this:

echo "hello world";

And see if what that does. Are you using your own server or is it online?
Posts: 50
Reputation: 725

Post » Thu Oct 13, 2011 1:59 am

Hrmm, that isn't giving me any issues at all o.o?......
Thanks a lot for your response, any idea what this means though? o.O
Tested it 50+ times and not a single time it faltered,
however I did discover a bug.

If you refresh the page, and don't move the mouse at all, the clicking action isn't registered.
As soon as you move the mouse and click again, it registers it.
(This however is not the reason I have been having issues because when
I rapidly click the mouse I also move it around to different locations
on the image so this wasn't the cause, it is however a bug).

"Are you using your own server or is it online? "
This is using my online serversparkfeather2011-10-13 08:26:37
Posts: 58
Reputation: 1,166

Post » Thu Oct 13, 2011 2:55 am

Well, I'm currently working on an AJAX project. On the php side it's nothing but php, no html. The bug thing could be that you are not focused on the canvas when you refresh but it could somehow focus it when you move your mouse.

If you want, check out this forum topic for AJAX requests:


The file I'm currently working on:

You might need to read the posts to get all the information from it so you can understand the php part.
Posts: 50
Reputation: 725

Post » Thu Oct 13, 2011 10:14 am

That might be it...but wouldn't that mean it should just require 2 clicks,
want to focus and one to press the button?
I know flash sometimes is out of focus but you just click inside the
canvas to focus it, not move the mouse? Maybe it works differently for

Thanks a lot for the links, but even though I read through it, a lot
still makes absolute no sense. Even though the tutorial up there is
in response to questions about keeping scores, all it pulls is php,
and every time anyone seems to ask how to differentiate what you grab
from the page your told off because that information is too difficult
for these forums?
Yet I still struggle to understand how all that is suppose to be done
within the php document, what about the tag thing for construct2,
what is the point of that if the data is always stored to the single
LastData variable. Unless you assign that to another variable you won't
be able to store more than one data coming in, how would you spread that
across a page with multiple highscores?
I know this is off topic but so many confusing questions.

Your latest demo looks good though, any chance you might be able to share what you have in the php and the construct file? That'd be a massive favor I'm sure for many people on this forum including myself.

Really appreciate your response b781rev, and has informed me of more than
I knew before, so I thank you greatly for your input, but for the original
topic, nothing in your latest response really explains how that new
php would work flawlessly while the other would have delays occasionally?
Unless I've overlooked something you were referring to?
Posts: 58
Reputation: 1,166

Post » Thu Oct 13, 2011 11:25 am

sparkfeather: I downloaded your ajax example.

Ajax is supposed to go look for an url online. All you do is try to get a php file with no url, no wonder it can't work.
Also, as Ashley said you only have a "on completed" event.
If there is an error in the process, you should also have a condition "on request failed" that would display you a message as there is an error.

You really should look attentivly again at the jax tutorial.

The client is you when you launch your C2 application.

The php file is on your online server (with a full url like http://www.myserver.com/myfile.php).

Ajax is a communication between your application and the online server.
The php file "responds" to the event "Request" and the data "responded" sits in Ajax.LastData

The fact that there is delay might come from the time it takes for the request to go from your computer to your server, the time that the server processes the request and then the time that the answer comes from the server back to your computer.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Posts: 6,932
Reputation: 56,898

Post » Sat Oct 15, 2011 1:18 am

Like I mentioned Kyatric, it was finding the php file sometimes just
not straight away originally, so that wasn't the issue.
Also many times I clicked a lot but it didn't do anything, and I waited
a very long time to see if it was just a possible major lag as you
mention waiting for the request/response, however it didn't. Then
clicking for awhile more it eventually popped up.
So from testing it didn't really seem to be a matter of delay.

Thanks a lot about the error tip, that was never mentioned in the
tutorial so I didn't even think of that or try it because I wouldn't
expect something so simple to error. Maybe it could have been erroring
a lot but without checking for that as you suggest I'd have no idea.
I'll make sure to add that for all ajax testing in the future :)
Posts: 58
Reputation: 1,166

Return to How do I....?

Who is online

Users browsing this forum: Aphrodite, nimos100 and 21 guests