1

Possible Duplicate:
How to pass JavaScript variables to PHP?

How to assign on this php variable assign javasript value

<script type="text/javascript">

var var_example = "2222";

</script>

<?php 
  echo $var_example_php =   ?        

  /// How to assign on this php variable asign javasript value 
?>
5
  • 4
    There has been 100 of questions exactly dealing with same question on SO Commented Oct 27, 2012 at 5:45
  • You commented that you want to assing PHP value to javascript, but your example shows that you are assigning javascript variable value to php Commented Oct 27, 2012 at 5:47
  • you can do it with ob_start() function.. check out my answer Commented Oct 27, 2012 at 6:27
  • Of course there are ways to do that, but they are not simple/elegant. Commented Jan 29, 2017 at 8:38
  • (Stupid rule the one with editing your post only 5 mins !) JS to PHP: Use hidden input in PHP, set valute in JS, submit page. Not elegant, though. But if you really need to do that... PHP to JS: it is currrently done in scripts. Commented Jan 29, 2017 at 8:47

4 Answers 4

10

You simply cannot do that, you need to understand the difference between client/server side programming, you cannot assign Javascript value to PHP variable, yea but you can assign PHP value to your javascript

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

4 Comments

So there is not any other way for this about Means i have one php variable now i want php variable in javascipt value there
yes you can use php variable value in JavaScript but you cannot assign javascript variable value to a php variable
you can do that.. check out my answer
@MateiMihai It can be done scanning the whole page, he can't do it simply else
8

You cannot do that. An alternative may be creating a cookie using javascript and then read the cookie in PHP.

In Javascript:

<script type="text/javascript">
    document.cookie = "cookieName=cookieValue";
</script>

And in PHP

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

   echo $phpVar;
?>

But an important point, for this to run the browser needs reload.

Best regards...

1 Comment

Surely this has really worked for me, I knew I could use cookies as an alternative BUT was worried of adding plugins. And this did not require that.
1

You can do it using ob_start() function (documentation). If you use this method you can call a function and parse the html content. You search for your variable and get it with php..

<html>
  <head>
  <?php
    $my_var = '';


    function callback($buffer)
    {
      global $my_var;
      $arr = array();

      preg_match('/var_example = \"(?P<var>\w+)\"*/', $arr, $buffer);

      $my_var = $arr['var'][0];  

      return $buffer;
    }

    ob_start("callback");

  ?>
  <script type="text/javascript">

    var var_example = "2222";

  </script>
  <?php

    ob_end_flush();

  ?>
  </head>
  <body>
  </body>
</html>

Comments

-2

Please try this:

Use document.write function in javascript.

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

4 Comments

Parse error: syntax error, unexpected T_ECHO
@Chirag but obvious you'll get an error...he is using echo in the variable value
Getting when i'm do echo before script this error come in :----- Parse error: syntax error, unexpected T_ECHO
for this example you need to remove echo statement $var_example_php = "<script>document.write(var_example);</script>";

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.