Jump to content

Performing calculations using php variables?


twilitegxa

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

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.