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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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