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

share|improve this question
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 7: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. – Kawal Babra Mar 21 '12 at 10:42
I hope that defines it :) – Kawal Babra Mar 21 '12 at 10:45

marked as duplicate by Quentin, KingCrunch, Your Common Sense, Shoban, stema Mar 22 '12 at 10:05

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.

1 Answer

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.

share|improve this answer
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. – Kawal Babra Mar 21 '12 at 8:02

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