Board index   FAQ   Search  
Register  Login
Board index php forum :: HTML JavaScript

PHP variables to JS

Javascript coding ..

Moderators: macek, egami, gesf

PHP variables to JS

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

Hallo,

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
<html>
<head>

 <?php
   $size = GetImageSize("http://www.fiftiesweb.com/elvis-portrait2sm.jpg");
   $pictureWidth=$size[0];
 ?>
 
 <script language="JavaScript">
   var pictureWidth = <?= $pictureWidth ?>;   // or
   var pictureWidth = <?PHP echo $pictureWidth; ?>;  // or
   var pictureWidth ="<?PHP echo $pictureWidth; ?>";
   document.write(pictureWidth);
   alert(pictureWidth);
 </script>

</head>
 <body>

 </body>
</html>



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 !!

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

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.
User avatar
gesf
Moderator
Moderator
 
Posts: 1717
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden

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

gesf,

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
error_reporting(E_ALL);

but nothing happened at all.

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

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


Return to JavaScript

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.