-4
<script type="text/javascript">
    var a=10;
</script>

<?php echo $value; ?>

I want to get the value of variable "a" to PHP variable "$value" without ajax request.

Anowar Hossain
  • 583
  • 4
  • 17
  • 3
    PHP runs on the server and Javascript runs in the web browser. If you want to send something in Javascript to PHP you will have to do an ajax request – WizKid Jun 01 '14 at 19:13
  • DId you ignore ALL the suggestions given by SO when asking? – mplungjan Jun 01 '14 at 19:13

4 Answers4

0

javascript is variable value you can't store it to php variable the reason behind is php is a server site language and javascript is only a client language both are independent. for this you can use ajax to send the ajax request to a page with the javascript variable value and there page you can use php code to get the value of that variable like $_GET['a']. may this help you!

Sumit
  • 698
  • 4
  • 11
0

PHP runs on the server side, while JavaScript runs in the client's browser. This means that the PHP code gets executed before the JavaScript, so it isn't possible to directly pass a variable from JavaScript to PHP.

You should look into either sending it via a GET or POST request or, if you need to do it without a page reload, using AJAX. This tutorial might be helpful:

http://www.w3schools.com/jquery/jquery_ajax_get_post.asp

Computerish
  • 9,590
  • 7
  • 38
  • 49
0

The Javascript is executing on the browser and the PHP is executing on the server so you will have to pass the value to the server somehow. Putting it in the querystring is one easy way, for example:

var url = 'http://mysite/file.php?value=' + escape(a);

It depends on what you are doing of course, but if you are making ajax calls or just posting form values, you need to send the value in the server requests.

Jason
  • 1,726
  • 17
  • 19
0

You could do this in one of two ways, either by performing a POST or GET request, or by using a cookie. Both would require a the page to reload however because PHP runs before the page is loaded.

viralpickaxe
  • 481
  • 4
  • 13