简体   繁体   中英

Javascript function works on button click but not in PHP echo

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{
}
?>

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>

try this

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM