By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,453 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,453 IT Pros & Developers. It's quick & easy.

How to call a javascript function from a php echo

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

Expand|Select|Wrap|Line Numbers
  1. echo "
  2. <tr>
  3. <td>
  4. <input title='Add New Link' type='image' height='24' width='24' src='add.png' name='add_link' onClick=\"alertme()\">
  5. </td>
  6. <td>
  7. <p>Add Link</p>
  8. </td>
  9. </tr>";
  10.  
The Javascript code is from a different file

Expand|Select|Wrap|Line Numbers
  1. function alertme()
  2. {
  3.     $.ajax({
  4.       url: "links_component.php?ACT='ADDLINK'",
  5.       success: function(data) {
  6.            $('#add_link').html(data);
  7.       }
  8.                 });
  9. };
  10.  
Now when the javascript is created through the echo output of the php the function doesn't seem to be found any ideas?
May 26 '10 #1
Share this Question
Share on Google+
8 Replies


code green
Expert 100+
P: 1,726
Have you done your <javascript> HTML tags?
May 26 '10 #2

P: 67
Forgive me if im wrong but I shouldn't have to call the javascript from the inner html since I have

Expand|Select|Wrap|Line Numbers
  1. <script src="links_component.js"type="text/javascript"></script>
  2.  
in the header of the html, I should just be able to call the function correct?
May 26 '10 #3

P: 67
@code green
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.
May 26 '10 #4

code green
Expert 100+
P: 1,726
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
Expand|Select|Wrap|Line Numbers
  1. echo  
  2. '<tr> 
  3. <td> 
  4. <input title="Add New Link" type="image" 
  5. height="24" width="24" 
  6. src="add.png" name="add_link" 
  7. onClick="alertme();"> 
  8. </td> 
  9. <td> 
  10. <p>Add Link</p> 
  11. </td> 
  12. </tr>'; 
  13.  
May 27 '10 #5

Dormilich
Expert Mod 5K+
P: 8,639
large blocks of HTML can also be defined using the Heredoc or Nowdoc syntax.
May 27 '10 #6

P: 3
Hi,

I have tried a your code and added the alertme function:
Expand|Select|Wrap|Line Numbers
  1. <head>
  2. <script type="text/javascript">
  3.     alertme = function(){
  4.         alert("me");
  5.     }
  6. </script>
  7. </head>
  8. <?php
  9. echo
  10.  '<tr>
  11.  <td>
  12.  <input title="Add New Link" type="image"
  13.  height="24" width="24"
  14.  src="add.png" name="add_link"
  15.  onclick="alertme();">
  16.  </td>
  17.  <td>
  18.  <p>Add Link</p>
  19.  </td>
  20.  </tr>';
  21. ?>
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?
May 27 '10 #7

Dormilich
Expert Mod 5K+
P: 8,639
you don’t even need PHP to output static HTML.
May 27 '10 #8

P: 3
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.
May 27 '10 #9

Post your reply

Sign in to post your reply or Sign up for a free account.