AJAX Post Problem

Get help using Construct 2

Post » Wed Sep 12, 2012 9:06 pm

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 Filesdroptank212012-09-12 21:28:45
B
22
S
9
G
3
Posts: 202
Reputation: 3,645

Post » Thu Sep 13, 2012 12:49 am

Are you making a cross-domain request?
Scirra Founder
B
357
S
214
G
72
Posts: 22,945
Reputation: 178,206

Post » Thu Sep 13, 2012 1:27 am

I hope not. It's on localhost, using xampp.
B
22
S
9
G
3
Posts: 202
Reputation: 3,645

Post » Thu Sep 13, 2012 1:54 am

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:
[URL=https://dl.dropbox.com/u/77974757/help2.capx ][/URL]

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. :)
B
67
S
21
G
15
Posts: 701
Reputation: 15,579

Post » Thu Sep 13, 2012 2:26 am

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.
B
22
S
9
G
3
Posts: 202
Reputation: 3,645

Post » Thu Sep 13, 2012 2:40 am

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.
B
67
S
21
G
15
Posts: 701
Reputation: 15,579

Post » Thu Sep 13, 2012 3:02 am

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);
}
B
22
S
9
G
3
Posts: 202
Reputation: 3,645

Post » Thu Sep 13, 2012 3:45 am

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.capxdroptank212012-09-13 03:46:53
B
22
S
9
G
3
Posts: 202
Reputation: 3,645

Post » Thu Sep 13, 2012 3:55 am

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. lanceal2012-09-13 03:56:27
B
67
S
21
G
15
Posts: 701
Reputation: 15,579

Post » Thu Sep 13, 2012 7:46 pm

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.
B
22
S
9
G
3
Posts: 202
Reputation: 3,645

Next

Return to How do I....?

Who is online

Users browsing this forum: cesisco and 15 guests