-1

this is simple example i want java script class variable to php echo please fix the following example to php echo value out put

<!DOCTYPE html>
<html>
<body>

<div class="example">The Book</div>


<? echo '<div class="example">The Book</div>'; ?>


<script>
function myFunction() {
    var x = document.getElementsByClassName("example");
    x[0].innerHTML = "Hello World!";
}
</script>

</body>
</html>

out put of java-script "The book" but php echo is '<div class="example">The Book</div>' Targeted out put of php The book

Henok
  • 35
  • 6
  • 1
    possible duplicate of [Access a JavaScript variable from PHP](http://stackoverflow.com/questions/2338942/access-a-javascript-variable-from-php) – ScottMcGready Sep 06 '15 at 13:23
  • You don't call your function anywhere so it won't get executed. Plus php is a serverside and js a clientside language. Serverside gets executed before clientside (xhr/ajax is a workaround which kind off smoothen the borders/lines of both) – TobiasJ Sep 06 '15 at 13:53

1 Answers1

2

Javascript is run on the client, PHP is run on the server. If you want PHP to use the values calculated in javscript you first have to send it to the server, either through something like AJAX, or perhaps just a query string if you don't mind loading a new page or loading the same page again but with a query string appended to the URL.

alkanen
  • 636
  • 6
  • 16