Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Possible Duplicate:
How to pass JavaScript variables to PHP?

How to assign on this php variable asign 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 
?>
share|improve this question
4  
There has been 100 of questions exactly dealing with same question on SO – Prashant Singh Oct 27 '12 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 – Mr. Alien Oct 27 '12 at 5:47
you can do it with ob_start() function.. check out my answer – Matei Mihai Oct 27 '12 at 6:27

marked as duplicate by Vulcan, Nikola K., mjv, Smi, DCoder Oct 27 '12 at 7:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4 Answers

up vote 4 down vote accepted

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

share|improve this answer
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 – Chirag Oct 27 '12 at 5:50
yes you can use php variable value in JavaScript but you cannot assign javascript variable value to a php variable – Mr. Alien Oct 27 '12 at 5:52
you can do that.. check out my answer – Matei Mihai Oct 27 '12 at 6:24
-1 for what?... – Mr. Alien Oct 27 '12 at 6:29
@MateiMihai It can be done scanning the whole page, he can't do it simply else – Mr. Alien Oct 27 '12 at 6:30

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...

share|improve this answer
1  
@Chirag this is an alternative. – evolquez Oct 27 '12 at 6:23
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. – mukamaivan Jan 15 at 22:01

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>
share|improve this answer

Please try this:

Use document.write function in javascript.

<?php
     $var_example_php = "<script>document.write(var_example);</script>";
     echo $var_example_php;
?>
share|improve this answer
Dear i'm try this it's not woking you have any other way – Chirag Oct 27 '12 at 5:51
try my update... – suresh.g Oct 27 '12 at 5:52
you cannot use echo there – Mr. Alien Oct 27 '12 at 5:56
how can i seen your update ? – Chirag Oct 27 '12 at 5:58
Parse error: syntax error, unexpected T_ECHO – Chirag Oct 27 '12 at 6:00
show 4 more comments

Not the answer you're looking for? Browse other questions tagged or ask your own question.