0

JavaScript:

function test(){
 alert('onchange success');
}

PHP + HTML :

<select id="mySelect" onchange="test();">
<?php
 $myRow = 1;
 for($i = 0 ; $i < 3 ; $i++){
  if($i == $myRow){
   echo("<option selected='selected'>$i</option>");
  } else {
    echo("<option selected=''>$i</option>");
  }
 }
?>
</select>

My problem and question

Why function test() doesn't get called when PHP declared option is selected?

Additional Info: No errors in Console.

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Mr. Mike
  • 453
  • 5
  • 23
  • _"No error show in Console"_, Php won't show errors in console unless you're running a Php script from the command line. If you're talking about the Chrome Dev Tools console (or similar in other browsers), it will show JS errors/warnings. – Emile Bergeron Apr 20 '17 at 02:13
  • yes i mean that @emile-bergeron – Mr. Mike Apr 20 '17 at 02:15
  • It's not called because Php is a server-side scripting language. It's happening before JS is even in the browser. The `onchange` callback is not called because it's not changing. – Emile Bergeron Apr 20 '17 at 02:22
  • So how if i want call the javascript when i select the option using PHP? @emile-bergeron – Mr. Mike Apr 20 '17 at 02:23
  • You can't "call" JavaScript with Php. This is a classic [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) where you're asking the wrong question. You want to achieve something (that you're not telling us), and you think that you need to call JS with Php. – Emile Bergeron Apr 20 '17 at 02:25
  • Possible duplicate of [How to call a JavaScript function from PHP?](http://stackoverflow.com/questions/1045845/how-to-call-a-javascript-function-from-php) – Emile Bergeron Apr 20 '17 at 02:30

1 Answers1

-1

$myRow is out of scope (in fact, not declared in PHP at all given the code snippets) because it is declared and defined outside of th <?php tag.

Try:

<script>
    function test(){
        alert('onchange success');
    }
</script>

<select id="mySelect" onchange="test();">
<?php
    $myRow = 1;
    for($i = 0 ; $i < 3 ; $i++){
        if($i == $myRow){
            echo("<option selected='selected'>$i</option>");
        } else {
            echo("<option selected=''>$i</option>");
        }
    }
?>
</select>

We can test the generated PHP at https://jsfiddle.net/r0Lcy730/

Matt Styles
  • 581
  • 1
  • 4
  • 14
  • Updated the question, i wrong copy paste from the sample. sorry. – Mr. Mike Apr 20 '17 at 02:05
  • I've updated my answer to reflect your changes. This script works for me. If it doesn't work for you, try chucking error_reporting(E_ALL); at the top of the file and giving us the output? – Matt Styles Apr 20 '17 at 02:09
  • javascript not executed. but i really sure the javascript can run in my browser. – Mr. Mike Apr 20 '17 at 02:17
  • Could you detail what output you get in your JS Console in your browser> – Matt Styles Apr 20 '17 at 02:18
  • Have you tried adding error_reporting(E_ALL); to the top of your file first? Please could you paste exactly the PHP+HTML you are executing? The following site generates the HTML+JS which I mentioned in my post above, so I'm not sure what is different in your code to mine: http://sandbox.onlinephpfunctions.com/code/68cb487bff0bf3dc0cae8e81248ccc08010eaa1d – Matt Styles Apr 20 '17 at 02:29