PHP variables to JS

Javascript coding ..

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 2
Joined: Mon Jul 04, 2005 1:17 pm

PHP variables to JS

Postby Kar » Mon Jul 04, 2005 1:57 pm


I'm trying since a lot of time, passing simple variables from php to JS.
I can find very often the following code.

Code: Select all


   $size = GetImageSize("");
 <script language="JavaScript">
   var pictureWidth = <?= $pictureWidth ?>;   // or
   var pictureWidth = <?PHP echo $pictureWidth; ?>;  // or
   var pictureWidth ="<?PHP echo $pictureWidth; ?>";



but it always shows an error in Javascript and a blanc window.
I tried this on two different php-servers. But I can't get it work.
What is the detail that I forgot ?
Or is it better with a $_GET or $_POST and hidden forms?
If so, does somebody know a simple working example to learn how to do.
I was hardly trying since three weeks to find something of these possibilitys.
No complete basic examples to find !!

Thanks in advance to all of you for your experience !!


User avatar
Posts: 1717
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden

Postby gesf » Tue Jul 05, 2005 4:52 am

Make sure you're really accessing the image file.

var pictureWidth = <?php echo $pictureWidth; ?>; whould be the better way to assign $pictureWidth to the javascript vaiable.
The Javascript error can also be related to the var pictureWidth... if no "size" was assign to it (First line).

Use error_reporting(E_ALL); so you can make sure about PHP's errors.

Also try to pass the JS code to the Body part of the HTML.

New php-forum User
New php-forum User
Posts: 2
Joined: Mon Jul 04, 2005 1:17 pm

Postby Kar » Tue Jul 05, 2005 12:48 pm


thanks for your explications.
Well, the thing is that I have to call this file blabla.php and not blabla.htm.
Yes it was the detail i was searching for. I'm sorry for this stupid thing,
but I thought that when I start with html code so it must be called ...htm.

I found that because I asked in the php line:

echo $pictureWidth; // and than

but nothing happened at all.

So, now I am happy and I want to thank you !!


Return to “JavaScript”

Who is online

Users browsing this forum: No registered users and 1 guest