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>
2
  • 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 Commented Feb 7, 2014 at 5:42
  • Learn about forms to send data to PHP easily and than learn AJAX for dynamic data exhange. Commented Feb 7, 2014 at 5:43

11 Answers 11

17

Using Cookie is the better solution i think -

<script> document.cookie = "myJavascriptVar = " + myJavascriptVar </script>
<?php
     $myPhpVar= $_COOKIE['myJavascriptVar'];
?>
Sign up to request clarification or add additional context in comments.

1 Comment

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 <script> block executes in the browser, as it builds the page. There won't be any cookie yet, when execute $myPhpVar= $_COOKIE...
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 );
  });

Comments

2

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;
?>

1 Comment

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 <script> block executes in the browser, as it builds the page. There won't be any cookie yet, when execute $phpVar= $_COOKIE...
1
$msg = "<script>var n=document.getElementById('fil').val; document.write(n);</script>";

echo $msg;

1 Comment

Edit your answer, to add some description. Not new comment.
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>";
    ?>

3 Comments

Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation 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.
Now, I try my best to improve my solution to better understand.Thanks
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: 3v4l.org/oDSl5
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

Comments

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.

Comments

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> 

Comments

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'];
?>

Comments

-1

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>";
?>

1 Comment

...use this code and it will works fine in all cases... Please explain why this code will work fine in all cases.
-2
<html>
    <body>
        <script>
            myvar=12345;
        </script>
        <?php $var = "<script>return myvar;</script>" ?>
    </body>
</html>

1 Comment

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 <script> tags into your markup usually a non-recommended development practice. softwareengineering.stackexchange.com/questions/86589/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.