You are here: Re: To pass a PHP variable to javascript « PHP Programming Language « IT news, forums, messages
Re: To pass a PHP variable to javascript

Posted by Rich on 01/30/06 21:03

In article <1jgbvulmfnf71$.1lol61gwsbjxe$.dlg@40tude.net>, Alvaro G. Vicario
says...
>
>*** Pablito escribiσ/wrote (Sun, 29 Jan 2006 15:14:44 GMT):
>> <?php
>> $cont=fopen('cont.txt','r');
>> $incr=fgets($cont);
>> //echo $incr;
>> $incr++;
>> fclose($cont);
>> $cont=fopen('cont.txt','w');
>> fwrite($cont,$incr);
>> fclose($cont);
>> echo "<script type="text/javascript">
>> var incr=$incr
>> </script>";
>> ?>
>
>This code doesn't make much sense. You must understand that PHP and
>JavaScript are _not_ executed at the same time. PHP is parsed in the server
>and it's used to create the JavaScript code. So, when you get JavaScript
>errors, check first the generated code.
>
>Try this:
>
><?php
>$cont=fopen('cont.txt','r');
>$incr=fgets($cont);
>//echo $incr;
>$incr++;
>fclose($cont);
>$cont=fopen('cont.txt','w');
>fwrite($cont,$incr);
>fclose($cont);
>?>
><script type="text/javascript"><!--
>var incr=<?=$incr?>;
>//--></script>
>
>Also, your code should raise a PHP parse error. I guess your PHP install is
>configured to hide errors. Edit php.ini and set accordingly the
>error_reporting value.
>

As you said the PHP is parsed by the server, so by the time the web browser gets
the document, it only sees the HTML and Javascript that were already present or
generated by the PHP.

Looks like Pablito was pretty close, but maybe a few syntax errors in the echo
statement. I prefer using the "<?=$var?>" method of printing in PHP variables so
I can put in less time trouble shooting syntax errors with echo. : )

Rich
--
Newsguy -- http://newsguy.com

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация