0

Can anyone tell me why my javascript function works for my button "onclick" but will not work from a php echo? The code is below. Thank you!

This works:

<button onclick="activate_modal('info')">This Works!</button>

But this does not:

<?php
if(1==1){
echo "<script type='text/javascript'>";
echo "activate_modal('info');";
echo "</script>";
}
else{
}
?>
2
  • 1
    add the script at bottom of the page Commented Mar 28, 2014 at 5:15
  • 2
    you should call your script after you have define activate_modal() function. Commented Mar 28, 2014 at 5:17

4 Answers 4

1

You have to change the code order. First you have put your php code. This is the simple alert example. Try this

 <?php

    echo "<script type='text/javascript'>";
    echo "activate_modal('info');";
    echo " function activate_modal(a){alert(a);}";
    echo "</script>";

    ?>


    <button onclick="activate_modal('info')">This Works!</button>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you - I'm pretty sure this is the answer. It just seems silly to me that you declare the function AFTER you call it.. but that is probably my lack of experience talking. So if I want to call the same function several times, the only way to do this (with PHP echos) is to declare the function again and again each time I need to use it?
Yes. I don't know what exactly you need. But the thing is, If you want to create JavaScript through the PHP echo method you have to follow this order. Then only you can create the functions.
Thanks for your help. I have "fancy" modals that use jquery/javascript to fade the entire screen and give a message, then allow you to click anywhere to close it. I am using PHP to validate form entries, and I want to basically say "if passes, continue, if not, call jquery modal" ...was hoping that I could simply use PHP echos to call the function but I suppose it's not that easy.
0

try this

<body  <?php echo "onload=activate_modal('info');"; ?>  >
----
----
</body>

Comments

0

Try this, this might be help you for calling function.

<?php
if ($ID = '') {
echo "<script language=javascript>alert('Please enter a valid username.')</script>";
}
?>

Comments

0

Something to consider when using PHP: It's always the FIRST thing interpreted. Likely, your javascript functions hadn't been loaded when the HTML telling the javascript to call that function was inserted into the page.

You can fix this by putting that script any time AFTER the javascript function has been declared in the HTML.

3 Comments

Thank you -- I actually have the function declared in the "head" section of the page, and I am trying to call the function in the "body" section. It is odd because it works with the button "onclick" just fine, but won't run in the php echos immediately below the button...
Another thing you can try is moving that php blurb to the very bottom of the body tag, so it gets called when the body is finished loading. As other answers suggested, you can put some php to insert that script into the body "onload" property.
Thanks again - please see my comments on the answer I selected -- explains a little better what I am trying to do. Appreciate your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.