【问题标题】:How to Insert or use Javascript or jquery inside php classes? [closed]如何在 php 类中插入或使用 Javascript 或 jquery? [关闭]
【发布时间】:2012-09-04 11:02:35
【问题描述】:

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

<!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>

这里是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();

?>

【问题讨论】:

  • 和从php输出html是一样的。相反 o echo '&lt;h1&gt;something&lt;/h1&gt;' 你做 echo '&lt;script type="text/javascript"&gt;...&lt;/script&gt;';
  • 你在使用模板吗?
  • 不,我没有使用模板

标签: php javascript jquery oop


【解决方案1】:

在 php 文件中包含 JavaScript 的最佳方法是创建一个外部 JS 文件并将其放在您的 public/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 函数和 Jquery 以及 ajax 调用

最后,你也可以在你的 php 文件之间加入,就像你把它包含在你的头脑中一样,但就我的经验而言,最好将你的 JS、html 和逻辑分开

【讨论】:

    【解决方案2】:

    在任何你想要的地方尝试这个:

    echo '<script type="text/javascript">alert("Some text"); </script>';
    

    【讨论】:

    • 嗯,它有帮助,但我也需要插入 jquery 并且没有最模糊的想法
    • @user1645961 echo '&lt;script type="text/javascript" src="jquery.js"&gt;&lt;/script&gt;';
    • 您可以使用外部 javascript 文件
    • 还有如何使用 js 或 jquery 在 php 类中进行验证
    • 验证您必须将控件插入到您的 jquery 或 javascript 代码中。我使用 jQuery 来执行此操作,例如,如果你想控制 id="id_text" 的输​​入文本是否可以使用 jquery 进行检查: if (!$("#id_text").val()) { alert("ERROR" ); }
    【解决方案3】:
    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
    

    【讨论】:

      【解决方案4】:
      <?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();
      ?>
      

      【讨论】:

      • ?>
      • 因为它允许您编写 HTML/CSS 而无需通过 PHP 回显它。
      • 谢谢,我不知道。不明白 jsExample(){}
      • 如果你调用jsExample,它基本上只会打印出里面的JavaScript。
      • 我知道但我不理解 jsexample() 中的 js 代码
      猜你喜欢
      • 2014-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多