Onkeypress函数没有定义,尽管已导入并在作用域中


onkeypress function isn't defined despite being imported and within scope

我似乎对这段代码有问题。

<td align = "right">Ingredients:</td> <td><textarea style="width: 300px" class="form-control" name="ingredients" rows="1" onkeypress='myFunction'></textarea></td>

我已经用require_once导入了它,但尽管我仔细检查了语法错误,我还是无法找到函数。我得到Uncaught ReferenceError: myFunction未定义。我把它放在这样的代码块中。

<?php
//some code I wrote
function myFunction() {
echo "hello";
}
?>

我在谷歌浏览器中尝试了这个,无论我尝试什么,我似乎都不能让它开火。你知道我做错了什么吗?

注意:到目前为止,我理解了响应,但我有点困惑,为什么这在另一个。php文件中不起作用,它们都是。php顺便说一句,我有这个发射正确…

$recipe = Recipe::getRecipes( $dbh, true );

与另一个.php文件包含…

    class Recipe 
    {           
        function copyFromRow( $row ) { 
         // some code
        } 

        static function getRecipes( $dbh, $withnumbers=false ) { 
            //some if else statement
            return $result; 
        } 
        //more irrelevant code
     }

现在我不确定我是否在这里和第一个例子之间做了不同的事情,所以有人能澄清一下吗?

不能在任何DOM事件上直接调用PHP函数。

你应该学习使用Ajax, $. post。

顺便说一句,你可以使用

<td align = "right">Ingredients:</td> <td><textarea style="width: 300px" class="form-control" name="ingredients" rows="1" onkeypress='myFunction()'></textarea></td>

<script>
function myFunction()
{
    $.ajax({
       type: "POST",
       url: 'yourphpfile.php',
       data: {data:data}, // optional if wish to pass data
       success: function(response){
          console.log(response);
       }
    });
}
<script>

$ . post

<script>
function myFunction()
{
     $.post( "yourphpfile.php", function( response ) {
       console.log(response);
     });
}
</script>

yourphpfile.php

 <?php
    if($_POST)
    {
       var_dump($_POST);
    }
  ?>

享受吧!