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;
?>
5
  • 1
    Possible duplicate of How to pass variables and data from PHP to JavaScript? Commented Apr 22, 2017 at 8:01
  • 3
    Welcome to SO. Please step first to HELP center, then visit GET started, and finally, read How to Ask Question and provide a MCVE : Minimal, Complete, and Verifiable Example. 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 Commented Apr 22, 2017 at 8:02
  • Did you try to use ajax? Commented Apr 22, 2017 at 8:16
  • 1
    @rymdmaskin, OP wants to do the opposite: passing a variable from JS to PHP. Commented Apr 22, 2017 at 9: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. Commented Apr 22, 2017 at 9:56

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

Comments

-1
<script>
    var jsvar = "Name";
</script>

<?php
    $phpVar = "<script>document.write(jsvar);</script>";
    echo $phpVar;
?>

Comments

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.