Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php类中插入或使用Javascript或jquery?_Php_Javascript_Jquery_Oop - Fatal编程技术网

如何在php类中插入或使用Javascript或jquery?

如何在php类中插入或使用Javascript或jquery?,php,javascript,jquery,oop,Php,Javascript,Jquery,Oop,我想知道如何在php类中插入或使用javascript或jquery。在这段代码中,我想在用户每次单击add按钮时显示一个警报,如果输入框中没有数字,那么我想显示一个警报,如果数字为零,我想在执行之前显示警报 <!DOCTYPE html> <html> <head><title>OOP Adding Machine</title></head> <body> <div align="center">

我想知道如何在php类中插入或使用javascript或jquery。在这段代码中,我想在用户每次单击add按钮时显示一个警报,如果输入框中没有数字,那么我想显示一个警报,如果数字为零,我想在执行之前显示警报

<!DOCTYPE html>
<html>
<head><title>OOP Adding Machine</title></head>
<body>
<div align="center">
<h1 style="color:#00F">Welcome User</h1>
<p style=" color:#00F; font-size:18px; font-weight:bold ">Please enter two numbers in the given input boxes, and this program will add them and display the answer.</p>

<form action="code.php" name="form1" method="post"> <br /><strong>Number#1:</strong><input type="text" name="adder1" /> <br /><strong>Number#2:</strong><input type="text" name="adder2" /><br /><input type="submit" name="submit" value="ADD"/> </form>
</div>
</body>

</html>

面向对象的加法器
欢迎用户
请在给定的输入框中输入两个数字,本程序将添加它们并显示答案


编号#1:
编号#2:
以下是php oop代码:

<?php
class addition
    {
     var $adder3;

     function adder($adder1,$adder2)
         {
          $this->adder3=$adder1+$adder2;
         }


     function sol()
         {

        echo "<h1>"." Result: ".$this->adder3."</h1>" ;
        echo "<h2>"."Program Executed"."</h2>";

         }
    }

$result=new addition();
$result->adder($_POST['adder1'],$_POST['adder2']);
$result->sol();

?>

无论您想在哪里,都可以在您的代码中尝试这一点:

echo '<script type="text/javascript">alert("Some text"); </script>';
echo'alert(“某些文本”);

将JavaScript包含在php文件中的最佳方法是创建一个外部JS文件,将其放在公共/JS文件夹中,并将JS包含在您的头中

 <script type='text/javascript' src='/public/js/example.js'></script>

Yoc还可以在同一个php文件的头部顶部包含JS和JQuery,如

<html>
  <head>
     <script type='text/javascript'>
         //your JS code here
     </script>

     $(document).ready(function() {
            $("#submit").on("click", function(e) {

                $.ajax({
                    url: "url",
                    type: 'POST',
                    data: data,
                    success: function(msg) {
                        //ajax success return
                        }
                        else{
                           //ajax call failed
                        }
                    }
                });
            });
        });
  </head>
</html>

//你的JS代码在这里
$(文档).ready(函数(){
$(“#提交”)。在(“单击”)上,功能(e){
$.ajax({
url:“url”,
键入:“POST”,
数据:数据,
成功:功能(msg){
//ajax成功回归
}
否则{
//ajax调用失败
}
}
});
});
});
上面的代码展示了如何在ajax调用中包含简单的JS函数和Jquery

最后,您还可以像在脑海中一样在php文件之间添加内容,但就我的经验而言,最好将JS、html和逻辑分隔开来


The best way would be calling Jquery AJAX to do this.

        On even, you can send the ajax request and then call back and grab the data in javascript and then display the alert


        In this case, you don't have to put javascript in php. Simple output value from Php file in ajax request and grab the value and display the event.

      See

      $.ajax({
      url: "test.php",
      context: document.body
    }).done(function() { 
      $(this).addClass("done");
    });

  A lots of option supported by Jquery to capture the events 

  See: http://api.jquery.com/jQuery.ajax/

Cheers
警报($('myElementId').text());
这很有帮助,但我也需要插入jquery,而且一点也不含糊idea@user1645961
echo'
您可以使用外部javascript文件,也可以使用js或jquery在php类中进行验证。要进行验证,您必须将I插入控件的jquery或javascript代码中。我使用jQuery来实现这一点,例如,如果您想控制id为=“id_text”的输入文本,您可以使用jQuery进行检查:if(!$(“#id_text”).val(){alert(“ERROR”)},这与从php输出html相同。相反,o
echo'something'
youdo
echo'…'你在使用模板吗?不,我没有使用模板吗?>因为它允许你编写HTML/CSS,而无需通过PHP进行回显。谢谢,我不知道这一点。不理解jsExample(){}如果调用jsExample,它基本上只会打印出其中的JavaScript
<?php
class addition
{
    var $adder3;

    function adder($adder1,$adder2)
    {
        $this->adder3=$adder1+$adder2;
    }


    function sol()
    {
        echo "<h1>"." Result: ".$this->adder3."</h1>" ;
        echo "<h2>"."Program Executed"."</h2>";
    }

    function jsExample(){
        ?>
        <script type="text/javascript">
        alert ( $('#myElementId').text() );
        </script>
        <?php
    }
}

$result=new addition();
$result->adder($_POST['adder1'],$_POST['adder2']);
$result->jsExample();
$result->sol();
?>