How to call a javascript function from a php echo

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chanshaw
    New Member
    • Nov 2008
    • 67

    How to call a javascript function from a php echo

    Hey I'm trying to echo out an onclick event that calls a javascript function

    Code:
    echo "
    <tr>
    <td>
    <input title='Add New Link' type='image' height='24' width='24' src='add.png' name='add_link' onClick=\"alertme()\">
    </td>
    <td>
    <p>Add Link</p>
    </td>
    </tr>";
    The Javascript code is from a different file

    Code:
    function alertme()
    {
    	$.ajax({
    	  url: "links_component.php?ACT='ADDLINK'",
    	  success: function(data) {
    	       $('#add_link').html(data);
    	  }
                    });
    };
    Now when the javascript is created through the echo output of the php the function doesn't seem to be found any ideas?
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Have you done your <javascript> HTML tags?

    Comment

    • chanshaw
      New Member
      • Nov 2008
      • 67

      #3
      Forgive me if im wrong but I shouldn't have to call the javascript from the inner html since I have

      Code:
      <script src="links_component.js"type="text/javascript"></script>
      in the header of the html, I should just be able to call the function correct?

      Comment

      • chanshaw
        New Member
        • Nov 2008
        • 67

        #4
        Originally posted by code green
        Have you done your <javascript> HTML tags?
        I apologise let me try to make my thought a little clearer. When the php is called and executes the echo that outputs the <input> tag that contains the javascript. If I remove the <input> with the javascript from the php and put it into the html it works correctly, but it doesnt seem to want to work when the php creates the <input>. It's like the php echo <input> does not recognize that the alertme() function is included in the javascript.

        Comment

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

          #5
          Forgive me if im wrong but I shouldn't have to call the javascript from the inner html since I have
          Thats fine. I just wanted to make sure this was present as it was not in your code.

          First point is I prefer wrapping HTML in single quotes and using double quotes internally, then escapes are unnecessary.
          The only possible problem is no semi-colon to close on click, but not too sure it is necessary
          Code:
          echo  
          '<tr> 
          <td> 
          <input title="Add New Link" type="image" 
          height="24" width="24" 
          src="add.png" name="add_link" 
          onClick="alertme();"> 
          </td> 
          <td> 
          <p>Add Link</p> 
          </td> 
          </tr>';

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            large blocks of HTML can also be defined using the Heredoc or Nowdoc syntax.

            Comment

            • Baul
              New Member
              • May 2010
              • 3

              #7
              Re: Calling a javascript function from a php echo

              Hi,

              I have tried a your code and added the alertme function:
              Code:
              <head>
              <script type="text/javascript">
                  alertme = function(){
                      alert("me");
                  }
              </script>
              </head>
              <?php
              echo
               '<tr>
               <td>
               <input title="Add New Link" type="image"
               height="24" width="24"
               src="add.png" name="add_link"
               onclick="alertme();">
               </td>
               <td>
               <p>Add Link</p>
               </td>
               </tr>';
              ?>
              and it works fine for me.
              I changed the call of the function from onClick to onclick because it is specified as onclick but it did work with onClick as well. I tested it in Firefox so perhaps you tested it in IE an there is a problem with onClick?

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                you don’t even need PHP to output static HTML.

                Comment

                • Baul
                  New Member
                  • May 2010
                  • 3

                  #9
                  That's right, but since the op's headline is "Calling a javascript function from a php echo" I think he wants to do something more elaborated then just calling this little function.

                  By the way, I don't think, that the php is the problem here.

                  Comment

                  Working...