-2

In the following example of code I want to store the JavaScript variable val into a PHP variable $ff.

<script>
$(document).ready(function() {

    $("#type").change(function() {
       var val = $(this).val();
       alert(val);
       <?php $ff = "Greenway"; ?>
   });

  });
</script>

The var val stores the name of shops, e.g. "GreenWay", "Tropin". I want to store this data in a PHP variable. I tried doing <?php $ff = $_GET['val']; ?> but it doesn't work.

vard
  • 4,057
  • 2
  • 26
  • 46
Ramesh Pardhi
  • 407
  • 5
  • 18

1 Answers1

0

You cant do it because PHP is server side language and javascript client side.

It means PHP is executed in a first time, the result (html + css + js) is sent to the client. Then when the client receive it, JS can be executed.

If you want JS to send a variable to the server (PHP), you can use AJAX : jQuery AJAX

ThinkTank
  • 1,187
  • 9
  • 15