PHP in JS?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dujmovicv
    New Member
    • Jul 2006
    • 10

    PHP in JS?

    Hi!
    I have some menu buttons on my page 'somepage.php' which I've made in JS:
    Code:
    <script language="JavaScript">
    	rollImage1 = new Image; rollImage1.src = "menu_b1_over.gif";
    	defaultImage1 = new Image; defaultImage1.src = "menu_b1.gif";
    	rollImage2 = new Image; rollImage2.src = "menu_b2_over.gif";
    	defaultImage2 = new Image; defaultImage2.src = "menu_b2.gif";
    	rollImage3 = new Image; rollImage3.src = "menu_b3_over.gif";
    	defaultImage3 = new Image; defaultImage3.src = "menu_b3.gif";
    	rollImage4 = new Image; rollImage4.src = "menu_b4_over.gif";
    	defaultImage4 = new Image; defaultImage4.src = "menu_b4.gif";
    </script>
    here goes some other elements of the page....
    Code:
    <a href="???????" target="data" onMouseOver="document.myImage1.src = rollImage1.src;"
    	onMouseOut="document.myImage1.src = defaultImage1.src;">
    	<img src="menu_b1.gif" name="myImage1" border=0></a>
    <a href="????????" target="data" onMouseOver="document.myImage2.src = rollImage2.src;"
    	onMouseOut="document.myImage2.src = defaultImage2.src;">
    	<img src="menu_b2.gif" name="myImage2" border=0></a>
    <a href="????????" target="data" onMouseOver="document.myImage3.src = rollImage3.src;"
    	onMouseOut="document.myImage3.src = defaultImage3.src;">
    	<img src="menu_b3.gif" name="myImage3" border=0></a>
    <a href="????????" target="data" onMouseOver="document.myImage4.src = rollImage4.src;"
    	onMouseOut="document.myImage4.src = defaultImage4.src;">
    	<img src="menu_b4.gif" name="myImage4" border=0></a>
    Is it possible to use PHP inside JS? JS can solve the menu (rolling over the images--> buttons), but I want to process the user's choice from the menu to PHP, so the script can load the appropriate content to the 'data' frame...
    Can I use php script at the ??????? inside JS to find out the user's choice from a select menu for example? Hope I was clear....
  • vssp
    Contributor
    • Jul 2006
    • 268

    #2
    yes ites possible to inside the java script using php.
    But not reterive any variable

    Comment

    • iam_clint
      Recognized Expert Top Contributor
      • Jul 2006
      • 1207

      #3
      actually you can retrieve a variable in a sense



      Code:
      function test() {
        var blah = "<?=blah?>";
      }

      Comment

      • vssp
        Contributor
        • Jul 2006
        • 268

        #4
        Hi friend

        Yes here is teh example

        <HEAD>
        <SCRIPT LANGUAGE="JavaS cript1.2">
        alert('Your score is <? echo "$variable" ?> . Thank you for playing.');
        </SCRIPT>
        </HEAD>
        <BODY>
        Main text here…
        </BODY>
        </HTML>

        Comment

        • vssp
          Contributor
          • Jul 2006
          • 268

          #5
          This is anoter example
          <?
          echo "<script language=\"Java Script\">\n";
          echo "alert("javascr ipt from php");\n";
          echo "</script>";
          ?>

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Turn your question around: "JS in PHP?"

            In my experience, the easiest way to use JavaScript is from within the PHP code. So i would recommend the solution given by VssP in the previous reply.

            Ronald :cool:

            Comment

            • luminary4him
              New Member
              • Jun 2006
              • 4

              #7
              Okay.. here's a question for you:

              I have been trying to get my Javascript to work on my php page and it doesn't seem to want to. I was beginning to think that maybe you couldn't do that. But obviously from what you guys are saying you should be able to.

              So here's my php in which I tried to insert some JS in the echo statement:

              Code:
              <?
              if ($page == 'index') 
              {
              echo "<img border=0 src='images/homenew.gif'>";
              }
              else
              {
              echo "<a href='http://www.healthy-connections.org/index.php' title='Healthy Connections Home'><img border=0 onmouseover='this.src=images/homebehind2.gif;' onmouseout='this.src=images/homebehind.gif;' src='images/homebehind.gif' style='margin-left: 0px;'></a>";
              }
              ?>
              Am I doing something wrong?

              Thanks,
              Elizabeth

              Comment

              • iam_clint
                Recognized Expert Top Contributor
                • Jul 2006
                • 1207

                #8
                i don't see any javascript in that besides the on mouse over out and click event... whats the page doing

                Comment

                • luminary4him
                  New Member
                  • Jun 2006
                  • 4

                  #9
                  Nothing and that's the problem. That is all the JS I'm trying to use. A super-simple onmouseover and onmouseout to swap out images on the navigational buttons. Shouldn't be a problem, right? The page is completely ignoring it like it is not even in there.

                  I find it obvious that it has something to do with the fact that it's in a .php file because I had this same html (w/ JS) in an .html file and it worked like I thought it should but when I copied and pasted it into my .php it did not.

                  And THAT, my friends, is why I am baffled. : )

                  Comment

                  • bevort
                    New Member
                    • Jul 2006
                    • 53

                    #10
                    Look at the result of your php file by viewing the source and copying it into a syntax sensetive editor. You will see probably some errors.

                    Also when you have the result in your browser you can see that there is an error and where to find it. IE is very bad in pointing out where the error is.

                    Om a mouseover() image swapping yoou have to load both images first before calling them in the swap script. Try a javascript site for some details on this.

                    It really is possible and I do note see any error in your lines of code

                    Comment

                    • dujmovicv
                      New Member
                      • Jul 2006
                      • 10

                      #11
                      Is it possible to use php code inside the JS tags?
                      Code:
                      <script src="<? =$some_php_variable?>.js" language="JavaScript1.2"></script>
                      ?????????

                      Comment

                      Working...