Connecting Tech Pros Worldwide Help | Site Map

To pass a PHP variable to javascript

  #1  
Old January 29th, 2006, 01:55 PM
Pablito
Guest
 
Posts: n/a
Hi at all,
can I pass a $var maked with PHP to javascript
How?
Thanks
Pablito


  #2  
Old January 29th, 2006, 02:05 PM
jamen
Guest
 
Posts: n/a

re: To pass a PHP variable to javascript


Pablito wrote:[color=blue]
> can I pass a $var maked with PHP to javascript[/color]

of course..

<script type="text/javascript">

var jsvar = <?php echo $phpvar ?>;

</script>
  #3  
Old January 29th, 2006, 02:05 PM
EmC
Guest
 
Posts: n/a

re: To pass a PHP variable to javascript


Pablito ha scritto:[color=blue]
> Hi at all,
> can I pass a $var maked with PHP to javascript
> How?
> Thanks
> Pablito
>
>[/color]
Hi Pablito,
that's a simple example:
<?php
$a="Hallo";
echo "<script type=\"text/javascript\">
alert ('$a')
</script>";
?>

Bye
Enrico

  #4  
Old January 29th, 2006, 03:45 PM
Pablito
Guest
 
Posts: n/a

re: To pass a PHP variable to javascript



"jamen" <jamen@invalid> ha scritto nel messaggio
news:43dcc9ca$0$2086$edfadb0f@dtext02.news.tele.dk ...[color=blue]
> Pablito wrote:[color=green]
> > can I pass a $var maked with PHP to javascript[/color]
>
> of course..
>
> <script type="text/javascript">
>
> var jsvar = <?php echo $phpvar ?>;
>
> </script>[/color]

I thank you very much but my script is this and it is like your but do not
work
Why?


<?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>";
?>

Pablito


  #5  
Old January 29th, 2006, 04:25 PM
d
Guest
 
Posts: n/a

re: To pass a PHP variable to javascript


"Pablito" <pablito@nonspam.com> wrote in message
news:EZ4Df.68327$eD5.1162011@twister2.libero.it...[color=blue]
>
> "jamen" <jamen@invalid> ha scritto nel messaggio
> news:43dcc9ca$0$2086$edfadb0f@dtext02.news.tele.dk ...[color=green]
>> Pablito wrote:[color=darkred]
>> > can I pass a $var maked with PHP to javascript[/color]
>>
>> of course..
>>
>> <script type="text/javascript">
>>
>> var jsvar = <?php echo $phpvar ?>;
>>
>> </script>[/color]
>
> I thank you very much but my script is this and it is like your but do
> not
> work
> Why?
>
>
> <?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>";
> ?>[/color]

You need to escape the quotes in your <script> tag:

echo "<script type=\"text/javascript\">
var incr=$incr;
</script>";

(and note the semi-colon after the var line). You might also want to put
'language=javascript' in the script tag, too, just to be on the safe side ;)

How are you using that javascript variable?
[color=blue]
> Pablito[/color]

dave


  #6  
Old January 29th, 2006, 07:15 PM
Alvaro G. Vicario
Guest
 
Posts: n/a

re: To pass a PHP variable to javascript


*** Pablito escribió/wrote (Sun, 29 Jan 2006 15:14:44 GMT):[color=blue]
> <?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>";
> ?>[/color]

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.

--
-+ Álvaro G. Vicario - Burgos, Spain
++ http://bits.demogracia.com es mi sitio para programadores web
+- http://www.demogracia.com es mi web de humor libre de cloro
--
  #7  
Old January 30th, 2006, 02:15 PM
Eric Anderson
Guest
 
Posts: n/a

re: To pass a PHP variable to javascript


Pablito wrote:[color=blue]
> Hi at all,
> can I pass a $var maked with PHP to javascript
> How?[/color]

If you want to pass complex datastructures from PHP to javascript take a
look at JSON (search google).

Eric
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing php date variable to javascript function gubbachchi answers 4 February 25th, 2008 05:32 AM
How parse XML variable to Javascript Gretsch answers 4 October 3rd, 2007 01:26 AM
Is it Possible To Transfer Data From Php Variable To Javascript Variable samiul answers 1 September 7th, 2007 08:07 AM
How to Pass PHP variables to JS? Don answers 6 July 17th, 2005 10:31 AM