-5

Possible Duplicate:
How to get javascript variable value in php

I am looking for help to set value of a php variable from javascript variable.

I tried these but nothing worked:

Try 1:

<script>
<?php $phpVar = ?> jVar;
</script>

Try2:

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

Failed too..

Please help. Please help!

Community
  • 1
  • 1
The Coder
  • 15
  • 1
  • 6
  • 2
    I think you forgot that php run on serverside and javascript on client side. so if you explain problem more briefly some solution can be suggested. – CoreCoder Mar 21 '12 at 07:53
  • I've a page which shows a grid of tiles. I have to reset the width of each tile according to browser's resolution. Depending upon resolution, tiles comes upto 2, 3, 4, 5... per row. I've max width set for each tile too. For this I am using javascript to calculate columns. In one file, I am using PHP to render html for these tiles. Now, don't have count of maximum number of tiles in one row. here i need PHPVar=JVar Now the problem is that, with this dynamic calculation I also have to calculate height of each tile and equalize height of each column in a row so that it fits best and looks good. – The Coder Mar 21 '12 at 10:42

1 Answers1

0

Actually php is server side scripting language (that runs at server) and javascript is basically client side programming language (which runs in the browser) so you can't set a php variable from javascript. Look at these tutorials php and javascript.

But using ajax (javascript) you can pass javascript variables to php and can use them in the to the php script. Here is ajax tutorial link.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • Thanks for Replying Sheikh. I agree with you. The site i am working on is single page site and it never refreshes. Everything is worked out in ajax. On page load I am checking browser properties like width etc. At that point I want to initialize a global PHP variable which can be used throughout the app during the session. – The Coder Mar 21 '12 at 08:02