Jump to content

Passing a Variable from PHP to Javascipt.


BellQuestWars

Recommended Posts

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!

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...

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];
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.