AJAX and PHP: Sending information

Get help using Construct 2

Post » Tue Apr 17, 2012 12:56 am

Hello! :)

Here is a Link to my capx: http://dl.dropbox.com/u/30036346/Dragon.capx

This is a game for research propourses. It's not even close to the final stage. The aim here is to press spacebar when the timer reaches 0. That way, you can damage the dragon. If you press it on the wrong time (when the dragon is protected by a Dark Aura) you'll get hit by it's fireball.

I've been learning about PHP and AJAX. I read the tutorials avaliable in the FAQ Thread. But I'm not a good programmer and I can't do it properly.

Question: I need to save this CSV file, which must register the time when the player pressed spacebar. Then it goes to another line in the CSV , and when Spacebar is pressed, the timer is registered again. This Way, I can keep track if the player improved his peformance or not.

The data needed are:

Name:
Age:
DragonTimer(which is a variable)
Trial: (how many times the player pressed spacebar so far)

Just like that.

I'm trying to use this code:
[quote]
<?php
/** Read in a username and score and save them to a file. */
// read variables from POST data.
$username = $HTTP_GET_VARS[name];
$score = $HTTP_GET_VARS[score];

// verify the username is set and not empty
if (!isset($username) || $username == "") {
    echo "failure";
    exit;
}

// verify the score is set and a number
if (!isset($score) || !is_numeric($score)) {
    echo "failure";
    exit;
}

// format the username and score as a comma delimited row
$entry = $username . "," . $score . $ DragonTimer "," . "Attempts""\n";

// append entry to the score file
if (!file_put_contents("scores.csv", $entry, FILE_APPEND)) {
    echo "failure"; // failed to write to file
    exit;
}

echo "success";
?>
[/quote]

Any help is really appreciated. But keep in mind that i'm not a programmer, i'm just a newbie in a adventure which is far beyond my professional field!
B
6
Posts: 56
Reputation: 712

Post » Wed Apr 18, 2012 1:27 am

A Little bump :)
B
6
Posts: 56
Reputation: 712

Post » Wed Apr 18, 2012 3:21 pm

After going crosseyed looking for info for newbies I decided to do the ole trial and error to link up to a mysql database. So far what I can do is send info to the database. This tutorial assumes you can create the mysql database already.
1: Populate the mysql database with the following fields
-playerid(varchar(30)
-timestamp(timesent) set this to autoincrement
-text(varchar(255)
2: Here is the PHP file

<?php
$username = $_GET['fname'];
$score = $_GET['testy'];
$con = mysql_connect("localhost","userid","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("name of database", $con);

mysql_query("INSERT INTO chat (playerid, text) VALUES ($username, $score)");


mysql_close($con);

?>

3: Now in your layout create a text box called "chatinput" as well as a button called "chatinputsend"
4: In the event sheet create an event
chatinputsend - onclick - AJAX - Request - "phpfiledirectorylisting?fname='lance'&testy='" & chatinput.Text & "'"

The above code sends the text box information into the appropriate fields within the mysql database. Hope that helps. Now to just figure out how to pull the information down from the database to populate a text window. Any ideas?
B
68
S
21
G
15
Posts: 701
Reputation: 15,604

Post » Wed Apr 18, 2012 8:56 pm

Please tell me that isn't production code.

You HAVE TO typecheck, validate and escape EVERYTHING before sending it to SQL.

Getting the information just takes another query to the database. You could theoretically just make this another php file or set up your original one to either send or recieve based on a query parameter, and it could format that information as JSON for an ajax call to that file.

something like this should work (assuming typos because i'm sleep deprived):

$uname = null;
$score = null;

if(isset($_GET['fname'], $_GET['testy'])){
... connect to the db

$uname = trim(strip_tags($_GET['fname'])); // remove malicious code
$uname = str_replace("\0", "", $uname); // remove poison bytes

// verify int and min/max values
if(filter_var($_GET['testy'], FILTER_VALIDATE_INT, array('options'=>array('min_range'=>0, 'max_range' => (?) ))){
$score = str_replace("\0", "", $_GET['testy']); // just because you're paranoid don't mean they're not after you

}

if(isset($score, $uname)){
mysql_query("INSERT INTO chat (playerid, text) VALUES ('".mysql_real_escape_string($username)."',".intval($score).")");
}
}twdead2012-04-18 21:26:21
B
10
S
5
G
1
Posts: 37
Reputation: 1,631

Post » Wed Apr 18, 2012 9:36 pm

Sure Thing... But I didn't understand a thing that you just said. Sorry, I'm that incopetent ;\

Could someone just tell me how do I write those variables, in a way that I can understand?
B
6
Posts: 56
Reputation: 712

Post » Wed Apr 18, 2012 10:29 pm

Ty twdead. :) php is brand new to me so the correction is greatly appreciated. :)
B
68
S
21
G
15
Posts: 701
Reputation: 15,604

Post » Thu Apr 19, 2012 1:06 am


Spend some time on php.net and stackoverflow and find out what the best practices are for SQL queries (and find a lot of good code for doing exactly this sort of thing - the community for PHP is pretty good because the language itself is so haphazard.) This is definitely not something you want to jump into without doing the research first.

Oh and I just realized I didn't mention the part of the code I posted above that sets the max range for the filter is '(?)' because I didn't know what to set it to. That's not actually part of the code, just a placeholder.
twdead2012-04-19 01:08:35
B
10
S
5
G
1
Posts: 37
Reputation: 1,631

Post » Thu Apr 19, 2012 1:20 am

@twdead

I'll take your advice. But in order to explain my problems properly, could you please tell me how I can make it interact with constrcut? Just a small example.
B
6
Posts: 56
Reputation: 712

Post » Thu Apr 19, 2012 1:26 am

I haven't used the ajax functions in construct 2 so I couldn't tell you specifically how, sorry.
B
10
S
5
G
1
Posts: 37
Reputation: 1,631

Post » Thu Apr 19, 2012 1:29 am

No problem. I'm ashame of my ignorance, hahaha.

Here, I've read some threads before, and I have some question about this:

"post.php?name=" & Vname & "&score=" & Vscore

Vname is my variable and "name" is my field in PHP, right? Same thing to score.

Then if I write this on construct using the AJAX, it will post my Variables called "vName" and "vScore"?
B
6
Posts: 56
Reputation: 712

Next

Return to How do I....?

Who is online

Users browsing this forum: Artpunk, Axiomaltd, MACPK, nicklowkcmagic, OddConfection, Yahoo [Bot] and 23 guests