BellQuestWars Posted April 27, 2011 Share Posted April 27, 2011 Hey guys, another question. I have a program (javascript) that needs a number. I need to get that number from a php document. I currently have this: Javascript: var x="<?= $x ?>"; <?php $x = 5; ?> But that does nothing (i found it through a google search.) Should I just use AJAX for this? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/234804-passing-a-variable-from-php-to-javascipt/ Share on other sites More sharing options...
pastcow Posted April 27, 2011 Share Posted April 27, 2011 You need to make sure your PHP is being executed. It could be that your javascript is inside a .js file which isnt processed by php. An alternate solution might be to do <script src="bleh.php"> and have the php output something along the lines of <script>var x = 5</script> etc... It might well be better to use AJAX but it depends on the situation... Quote Link to comment https://forums.phpfreaks.com/topic/234804-passing-a-variable-from-php-to-javascipt/#findComment-1206686 Share on other sites More sharing options...
Pikachu2000 Posted April 27, 2011 Share Posted April 27, 2011 Don't use short "quick echo" tag syntax <?= use the full <?php echo syntax instead. Short tags are disabled by default now. Quote Link to comment https://forums.phpfreaks.com/topic/234804-passing-a-variable-from-php-to-javascipt/#findComment-1206732 Share on other sites More sharing options...
BellQuestWars Posted May 1, 2011 Author Share Posted May 1, 2011 Okay, so I have a script that uses a php variable to get a number, and the php variable is collected from the database. The problem is the php variable is a string, and the Javascript variable is a number, so I need a way to convert a string into a number. Heres my php script: <?php session_start(); require_once("connect.php"); $result = mysql_query("SELECT * FROM members WHERE username='$_SESSION[username]'"); $x = $row[x]; $y = $row[y]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/234804-passing-a-variable-from-php-to-javascipt/#findComment-1209217 Share on other sites More sharing options...
coffeecup Posted May 9, 2011 Share Posted May 9, 2011 i would assume the PHP output needs to be formated into XHTML tags and then Javascript can be used to extract the value. if you want to filter a numeric value with Javascript look into the parseFloat method. Quote Link to comment https://forums.phpfreaks.com/topic/234804-passing-a-variable-from-php-to-javascipt/#findComment-1212604 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.