Little help with some basic network coding? (Javascript)

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
Kronos
Posts: 20
Joined: Fri Dec 30, 2011 6:49 pm

Little help with some basic network coding? (Javascript)

Post 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~
Peace~
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: Little help with some basic network coding? (Javascript)

Post 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');
?>
Orgullo Catracho
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Little help with some basic network coding? (Javascript)

Post 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
 
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Kronos
Posts: 20
Joined: Fri Dec 30, 2011 6:49 pm

Re: Little help with some basic network coding? (Javascript)

Post 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~
Peace~
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: Little help with some basic network coding? (Javascript)

Post 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 :roll:
Orgullo Catracho
Post Reply

Return to “Advanced Help and Support”