#1
  1. No Profile Picture
    rubbish in javascript
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2001
    Location
    London
    Posts
    706
    Rep Power
    14

    Question javascript in php?


    hello

    can anyone tell me why this works...

    <?

    $hello = "<script language=\"JavaScript1.2\">document.write(\"hello\");</script>";
    echo $hello;

    ?>

    it echoes out "hello"
    but this does'nt....

    <?

    $hello = "<script language=\"JavaScript1.2\">document.write(\"hello\");</script>";

    //create image
    $image = imagecreate(468, 60);

    //colors
    $grey = ImageColorAllocate($image,200,200,200);
    $black = ImageColorAllocate($image,0,0,0);

    imagestring($image, 1, 20, 32, $hello, $black);

    //visualise image
    header("content-type: image/jpeg");
    imagejpeg($image, '', 100);
    imagedestroy($image);

    ?>

    it echoes out "<script language=\"JavaScript1.2\">document.write(\"hello\");</script>"

    i thought it might have had something to do with the headers but i deleted those to test and it still does the same??

    thanks in advance
    gar
  2. #2
  3. Second highest poster :p
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2001
    Posts
    7,322
    Rep Power
    33
    Javascript is a client side scripting language, therefore it is ran by the browser.

    With the first example the Javascript is passed to the browser which executes it and will print out hello.

    In the second the browser looks through the HTML and all it sees is an image, it cannot see the script in the image to execute it.
    - Andreas Koepke

    Koepke Photography

  4. #3
  5. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    St Louis, MO
    Posts
    20
    Rep Power
    0
    Are you trying to get JavaScript to write "Hello" on the Image generated by PHP?

    JavaScript can't do this, but PHP can with the GD libraries.

    Try:

    PHP Code:
    $text "hello";
    ImageTTFText ($imageSizeAnglexycolor"/path/arial.ttf""$text"); 
    example:
    PHP Code:
    $text "hello";
    $white ImageColorAllocate($image,255,255,255);
    ImageTTFText ($image1801010$white"tahoma.ttf""$text"); 
    This Writes HELLO on the IMage defined by $image at 10 down from the top and 10 from the left with a angle of 0 in the Tahoma font. This assumes that tahoma.ttf is in the same directory as the PHP script.

    But if you want to use ImageString, then you must use it like this:
    PHP Code:
    $hello "hello";
    imagestring($image12032$hello$black); 
    There is no need for JavaScript to echo the data onto the page at all, since the string is being created on the image with PHP.

    Again, your problem is due to that fact that PHP is executed on the server when the page is requested. The javascript is executed in the user's browsers, so the string $hello is what PHP is rendering it onto the image as, since the javascript hasn't been executed yet.
    Last edited by Clark; May 19th, 2002 at 09:43 PM.
  6. #4
  7. No Profile Picture
    rubbish in javascript
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2001
    Location
    London
    Posts
    706
    Rep Power
    14
    >Are you trying to get JavaScript to write "Hello" on the Image generated by >php?

    not at all, i'm trying to capture info in javascript like what system and browser the person is using and then put that into the php image using imagestring(), my demo just used "hello" to simplify it for the means of posting here, my problem was of course i briefly forgot because the image php script is being called as an image ie "img scr=imagescript.php" and that the browser recognises it only as an image of course, anyway i've solved the problem by putting the javascript in the page with the image tag and generating the image tag with a document.write('') which write in the variables to be passed to php,

    javascript's means of passing variables in the url seems quite annoying and cumbersome, but i guess its not designed to really do this!!

    a+
    gar
  8. #5
  9. Senior Member
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2000
    Location
    Enschede, The Netherlands
    Posts
    1,527
    Rep Power
    16
    why use javascript at all

    the client will request the page from the server, and you can check the OS/browser through some default variables like $_SERVER["HTTP_USER_AGENT"]. That would give 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)' for example which you can then pass to GD.. another nice feature (I think) is checking a users DNS-name through his IP, check the country he's from, and create an image in his own language

    If you name the php-script image.jpg instead of image.php and put a .htaccess with the folllowing lines the client won't even notice the image is a script unless you show his IP in a very large font right in front of him
    Code:
    AddType application/x-httpd-php .gif
    AddType application/x-httpd-php .jpg
    Of course there shouldn't be any 'real' images in the same directory as well
  10. #6
  11. No Profile Picture
    rubbish in javascript
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2001
    Location
    London
    Posts
    706
    Rep Power
    14
    >the client will request the page from the server, and you can check the >OS/browser through some default variables like >$_SERVER["HTTP_USER_AGENT"].

    thats a good point i forgot about the server variables!! but still there are a few things about the user you can detect which php (as far as i know) can't, namely...

    1 - users screen resolution
    2 - list of plugins
    3 - font anti-aliasing
    4 - java
    5 - javascript


    unfortunity for this i won't have access to the .htaccess file as its to be placed on another server than my own, however ....

    AddType application/x-httpd-php .gif

    thats interesting i did'nt know how to do that! is it possible to also do...

    AddType application/x-httpd-php .html

    so that php renders on .html pages?? i think i'm not very aware of what the possibilites of the .htaccess file are do you know where i can find a tutorial or something that goes over all these???

    thanks
    gar
  12. #7
  13. Senior Member
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2000
    Location
    Enschede, The Netherlands
    Posts
    1,527
    Rep Power
    16
    Indeed there are some things PHP can't detect, so if you want those extra things you might wanna stay with javascript

    unfortunity for this i won't have access to the .htaccess file as its to be placed on another server than my own, however ....
    Hmm, I run my own server so I wouldn't know about the ability to put htaccess files there but it seems to me if you can put a php file on the server you should also be able to put a htaccess file there

    thats interesting i did'nt know how to do that! is it possible to also do...
    AddType application/x-httpd-php .html
    so that php renders on .html pages??
    yup, but remember that ALL html pages will be parsed, also html pages with no php-code in them, making the response time (a bit slower... dunno exact times though).. but if all your pages are php you could rename them to html and put the abouve AddType in that directory so visitors won't know they're looking at generated pages

    i think i'm not very aware of what the possibilites of the .htaccess file are do you know where i can find a tutorial or something that goes over all these???
    http://www.javascriptkit.com/howto/htaccess.shtml
    a search on 'htaccess' with google gives many other pages as well
  14. #8
  15. No Profile Picture
    rubbish in javascript
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2001
    Location
    London
    Posts
    706
    Rep Power
    14
    >Indeed there are some things PHP can't detect, so if you want those extra >things you might wanna stay with javascript

    for what i'm doing i only need the system and browser so i'm going to take your suggestion on board for this particular example but its good to know the javascript way now as well if i ever did want to use those other extra things.


    >Hmm, I run my own server so I wouldn't know about the ability to put >htaccess files there but it seems to me if you can put a php file on the server
    >you should also be able to put a htaccess file there

    yes correct, but i explained it a bit badly, what i mean is this is a page or for example a banner ad! thats going on a friends server and while i know he has php and i can do that with the image i don't want to impose on his .htaccess file which might already be set up for things.


    >yup, but remember that ALL html pages will be parsed

    mm great, that was for something else i was thinking about which i now know is possible, namely i've a friend who runs a server with manilla type blog sites and manilla renders .html files but he has php installed and it would be good to be able to input php via manillas forms to perform certain things on your personal page.


    >http://www.javascriptkit.com/howto/htaccess.shtml
    >a search on 'htaccess' with google gives many other pages as well

    thanks for that!!! will have a look in a bit!

    thanks
    a+
    gar
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    Ohio
    Posts
    1
    Rep Power
    0

    Lightbulb


    If you are inserting the javascript in an image, then the browser will not process the script. However, when you simply echo the javascript to the browser, it receives the script as text rather than an image, so it processes the code.

    I hope this helps.

IMN logo majestic logo threadwatch logo seochat tools logo