7

I have declared a javascript variable ,

 var myJavascriptVar = 12345;

And unable to assign that value to php variable;

 $myPhpVar = 'myJavascriptVar'; 

I know Ajax may be the solution of my problem. But i don't know how to use Ajax and solve the problem.

<html>
    <body>
        <script>
            var myJavascriptVar = 12345;
            <?php $myPhpVar='myJavascriptVar';?> 
        </script>
        <?php echo $myPhpVar; ?>
     </body>
</html>
Punit Gajjar
  • 4,937
  • 7
  • 35
  • 70
DKBHOI
  • 348
  • 2
  • 6
  • 17
  • 1
    You sure you want to give a JS value to PHP? PHP doesn't work on client side. What you are making will give that php the value only for once from your code. It wont give it to php live on client side like that – Hanky Panky Feb 07 '14 at 05:42
  • Learn about forms to send data to PHP easily and than learn AJAX for dynamic data exhange. – Shiva Avula Feb 07 '14 at 05:43

11 Answers11

17

Using Cookie is the better solution i think -

<script> document.cookie = "myJavascriptVar = " + myJavascriptVar </script>
<?php
     $myPhpVar= $_COOKIE['myJavascriptVar'];
?>
  • 5
    This won't work until the SECOND time the page is viewed. The PHP code executes *on the server*, *before* it submits the page to the browser. The ` – ToolmakerSteve Oct 16 '19 at 08:36
3

Try using ajax with jQuery.post() if you want a more dynamic assignment of variables.

The reason you can't assign a variable directly is because they are processed in different places.

It's like trying to add eggs to an already baked cake, instead you should send the egg to the bakery to get a new cake with the new eggs. That's what jQuery's post is made for.

Alert the results from requesting test.php with an additional payload of data (HTML or XML, depending on what was returned).

$.post( "test.php", { name: "John", time: "2pm" })
  .done(function( data ) {
    alert( "Data Loaded: " + data );
  });
1

PHP is server side language and JS is client side.best way to do this is create a cookie using javascript and then read that cookie in PHP

<script type="text/javascript">
    document.cookie = "myJavascriptVar =12345";
</script>

<?php 
   $phpVar =  $_COOKIE['myJavascriptVar'];

   echo $phpVar;
?>
Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68
  • 3
    This won't work until the SECOND time the page is viewed. The PHP code executes *on the server*, *before* it submits the page to the browser. The ` – ToolmakerSteve Oct 16 '19 at 08:36
1
$msg = "<script>var n=document.getElementById('fil').val; document.write(n);</script>";

echo $msg;
1

I have a better solution: Here, in the .php file, I have a variable called javascriptVar. Now, I want to assign the value of javascriptVar to my php variable called phpVar. I do this by simply call javascript variable by document.writeln in the script tag.

    <?php 
        echo "<script>
                var javascriptVar = 'success';
             </script>";
        
    
        $phpVar = "<script>document.writeln(javascriptVar);</script>";
    ?>
Nazmul81
  • 150
  • 1
  • 3
  • 13
  • 1
    Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – jasie Oct 13 '20 at 06:24
  • Now, I try my best to improve my solution to better understand.Thanks – Nazmul81 Oct 28 '20 at 12:29
  • This makes no sense. It does not assign the value of the javascript variable to $phpVar. The value of $phpVar is the exact string which was assigned to it - i.e. a snippet of Javascript. Since PHP runs on the server and JS runs on the client, the JS code will not be executed until after the assignment to the PHP variable has completed, and indeed the entire PHP script has completed. Demo: https://3v4l.org/oDSl5 – ADyson Sep 23 '22 at 13:59
0

I guess you can use cookies for it.

1) First add a cookie jquery plugin.

2) Then store that window width in a cookie variable.

3) Access your cookie in PHP like $_COOKIE['variable name'].

http://www.w3schools.com/php/php_cookies.asp

hizbul25
  • 3,829
  • 4
  • 26
  • 39
0

Javascript will be interpreted in Client's browser and you can not assign it to PHP variable which is interpreted on SERVER .

Feasible Solution : You can submit the Javascript value via ajax or through form submit.

Dimag Kharab
  • 4,439
  • 1
  • 24
  • 45
0

You should see these links:

Assign Javascript value to PHP variable

Passing Javascript vars to PHP

Or you can use AJAX request or POST and GET methods to achieve this.

Snippet below may helpful for you:

<?php 
   if(isset($_POST['isSubmit']))
   {
      // do something here
      echo $_POST["name"];
   }
?> 

<form action="<?php $_SERVER['PHP_SELF'];?>" method="POST"> 
Your name: <input type="text" name="name" /> 
<input type="Submit" value="Submit" name="isSubmit"> 
</form> 
Community
  • 1
  • 1
Sohail xIN3N
  • 2,951
  • 2
  • 30
  • 29
0

Use this code to solve your problem.

<script type="text/javascript">
  var abc= 'this is text';
 <?php $abc = "<script>document.write(abc)</script>"?>   
</script>
<?php echo $abc;?>

<script> document.cookie = "myJavascriptVar = " + myJavascriptVar </script>
<?php
     $myPhpVar= $_COOKIE['myJavascriptVar'];
?>
-2
<html>
    <body>
        <script>
            myvar=12345;
        </script>
        <?php $var = "<script>return myvar;</script>" ?>
    </body>
</html>
  • Thank you for your answer. It looks like the reason that some may downvote you is that this does not _assign_ a javascript variable, this only returns one. As well, dropping inline ` – Metagrapher Jun 28 '21 at 03:19
-2

Please use this code and it will works fine in all cases.

<script type="text/javascript">
var width=screen.width;
</script>
<?php
     echo $myPhpVar= "<script>document.writeln(width);</script>";
?>
Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
  • 1
    _...use this code and it will works fine in all cases..._ Please explain **why** this code will work fine in all cases. – B001ᛦ Jul 13 '21 at 11:01