Confused on how to merge javascript with php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • orfiyus
    New Member
    • Jul 2007
    • 36

    Confused on how to merge javascript with php

    Wutup

    Im trying to write a php script that has a link. When the user clicks the link the link turns into a text field which the user can modify. When the user clicks elsewhere the text that the user typed is turned back into a link. Ive got the php script which Im supposed to change and I got a piece of javascript code that does the whole link changing of link to textfield and back again thing.

    Problem Im having is I dont understand how to combine the two together. I tried to echo the javascript tags similar to how html tags can be echoed but that didnt work. I googled and found examples similar to this:

    Code:
    <?php
    
    echo("This is php");
    ?>
    
    <script....
    //javascript stuff goes here
    </script>

    Is there any way I can put the javascript within the php part of the code?

    Thanks
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Javascript calls belongs within the HTML.
    Ideally close the php tags then write the HTML.
    [PHP]<?php
    echo("This is php");
    ?>
    <script...etc >
    //javascript stuff goes here
    </script>[/PHP]
    If you must place within php then echo out the HTML
    [PHP]<?php
    echo("This is php");
    echo '<script>
    //javascript stuff goes here
    </script...etc>';
    ?>[/PHP] But I suspect this is not what you are asking

    Comment

    • orfiyus
      New Member
      • Jul 2007
      • 36

      #3
      Actually I tried just echoing out the javascript. I have a bunch of javascript functions and html lines that call these functions. When I try just echoing it out I get a blank page.

      I shouldve just posted the code Im working with right off the bat here it is its pretty short. Can you take a look maybe theres an error I dont see ?

      Thanks

      Code:
      <?php
      
      echo("<html><body>");
      echo("<TITLE>COMBO2.PHP</TITLE>");
      echo("<BR><B><U>THIS Is the test to combine php javascript and html<br></U>");
      echo("<BR><BR>");
      
      
      echo("<script type="text/javascript"><!--\n");
      echo("function editLink() { ");
      echo("var myLink = document.getElementById('my_link'); ");
      echo("var myInput = document.getElementById('my_input'); ");
      
      echo("myInput.value = myLink.innerHTML; ");
      echo("myLink.style.display= 'none'; ");
      echo("myInput.style.display = 'inline'; ");
      echo("} ");
      
      echo("function doneEdit() { ");
      echo("var myLink = document.getElementById('my_link'); ");
      echo("var myInput = document.getElementById('my_input'); ");
      
      echo("myLink.innerHTML = myInput.value; ");
      echo("myLink.style.display = 'inline'; ");
      echo("myInput.style.display = 'none'; ");
      echo("} ");
      
      echo("//--></script>");
      
      echo( "<a id="my_link" href="javascript:editLink()">Click Me</a> ");
      echo( " <input type="text"  style="display: none;" ");
      echo( "id="my_input" ");
      echo( "onblur="doneEdit()"/> ");
      echo("<br>");
      echo(" </body></html>");
      ?>
      Last edited by orfiyus; Nov 30 '07, 02:52 PM. Reason: Fixed up a sentence

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        Well for a start echo is not a function so you don't need brackets.
        Nor in this situation do I see a need for echo, just place the code ouside the php tags.
        i am a little confused with why you are using php.
        Anyway, I don't see a <head> tag even though you are using <title>.
        Javascript is touchy about things like that

        Comment

        • orfiyus
          New Member
          • Jul 2007
          • 36

          #5
          Im trying to do it this way because Ive got a huge php script to modify in the same way and I dont know if I ll be able to seperate the it into 2 php blocks. and the <head> tag didnt do anything when I added it in.

          I guess I ll try it on the huge one and see wut happens cant hurt now that I think of it.

          Worst case scenario I ll have to convert these javascript functions to php functions (hopefully it ll be possible).

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            I take it you are aware of Javascript being client-side and php being server-side and the implications of mixing the two?

            Comment

            • orfiyus
              New Member
              • Jul 2007
              • 36

              #7
              Originally posted by code green
              I take it you are aware of Javascript being client-side and php being server-side and the implications of mixing the two?
              yea I guess thats gonna be a problem. Wutever I ll just seperate the huge script and see if that works. If not I ll just rewrite the javascript into php.

              Comment

              Working...