Connecting Tech Pros Worldwide Help | Site Map

To pass a PHP variable to javascript

 
LinkBack Thread Tools Search this Thread
  #1  
Old January 29th, 2006, 12:55 PM
Pablito
Guest
 
Posts: n/a
Default To pass a PHP variable to javascript

Hi at all,
can I pass a $var maked with PHP to javascript
How?
Thanks
Pablito



  #2  
Old January 29th, 2006, 01:05 PM
jamen
Guest
 
Posts: n/a
Default 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, 01:05 PM
EmC
Guest
 
Posts: n/a
Default 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, 02:45 PM
Pablito
Guest
 
Posts: n/a
Default 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, 03:25 PM
d
Guest
 
Posts: n/a
Default 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, 06:15 PM
Alvaro G. Vicario
Guest
 
Posts: n/a
Default 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, 01:15 PM
Eric Anderson
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.