-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

2
  • 1
    possible duplicate of Access a JavaScript variable from PHP Commented Sep 6, 2015 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) Commented Sep 6, 2015 at 13:53

1 Answer 1

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.

2
  • how do JavaScript variables to url get? may u know @alkanen Commented Sep 6, 2015 at 14:32
  • I'm sorry, I'm not sure I understand your question =/ Commented Sep 15, 2015 at 15:01

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.