简体   繁体   中英

How can i insert javascript inside a PHP echo function?

I can not get the button clicked when i insert it inside an echo..

echo '<input onclick="window_target = document.basicForm.maparea1; 
                      window_name = window.open("search.php", 
                                                "window_name",
                                                "resizable=0,location=0,directories=0,status=0,top=30,left=100,toolbar=no,width=180,height=480,menubar=no,scrollbars=no"
                                                );
                      window_name.window_target = window_target
                     " value="Select" type="button">';

welcome to modern JS: try not to use onclick , or in fact any on... handler embedded in the HTML directly. Instead, generate your page to do something like:

<!doctype html>
<html>
  <head>...</head>
  <body>
    <input type="..." id="spawnClickerThing" ...>
    ...
    <script src="mypagescript.js"></script>
  </body>
</html>

(so not PHP echo/printed!) and then in that javascript include, do all the event binding etc:

...
function newWindowHandling(avt) {
  var window_target = document.basicForm.maparea1;
  var window_name = window.open("search.php", "window_name", "...");
  window_name.window_target = window_target;
}

function setEverythingUp() {
  document.removeEventListener("DOMContentLoaded", setEverythingUp);      

  var inputclicker = document.querySelector("#spawnClickerThing");
  inputclicker.addEventListener("click", newWindowHandling);
}

document.addEventListener("DOMContentLoaded", setEverythingUp);

depending on whether you use libraries for more efficient JavaScript, that code can be compacted, but the general principle holds. Don't inject JS "inline" using PHP, it's extremely fragile and impossible to maintain. Keep your script in .js files that are easy to edit, independently of the .php scripts that generate the HTML source code for your pages.

转义报价

echo '<input onclick="window_target = document.basicForm.maparea1; window_name = window.open(\'search.php\', \'window_name\', \'resizable=0,location=0,directories=0,status=0,top=30,left=100,toolbar=no,width=180,height=480,menubar=no,scrollbars=no\'); window_name.window_target = window_target" value="Select" type="button">';

you can't this way for this to work you have to use

you have to use like this in your javascript .i am assuming you have jquery included

$(document).on('click', '.btninline' ,function() {
window_target = document.basicForm.maparea1; 
window_name = window.open("search.php", "window_name","resizable=0,location=0,directories=0,status=0,top=30,left=100,toolbar=no,width=180,height=480,menubar=no,scrollbars=no"); 
window_name.window_target = window_target;
}

and then in your php

echo '<input class="btninline" value="Select" type="button">';

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