-3

I want to pass JavaScript variables to PHP variables.

var a=0 to $a

<script>
    var a=0;
</script>

    <?php

        $a = 0;
    ?>
Dave
  • 5,108
  • 16
  • 30
  • 40
  • 2
    You need to make an ajax request to the server to execute a php script. When your javascript gets executed, the request you are referencing, has already finished. – jeroen Feb 13 '19 at 15:47
  • You can choose AJAX if your looking for dynamic interaction on your page but if you just need to pass data to PHP in a very basic way then you can choose to just redirect the user to a URL which puts the variables into the GET request. You can also choose to populate `
    ` inputs if your data is based on user interaction.
    – MonkeyZeus Feb 13 '19 at 15:51
  • @WorapradNikesri You really need to look into the difference between front end and back end. It seems like you have a fundamental misunderstanding on their purposes and how they function. PHP Runs once at the server, renders an HTML page, and ships that page to the client. the client executes Javascript. – Marie Feb 13 '19 at 16:33
  • Possible duplicate of [Passing JavaScript array to PHP through jQuery $.ajax](https://stackoverflow.com/questions/2013728/passing-javascript-array-to-php-through-jquery-ajax) – andy magoon Feb 13 '19 at 17:50

1 Answers1

0

Because JS runs on the client (the browser) and PHP runs on the server, there is no way for JS to talk directly to PHP, unless you include some heft HTTP-level communication, either via a FORM submit or AJAX (Your PHP can write directly out to the JS before it is sent to the browser, but not vice-versa)

MikeB
  • 580
  • 3
  • 18