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

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Gunner
Posts: 276
Joined: Sun Aug 14, 2011 12:07 am

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

Post 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
Last edited by Gunner on Tue Feb 11, 2014 8:27 am, edited 1 time in total.
Skype: mrvictordiaz
User avatar
a_bertrand
Posts: 1537
Joined: Mon Feb 25, 2013 1:46 pm

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

Post 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.
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
Gunner
Posts: 276
Joined: Sun Aug 14, 2011 12:07 am

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

Post 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?
Skype: mrvictordiaz
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

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

Post 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>
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Gunner
Posts: 276
Joined: Sun Aug 14, 2011 12:07 am

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

Post by Gunner »

crap, wait a second
Last edited by Gunner on Tue Feb 11, 2014 8:21 am, edited 2 times in total.
Skype: mrvictordiaz
User avatar
a_bertrand
Posts: 1537
Joined: Mon Feb 25, 2013 1:46 pm

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

Post by a_bertrand »

I think it's time to learn a bit more JS and what AJAX is and what JSON is maybe...
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
Gunner
Posts: 276
Joined: Sun Aug 14, 2011 12:07 am

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

Post 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
Skype: mrvictordiaz
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

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

Post 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.
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”