twilitegxa Posted May 2, 2010 Share Posted May 2, 2010 How do I use php variables in JavaScript? I have data being pulled from two different tables, and what I want to do is perform a calculation when a button is clicked, then output the result. Say for example I have this: php variable named $a php variable named $b So what I'd like to do is this: When this button is clicked, subtract $b from $a, then display that result into a new variable, named $c. Can anyone help get started? I'm very new to JavaScript still. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 2, 2010 Share Posted May 2, 2010 I'm curious as to why you would need to do that. You can do subtraction math with just JavaScript? Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted May 2, 2010 Author Share Posted May 2, 2010 Well, I'm not sure how to do it. What I have is a little game I'm trying to make. The player logs in, chooses their character, then goes to the page where they can fight random monsters. So the random monster is generated and then the player chooses attack. Upon pressing the attack button, I want to take the character's acv (attack combat value), which is being pulled from a table using php, and subtract that number from the monster's hp. But I don't want the monster's hp being updated in the table in the database, just on the page when they are fighting it. Then after they beat the monster (getting the hp down to zero), they will get experience and money and can choose to fight again or do something else. So what I need to be able to do is use the php variables in my JavaScript calculations. Does that help explain what I'm trying to do any better? Can anyone help? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 2, 2010 Share Posted May 2, 2010 I would use AJAX. Have it pull up a PHP file that returns some JSON data about the acv of the player. Then have JavaScript take it from there for all the subtraction. After all is done, send an AJAX request back to a PHP file to update the results. Quote Link to comment 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.