Hello everyone,I need a help to back values from php to javascript...

Listen:

__hasInventory = true;
	_iMatrix = [];

	$.ajax({
		url: 'index.php?acao=test',
		dataType: 'script',
		type: 'post',
		data: {mode: 0},
		success: function () {

If is all Ok with post method,I'd like it back with results passing PHP results to Javascript:

Something like:

_iMatrix[id]['descricao']

      _iMatrix[id]['hp']	

      _iMatrix[id]['nome']

Can someone Help me please = (((((

THanks

Helinxed,

If I understand correctly, then you can pass your php object/array back by JSON encoding it.

Your code will be something like this :

...
$arr = array(
  'descricao' => 'A',
  'hp' => 'B',
  'nome' => 'C'
);
exit(json_encode($arr));
//at this point, id  must be defined
$.ajax({
  url: 'index.php?acao=test',
  type: 'POST',
  data: {mode: 0},
  dataType: "JSON",
  success: function(data){
    _iMatrix[id] = data;
  }
});

untested

Airshow

Oh,I'm almost there ^^

This script is for an Online Map-Game...I need it to get info in map for each player

This _iMatrix[[B]id[/B]] is the ID of each Player in Map.

_iMatrix[[B]999[/B]] -> _iMatrix[[B]999[/B]] get the info descricao of the player 999

There's some way of get each info by ID of the player?

Thanks a lot!

Helinxed,

Aha, that's a bit different.

Do you want :-

  • to request all players in a single request?
  • to request each player by id, one at a time?

Airshow

I have a table in Mysql called Map. There's: ID , MAP_ID, DESCRICAO, HP, NOME

I'd like to get All players that's in the Map_ID.

Select * FROM Map where MAP_ID = '1'

Get this result and pass it to javascript BY ID of the player. So I guess request each player in the map by id one at a time...

I'll make this request in each 4 seconds to update users in map in real time

update = setInterval(function () {

            }, 4000);

Thank you a lot for helping me bro.

Helinxed,

I think you probably want something like this:

$map_id = (isset($_REQUEST['map_id'])) ? intval($_REQUEST['map_id']) : false;
if($map_id){
	$sql = "Select * FROM Map where MAP_ID = $map_id";
	//execute $sql here to give the results as (array) $rows
	exit(json_encode($rows));
}
else{
	exit(json_encode(array()));
}
function fetch_matrix(map_Id){
	$.ajax({
		url: 'index.php',
		type: 'POST',
		data: {acao:'test', mode:0, map_id:map_id},
		dataType: "JSON",
		success: function(data){
			return data;
		}
	});
}


var _iMatrix = fetch_matrix(1);

You may need to work on this a bit to get exactly what you want.

Airshow

$map_id = (isset($_REQUEST['map_id'])) ? intval($_REQUEST['map_id']) : false;
if($map_id){
	$sql = "Select * FROM Map where MAP_ID = $map_id";
        while($rows = mysql_fetch_array($sql)) {
	//execute $sql here to give the results as (array) $rows
	exit(json_encode($rows));
}
}
else{
	exit(json_encode(array()));
}
function fetch_matrix(map_Id){
	$.ajax({
		url: 'index.php',
		type: 'POST',
		data: {acao:'test', mode:0, map_id:map_id},
		dataType: "JSON",
		success: function(data){
			return data;
		}
	});
}
 
var _iMatrix = fetch_matrix(1);

In javascript the results would be like this:?

_iMatrix['id']['descricao']

Try:

$map_id = (isset($_REQUEST['map_id'])) ? intval($_REQUEST['map_id']) : false;
if($map_id){
	$sql = "Select * FROM Map where MAP_ID = $map_id";
        while($rows = mysql_fetch_array($sql)) {
	//execute $sql here to give the results as (array) $rows
	exit(json_encode($rows));
}
}
else{
	exit(json_encode(array()));
}
function fetch_matrix(map_id, target){
	$.ajax({
		url: 'index.php',
		type: 'POST',
		data: {acao:'test', mode:0, map_id:map_id},
		dataType: "JSON",
		success: function(data){
			$(data).each(function(){
				target[this.ID] = {
					descricao: this.descricao,
					hp: this.hp,
					nome: this.nome
				};
			});
		}
	});
}

var _iMatrix = [];
fetch_matrix(1, _iMatrix);

Airshow

Thank you dude

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.