AJAX and PHP: Sending information

0 favourites
  • 15 posts
From the Asset Store
Paypal PHP Supports multiple platforms, Web/html5 Android, IOS. (Including storing transaction data)
  • Hello! :)

    Here is a Link to my capx: 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:

    <?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";

    ?>

    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!

  • A Little bump :)

  • 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?

  • 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).")");

    }

    }

  • 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?

  • Ty twdead. :) php is brand new to me so the correction is greatly appreciated. :)

  • 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.

  • 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.

  • I haven't used the ajax functions in construct 2 so I couldn't tell you specifically how, sorry.

  • 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"?

  • Yeah, that's more or less it.

    Queries are separated as name=value pairs (like variables), with the pairs themselves separated by ampersands, so with "post.php?name=vname&score=vscore" The variable names would be 'name' and 'score' and their values would be 'vname' and 'vscore', respectively.

    In php, this would be recieved in the $_GET array as

    $_GET['name'] = vname

    $_GET['score'] = vscore

  • Best explanation that I've found so far. Thanks a lot!! Really.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Well, I worked a bit on it. Got some help with some students here in my University.

    This is what I got:

    <?php

    $username = $_GET['name'];

    $score = $_GET['score'];

    $acerto = $_GET['Hit'];

    $tempoReacao = $_GET['reactionTime'];

    if (!isset($username) || $username == "") {

        echo "failure";

        exit;

    }

    if (!isset($score) || !is_numeric($score)) {

        echo "failure";

        exit;

    }

    if(!isset($acerto) || !is_numeric($acerto)){

    echo "failure";

        exit;

    }

    if(!isset($tempoReacao)){

    echo "failure";

        exit;

    }

    $entry = $username . "," . $score .";".$acerto.";".$tempoReacao."\n";

    if (!file_put_contents("scores.csv", $entry, FILE_APPEND)) {

        echo "failure";

        exit;

    }

    echo "success";

    ?>

    But I have a question now, a very important hone.

    How do I use it with AJAX?

    Like, I think that I must go this way:

    Keyboard -> On Spacebar Pressed -> Ajax -> Request (request what and how?)

    Thanks!

  • A little bump again :) (Hope it's not agains the forum's rules)

  • Search the website:

    AJAX - Manual

    AJAX - Tutorial

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