I'm trying to develop a little game and I have run into a problem.

Currently I have the user log in, choose their character, and then go to a page where they can fight random monsters. When they get to the page to fight with the random monsters, the character's information as well as the random monster's information is pulled from tables in my database.

What I would like to do is when the user clicks the Attack button, perform a function that takes the monster's hp ($hp) and subtracts from it the character's attack combat value ($acv1).

Can anyone help me with how I can access the php variables within my JavaScript code and how I can go about doing this type of calculation?

I'd like to store the monster's hp in a javascript variable, so that when the character attacks, the hp can be updated until the character has reduced the monster's hp to zero, in which case a php script should be run that updates the character's table.

Can anyone help me out? I have no clue how to get the values of the php variables to use in my JavaScript, and I am really new to JavaScript anyway, so I might even need help writing the function!

write them to javascript variaqbles

<script type = 'text/javascript'>
var hp=<?php echo $hp; >
var ac1=<?php echo $acv1 ?>
</script>

this would be the perfect place to implement AJAX to update the database with the combat details

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.