Will Javascript work inside my *.php file?

Should I name all of my scripts *.html in order for both PHP and JS to work together.

I could not find the answer in the two books I've purchased not on the internet and I'm having trouble trying to get a JS onclick event to fire.

    you can echo out javascript just like html inside a php file. Just make sure its in the right place on the html DOM. You can also stop php and type in html/js normally like:

    // stop php
    ?>
    <script type="text/javascript" ....>
    function bla()
    {
    alert('yipyy skipyyy');
    }
    </script>
    <?php //start php

      I have this PHP code working, but I've been unsuccessful in getting an 'onclick' event to work.

      echo '<div style="position:'.$row[12].'; left:'.$row[8].'px; top:'.$row[9].'px;"><img src="'.$row[2].'" onmouseover="this.src=\''.$row[3].'\';" onmouseout="this.src=\''.$row[2].'\';" ></a></div>';

      I am using a custom image file as a button and it is working properly for 'onmouseover' and 'onmouseout' events.

        It was before the </a> but it doesn't work so I deleted it.

        I do not know how to write -> onclick="this.src\' '.$row[6].'\';"

          correction: onclick="this.src=\' '.$row[6].'\';"

          $row[6] contains a URL to a different *.php script, another <div> layer in the main.php script, or a PHP Function located in a include("main_funcs.php") that I want to show.

            it should be the same as the other ones that are working. put it on the image tag with the rest of them. Also you might not see anything because the second you click it goes to the new page so the javascript might not be able to load the new file fast enough for you to notice before the current page loses focus to the new page.

              I've already tried onclick="this.src=\' '.$row[6].'\';" inside the <img> tag...it isn't working.

              $row[6] is holding http://www.google.com ... may be it needs quotes or the <a href="http://www.google.com"></a>

                If I add the highlighted I can go to a different web page...

                echo '<div style="position:'.$row[12].'; left:'.$row[8].'px; top:'.$row[9].'px;"><a href="http://www.google.com"><img src="'.$row[2].'" onmouseover="this.src=\''.$row[3].'\';" onmouseout="this.src=\''.$row[2].'\';" border="0"></a></div>';

                but I want the ability to call a PHP function that shows a new <div> layer

                  If you want to use javascript to call php to display new content try looking into ajax functions. Or if the content is predetermined you could build it out into javascript during the page load and have the onclick "activate it" and whatnot to show it. If you are just showing new content when clicked, why are you trying to load a new page? It should either send them to a new page or show new content, not both.

                    I've built my menu system in MySQL and I would like the ability to:

                    1. Show new content on an existing page that is hidden by the <div> tag
                    2. Launch a new window passing variables to the new script
                    3. Use a PHP function

                    I'm just having trouble with 'onclick' because I wasn't able to figure out how to use it.

                    I guess I could use the href="#" method to make a <div> layer visible and/or execute a PHP function.

                      You may want to take a look at jquery also, it might help you separate php and javascript and may be easier for you to do whatever you need to do.

                        I got JS and PHP to play together...

                        <?php
                        echo [COLOR="Red"]'[/COLOR]<script language="JavaScript"> //begin JavaScript Code
                        [COLOR="Blue"]function testingjs()
                        {   
                        alert("JS function inside php");[/COLOR] [COLOR="red"]'[/COLOR]; //end JavaScript Code // here is where you insert PHP code
                        include("MySQL_menu.php"); $m_var1="HomeMenu";
                        $b_var1="Button01";
                        $x_path="..\MenuSystem\"; echo '} </script>'; //close the function ?> //end PHP Code
                          Write a Reply...