Page 1 of 1

I passed variable from php to ajax call and it's not- solved

Posted: Mon Feb 10, 2014 12:28 pm
by Gunner
So I have this file where the ajax call starts

Code: Select all

<script type="text/javascript">
// if the user moves down
	$(document).ready(function(){
		$("#down").click(function(){
			$.ajax({
				type: "POST",
				url: "move.php",
				cache: false,
				data: {move: "down"},
				success: function(html){
					$("#"+tile).html(html);
				}
			});
		});
	});
</script>
and this move.php

Code: Select all

<?php
include "config/connect.php";
session_start();
$player = $_SESSION['player'];
if (isset($_POST['move'])){
	$move = $_POST['move'];
	if ($move == 'down'){
		mysql_query("Update movement set tile=tile+11 where name='$player'") or die (mysql_error());
		$gettingtileinfo = mysql_fetch_array(mysql_query("Select * from movement where name='$player'"));
		$tile = $gettingtileinfo['tile'];
		echo '<div id="actor1_down"></div>';
		
	}
}
?>

<script type="text/javascript">
var tile = tile_<? echo $tile; ?>;
</script>
The problem is that the variable called 'tile' won't get passed to my ajax call.. which is really strange..
btw I already changed this

Code: Select all

$("#"+tile).html(html);
to this

Code: Select all

$("#tile_1").html(html);
to just try if it could work and it works perfectly.. So the problem over here is the variable passsing..

any help would be appreciated thanks

Re: I passed variable from php to ajax call and it's not wor

Posted: Mon Feb 10, 2014 12:53 pm
by a_bertrand
You can't pass variables like that from PHP to your JS. If you want to do that, either pass all the data (HTML and variables) as JSON or XML and parse it in your JS to extract the variables and the HTML.

Re: I passed variable from php to ajax call and it's not wor

Posted: Mon Feb 10, 2014 2:26 pm
by Gunner
So I had to call 2 ajax? The first which is to pass the variables and then the 2nd to do the stuff?

Re: I passed variable from php to ajax call and it's not wor

Posted: Mon Feb 10, 2014 4:01 pm
by Chris
You could use eval to do that. In english we would say: Don't do that.

You want to use JSON parse an array and send a list of things to the client:

Code: Select all

<?php
include "config/connect.php";
session_start();
$player = $_SESSION['player'];
if (isset($_POST['move'])){
   $move = $_POST['move'];
   if ($move == 'down'){
      mysql_query("Update movement set tile=tile+11 where name='$player'") or die (mysql_error());
      $gettingtileinfo = mysql_fetch_array(mysql_query("Select * from movement where name='$player'"));
      $data = array();
      $data['tileId'] = 'tile_' . $gettingtileinfo['tile'];
      $data['tileView'] = '<div id="actor1_down"></div>';
      echo json_encode($data);
   }
}
?>

Code: Select all

<script type="text/javascript">
   $(document).ready(function(){
      $("#down").click(function(){
         $.ajax({
            type: "POST",
            url: "move.php",
            cache: false,
            dataType : 'json',
            data: {move: "down"},
            success: function(data){
               $("#" + data.tileId).html(data.tileView);
            }
         });
      });
   });
</script>

Re: I passed variable from php to ajax call and it's not wor

Posted: Tue Feb 11, 2014 8:04 am
by Gunner
crap, wait a second

Re: I passed variable from php to ajax call and it's not wor

Posted: Tue Feb 11, 2014 8:10 am
by a_bertrand
I think it's time to learn a bit more JS and what AJAX is and what JSON is maybe...

Re: I passed variable from php to ajax call and it's not wor

Posted: Tue Feb 11, 2014 8:22 am
by Gunner
thanks Chris, I know it should be working, but for some reason it's not working here.. Is it even possible that I'm using the outdated PHP version?? GOD PLEASE I already tried it on my practice file and the variable still fails to pass surprisingly. Even copied your whole code. I also tried to convert all the variables to normal words to just try, but returns the same.

a_bertrand yes I'm still advancing out a bit, for me the best way to learn is to get in the code itself

update: nope I'm not using the outdated PHP version.

update again: nope, it's working normally.. when I tried another time on a different file. I'll just check every line out whats wrong.

thanks everyone

Re: I passed variable from php to ajax call and it's not- so

Posted: Tue Feb 11, 2014 9:18 pm
by Jackolantern
I will be honest: PHP AJAX responses can be fiddley to get working in the beginning. When I first started messing with AJAX a few years ago, this was a huge headache for me.