Jump to content


Photo

javascript variable to php variable


  • Please log in to reply
3 replies to this topic

#1 baho

baho
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 04 August 2006 - 05:07 PM

How can i pass the javascript variable 'i' value to php variable '$k'?

<html><body>
<script language="javascript">
var i=5;
<?php $k;?>
</script>
</body></html>

Thanx.

#2 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 04 August 2006 - 06:13 PM

<html><body>
<script language="javascript">
var $i=5;
<?php $k = $i;?>
</script>
</body></html>
when you create the javascript variable, make it readable as a variable by PHP by using the PHP standard method of giving it a $ at the beginning.
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#3 baho

baho
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 04 August 2006 - 07:58 PM

thanx, but this code doesn't work

<script language=javascript>
  $k=5;
  alert ($k);
  alert ("<?php echo $k?>");
  <?php $i=$k;?>
  alert ("<?php echo $i?>");
</script>


Can you tell me what is the problem here?

#4 stillatmycomputer

stillatmycomputer
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 05 August 2006 - 03:42 AM

PHP code is processed by the server before the browser processes your javascript.  Set your variables first in PHP.

<script language="javascript">
var <?php $k=5; echo('$k='.$k); ?>;
alert($k);
alert("<?php echo($k); ?>");
<?php $i=$k; ?>
alert("<?php echo $i ?>");
</script>




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users