Page 1 of 1
Little help with some basic network coding? (Javascript)
Posted: Tue Jan 24, 2012 2:08 am
by Kronos
Well I've been trying for about a month now, but I can't seem to figure a way to save the players X and Y coordinates onto my PHP/MySQL server. I've tried passing the variables through the players cookies, bridging them with HTML and mirroring player movements in PHP, but nothings worked.
In my Javascript they are simply X and Y, but I can't seem to find a way to change them into PHP, something like $xValue and $yValue. Yes I understand that javascript is client side and php is server side, everywhere else on the internet says it's impossible to convert them. But does anyone know a workaround maybe?
Thanks in advance~
Re: Little help with some basic network coding? (Javascript)
Posted: Tue Jan 24, 2012 4:03 am
by Ark
You could use jquery and ajax to make it happen:
Code: Select all
<html>
<head>
<script type="text/javascript" src="jquery/jquery.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
var $data=$("input").val();
$.post("probando.php",{data:$data});
$("input").val("");
});
});
</script>
</head>
<body>
<input type="text" />
<br/>
<button>click to send data</button>
</body>
</html>
Now the server side:
Probando.php
Code: Select all
<?php
require_once'connect.php';
mysql_query("INSERT INTO `test`(testdata) VALUES('$_POST[data]')") or die('error');
?>
Re: Little help with some basic network coding? (Javascript)
Posted: Tue Jan 24, 2012 7:44 am
by Chris
You might want to look into vectors.
Code: Select all
class Vector2 {
public $x;
public $y;
function __construct($x=0, $y=0)
{
$this->set($x, $y);
}
function set($x, $y)
{
$this->x = $x;
$this->y = $y;
}
}
Code: Select all
class Player {
public $position;
function __construct()
{
$this->position = new Vector2();
}
}
Code: Select all
$myPlayer = new Player();
$myPlayer->position->set(13, 37);
echo $myPlayer->position->x; // 13
echo $myPlayer->position->y; // 37
Re: Little help with some basic network coding? (Javascript)
Posted: Fri Feb 03, 2012 1:25 am
by Kronos
Could I ask you guys to explain the codes you've both displayed in a fair amount of detail. I'm really green with the code still, and couldn't quite figure out how to implement these.
And again, thank you in advance~
Re: Little help with some basic network coding? (Javascript)
Posted: Fri Feb 03, 2012 4:21 am
by Ark
Basically what I did there is binding a function when the click event happens to the <button></button> element and that function is:
Code: Select all
<script>
$(document).ready(function(){
$("button").click(function(){
var $data=$("input").val();
$.post("probando.php",{data:$data});
$("input").val("");
});
});
</script>
Saving the data in a variable $data (in your case it could be two variables x and y), then i'm sending the info(using AJAX) on a $.post() function to a .php file:
Code: Select all
<?php
require_once'connect.php';
mysql_query("INSERT INTO `test`(testdata) VALUES('$_POST[data]')") or die('error');
?>
you don't need to open the .php file, as ajax does it dynamically and sends the data with the $_POST method, and the .php saves it in the mysql.
source:
http://www.w3schools.com/jquery/jquery_syntax.asp
http://www.w3schools.com/jquery/ajax_post.asp
And hall's wisdom
