#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Australia
    Posts
    52
    Rep Power
    12

    Call a JavaScript function


    How can I call up the javascript function from PHP/hmtl file?

    <script>
    function get_color(event)
    {
    alert(event.x, event.y);
    }

    </script>
    <?php

    print("<A HREF='javascript:get_color(event)'><IMG SRC='color-square.gif' BORDER='0' ALT='Color Panel' ISMAP></A>");

    ?>

    I want to click the image, then it calls the javascript function to return the pointer location where the click is...can i?
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2002
    Location
    Queensland, Australia
    Posts
    827
    Rep Power
    141
    There are a few posts in this forum in regards to this.

    I think you'll find you simply can't use JS and PHP like you are trying to. You can't get a value using JS then have PHP use it in the same page. This is because PHP is a server-side language and JS is client-side. You can however pass the variable obtained using JS using POST or GET to a new page or back to itself and use PHP's $_POST or $_GET to grab the values.

    This is my understanding, but I'm no expert.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Posts
    790
    Rep Power
    15
    Here's a short example of using this type of function in an HTML file. The same thing applies if you're printing the HTML out from a PHP script.

    Also, IE and Netscape handle the event object differently. So, you would need to do browser detection and output browser specific functions.

    The example below works in IE.
    Code:
    <html>
    <head>
    <title>Call	JavaScript	Function</title>
    <SCRIPT LANGUAGE="JavaScript">
    	function get_color()
    	{
    		X = event.x;
    		Y = event.y;
    		strAlert = "You clicked the mouse at X = " + X + " Y = "	+ Y	;
    		alert(strAlert);
    		}
    </SCRIPT>
    </head>
    <body>
    <a onClick='get_color();'>
    <IMG name ='mypic' SRC='logohead.gif' BORDER='0' ALT='Color Panel'>
    </a>
    </html>
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    16
    Rep Power
    0
    You should be able to get it to work in Netscape as well. Using the prev example:
    <html>
    <head>
    <title>Call JavaScript Function</title>
    <SCRIPT LANGUAGE="JavaScript">
    function get_color()
    {
    X = event.x;
    Y = event.y;
    strAlert = "You clicked the mouse at X = " + X + " Y = " + Y ;
    alert(strAlert);
    }
    </SCRIPT>
    </head>
    <body>
    <a href="javascript:get_color();">
    <IMG name ='mypic' SRC='logohead.gif' BORDER='0' ALT='Color Panel'>
    </a>
    </html>

    Good luck!

IMN logo majestic logo threadwatch logo seochat tools logo