0

I have this piece of php code :

<?php
$food = 100;

$food  $food - 10;
?>

and I need to update it so it displays the food variable on the web page immediately without reloading the whole page

This would be the javascript code:

var food = 100;

var food = food - 10;

$("div").html(food); 
2
  • Your Javascript can't touch your PHP because one runs on the client and one runs on the server. Can you clarify your question please? Commented Jun 20, 2016 at 15:49
  • PHP runs on the server so you need to contact the server to get it to do anything - you can't just change parts of the page on the fly unless you're using something like JavaScript/Ajax to contact the server and parse the response. Commented Jun 20, 2016 at 15:52

2 Answers 2

1

If you are wanting to set the initial variable on page load you could insert it into your JavaScript like so

var food = <?=$food?>

This will then allow you to manipulate the value without reloading the page.

Sign up to request clarification or add additional context in comments.

1 Comment

Still processing the data with JavaScript but yeah, if the initial data is coming from PHP this is an option.
0

PHP is a server side language so cannot output data like you want with out refreshing. Javascript can do this because it is client side.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.