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;
?>
-
1Possible duplicate of How to pass variables and data from PHP to JavaScript?rymdmaskin– rymdmaskin2017年04月22日 08:01:55 +00:00Commented Apr 22, 2017 at 8:01
-
3Welcome 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 THISOldPadawan– OldPadawan2017年04月22日 08:02:33 +00:00Commented Apr 22, 2017 at 8:02
-
Did you try to use ajax?Adhan Timothy Younes– Adhan Timothy Younes2017年04月22日 08:16:52 +00:00Commented Apr 22, 2017 at 8:16
-
1@rymdmaskin, OP wants to do the opposite: passing a variable from JS to PHP.Jordi Nebot– Jordi Nebot2017年04月22日 09:25:16 +00:00Commented 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.rymdmaskin– rymdmaskin2017年04月22日 09:56:49 +00:00Commented Apr 22, 2017 at 9:56
2 Answers 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 :
answered Apr 22, 2017 at 8:03
aprogrammer
1,7781 gold badge11 silver badges20 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
<script>
var jsvar = "Name";
</script>
<?php
$phpVar = "<script>document.write(jsvar);</script>";
echo $phpVar;
?>
Richa
7911 gold badge14 silver badges26 bronze badges
Comments
default