0

here is my sample code hope you can help me. how can i pass a JavaScript variable to php variable?

<script>
    var jsvar = "name";
</script>

<?php
    $phpVar= jsVar;
?>
  • 1
    Possible duplicate of [How to pass variables and data from PHP to JavaScript?](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – rymdmaskin Apr 22 '17 at 08:01
  • 3
    Welcome to SO. Please step first to [HELP center](http://stackoverflow.com/help), then visit [GET started](https://meta.stackoverflow.com/questions/252149/how-does-a-new-user-get-started-on-stack-overflow), and finally, read [How to Ask Question](http://stackoverflow.com/help/how-to-ask) and provide a [MCVE : Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). But keep in mind that *SO is a community that helps*, and **no one will do all the work for you**. You can read [THIS](http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php?rq=1) – OldPadawan Apr 22 '17 at 08:02
  • Did you try to use ajax? – Adhan Timothy Younes Apr 22 '17 at 08:16
  • 1
    @rymdmaskin, OP wants to do the opposite: passing a variable from JS to PHP. – Jordi Nebot Apr 22 '17 at 09:25
  • @JordiNebot Yes, but it is still the same idea behind it. PHP is server-side language and JavaScript in this case is Client-side. You still need to exchange data between the server and client. – rymdmaskin Apr 22 '17 at 09:56

2 Answers2

2

You can't. As others said million times, JavaScript is Client Side Language meanwhile PHP is Server Side. You can't manipulate Server Side language via Client Side Language. You should pass your JavaScript variable to Server with AJAX and do your operation.

Sources that you can find useful :

Access a JavaScript variable from PHP

How to pass variables and data from PHP to JavaScript?

Community
  • 1
  • 1
aprogrammer
  • 1,764
  • 1
  • 10
  • 20
-1
<script>
    var jsvar = "Name";
</script>

<?php
    $phpVar = "<script>document.write(jsvar);</script>";
    echo $phpVar;
?>
ric
  • 627
  • 1
  • 12
  • 23