AJAX Post Problem

  • I am having an issue figuring out AJAX Post. Basically i want the player to be able to click a button, AJAX sends info to a php page which in turn updates a stat in a mysql database.

    So far it isn't working. Can anyone tell me what I may be doing wrong?

    Files: DropBox Files

  • Are you making a cross-domain request?

  • I hope not. It's on localhost, using xampp.

  • Thats way out of my league for php programming. I utilize one php file for each type of request. I know it takes up more room and is redundant but its more easy for me to get my head around. What your doing in the capx looks fine though. Heres a copy of one of my php files(work in progress I know I need to add the safety check like stripslashes :)) Anyways heres what works for me:

    And the php file:

    <?php

    $minx = $_REQUEST['min_x'];

    $miny = $_REQUEST['min_y'];

    $terrain = $_REQUEST['ter'];

    $SFood = $_REQUEST['food'];

    $SWood = $_REQUEST['wood'];

    $SStone = $_REQUEST['stone'];

    $SOre = $_REQUEST['ore'];

    $SGems = $_REQUEST['gems'];

    //Connect To Database

    $hostname='#####';

    $username='#####;

    $password='#####';

    $dbname='#####';

    $usertable='#####';

    $MapID = 'Map_ID';

    $MapX = 'Map_X';

    $MapY = 'Map_Y';

    $MapTYPE = 'Map_TYPE';

    $Food = 'Map_FOOD';

    $Wood = 'Map_WOOD';

    $Stone = 'Map_STONE';

    $Ore = 'Map_ORE';

    $Gems = 'Map_GEMS';

    $con = mysql_connect($hostname,$username,$password);

    if (!$con)

    {

    die('Could not connect: ' . mysql_error());

    }

    mysql_select_db($dbname, $con);

    $sql="INSERT INTO $usertable ($MapX, $MapY, $MapTYPE, $Food, $Wood, $Stone, $Ore, $Gems)

    VALUES

    ('$minx','$miny','$terrain','$SFood','$SWood','$SStone','$SOre','$SGems')";

    if (!mysql_query($sql,$con))

    {

    die('Error: ' . mysql_error());

    }

    echo "1 record added";

    mysql_close($con);

    ?>

    Just remember this isn't an effective php script yet. Still need the safety features so feel free to take from it what you want just be warned it's currently not secure enough for commercial use. :)

  • I'll examine your php. In the mean time, here is my newest attempt for the php code:

    <?php

    include 'connect.php';

    session_start();

    ?>

    <center>

    </center><br><br>

    <?php

    if (isset($_SESSION['player']))

    {

        $player=$_SESSION['player'];

        $userstats="SELECT * from km_users where playername='$player'";

        $userstats2=mysql_query($userstats) or die("Could not get user stats");

        $userstats3=mysql_fetch_array($userstats2);

    if (isset($_POST['score'])){   

        

        $score=$_POST['score'];

        }

        $updatestats="update km_users set skillpts=skillpts+$score where ID='$userstats3[ID]'";

                mysql_query($updatestats) or die("Could not update stats");

                

         

    }else{

         echo 'Your score wasnt passed in the request.';

    }

    ?>

    My attempt is to update existing data in the database by having the POST data from AJAX added to the users skillpts in the database. So far nothing has yet worked and I keep getting a lot of "Undefined varibale" errors when trying to do it the way that makes logical sense.

    I must be missing something.

  • you know what, are you using godaddy? I am and for some reason it will not let me use localhost. I have to plug in the server address when creating the mysql connection along with the password. Had a freehost before I think it wass fii.me that had the same problem. Maybe try plugging in the server address and password for a trial run? BTW feel free to tell me how to secure my php code. :) I would gladly accept the guidance.

  • I'm using apache server and xampp on my laptop as host. Comes with everything I need including phpmyadmin.

    As far as securing your php code, you seem to know way more about this stuff than I do. I'm a novice. But...the only thing I've learned of is adding the below to your mysql connect coding:

    if(!get_magic_quotes_gpc())

    {

    $_GET = array_map('mysql_real_escape_string', $_GET);

    $_POST = array_map('mysql_real_escape_string', $_POST);

    $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);

    }

    else

    {

       $_GET = array_map('stripslashes', $_GET);

       $_POST = array_map('stripslashes', $_POST);

       $_COOKIE = array_map('stripslashes', $_COOKIE);

       $_GET = array_map('mysql_real_escape_string', $_GET);

       $_POST = array_map('mysql_real_escape_string', $_POST);

       $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);

    }

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I have modified the php to the below, however, it only updates the database if I physically plug in the url plus ?&score=1 into the browser bar. Clicking on the button to call up the AJAX doesn't run the function.

    <?php

    include 'connect.php';

    session_start();

    ?>

    <center>

    </center><br><br>

    <?php

    if (isset($_SESSION['player']))

    {

        $player=$_SESSION['player'];

        $userstats="SELECT * from km_users where playername='$player'";

        $userstats2=mysql_query($userstats) or die("Could not get user stats");

        $userstats3=mysql_fetch_array($userstats2);

    if (isset($_REQUEST['score'])){   

        

        $score=$_REQUEST['score'];

        $updatestats="update km_users set skillpts=skillpts+$score where ID='$userstats3[ID]'";

                mysql_query($updatestats) or die("Could not update stats");

                

         

    }else{

         echo 'Score not passed in the request.';

    }

    }

    ?>

    Attached capx: http://dl.dropbox.com/u/44787590/corporal_sanders_1/Corporal_Sanders_1.capx

  • I don't think construct2 utilizies the localhost directory path. Change it to the physical http path in the event.

    IE:

    "localhost/killmonster/sanderswin1.php?&score=1"

    to

    "http://colsandersisrad.com/killmonster/sanderswin1.php?score=1"

    I think that may solve your problem. I think the localhost is only for the php side. Thats all I can see in your ajax request. But make sure you add it an on complete event and on error event to make verify the score updated.

  • Did not work on either hosted site or on localhost server, even after adding: header('Access-Control-Allow-Origin: *');

    I am at a total loss.

  • Try the php I had posted earlier and just change the variables around to meet your needs. I know it works because I use it on my server with construct2. If it works you'll have a good starting point that just needs the security stuff put in.

  • You can find an example of my working Ajax construct2 at the following link.

    scirra.com/forum/what-am-i-doing-wrong-here_topic56751.html

    Between the php example and the capx in the above link I'm hoping you can get yourself up and running. :)

  • Ps Ashley

    I can't find documentation on the data field in Ajax requests. How does it work?

  • I think I know what's wrong. On the event sheet I have Button->On Clicked.

    Instead I need to add the mouse plugin and have Mouse->left click on Button.

    I've been clicking on the dang button expecting it to run the AJAX request but never added the mouse control to do so.

    <img src="smileys/smiley18.gif" border="0" align="middle" />

  • Disregard...that didn't work either. Having a look at your capx now.

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