Please HELP Me!!!

I want to directly pass the value of a javascript variable into php variable... I used this code below but it doesn't work.. Can anyone teach me how to do it?? PLEASE.. Thank you so much in advance..

Here's my code:

<script type="text/javascript">
  var a = 'This is my value'  
   <?php $get_value_of_a= ?> a;
</script>

What will be the best solution in order to pass the value of 'a' into '$get_value_of_a' ?? PLEASE HELP Me.. Thank you...

Recommended Answers

All 4 Replies

If I remember correctly, the most common way is to link to the appropriate php module with the javascript variable as a parm. This is an example:

echo "<script language='JavaScript'>
			var w = screen.width;
			var h = screen.height;
  //alert('You are using ' + w + 'x' + h + ' resolution...!');
  document.location.href='default.php?user=$userid&p=$p&w='+w+'&h='+h;
  </script>
  ";

Thank's for the response chrishea.. I appreciate it...
but what if I want to pass the value of javascript variable into php variable within the same page only. Is there a way to do that?

Member Avatar for diafol

You need to use ajax to pass a js value -> php -> update DB etc -> optional response data to js -> change page data. That does not force a redirect. However, chris' solution may suit you better.

Another recent development has been W3C 'media queries'.
Have a look here if you want to develop responsive design sites (for diff. screen resolutions or mobiles/tablets etc):
http://www.alistapart.com/articles/responsive-web-design/

I agree with ardav. PHP is processed by your server before the page is ever sent to the user. Javascript on the other hand is processed by the visitor's computer. Because of the nature of these languages PHP will ALWAYS execute before JAVA. THerefore java can never pass a variable back into php because the php is long done before the java starts working.

That is not to say that java can not send variables back to the server to be stored or otherwise used but they need to be sent back. The most common way to send vars back is a page reload. When this is not ideal you can use AJAX to send post or get vars back to the server. Ajax does however require another page to process what is being sent back to the server.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.