0

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">';
1
  • 2
    Invalid quoting in the echoed tag. Commented Sep 19, 2014 at 20:11

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

escape the quotes

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">';

1 Comment

Surely there's a better way to write this than a 320-character line that breaks my scrollbar?
0

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">';

Comments

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.