2012-09-04 23 views
0

我想知道如何在php類中插入或使用javascript或jquery。在這段代碼中,我希望每次用戶單擊添加按鈕時都會顯示一條警告,如果輸入框中沒有數字,那麼我想在數字爲零時顯示警報。我想在執行前顯示警報。如何在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(); 

?> 
+0

它與從php輸出html相同。而不是'回聲'

東西

''你做'回聲'';' –

+0

你使用模板嗎? – Gerep

+0

nope我沒有使用模板 – user1645961

回答

0

試試這個到你的代碼,無論你想:

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

好吧,但它也有幫助,但我也需要插入jquery,並且沒有模糊的想法 – user1645961

+0

@ user1645961'echo'' ;' –

+0

你可以使用外部JavaScript文件 – 2012-09-04 11:16:39

1

包含的JavaScript在PHP文件的最佳方法是創建一個外部JS文件,並把它放在你的公共/ JS文件夾中,並將其包含在你的腦袋裏,就像

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

YOC還可以包括JS和JQuery在同一PHP文件在像

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

在頭部的頂部上面的代碼說明了如何包括與AJAX調用沿着簡單JS funcrtion和jQuery

最後,您還可以將PHP文件之間包括在以同樣的方式,你包括在你的頭上,但就我的經驗而言這是awlyas更好地保持你的JS,HTML和邏輯separatley

0
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 
+0

這不是一個選項 – user1645961

0
<?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(); 
?> 
+0

?><?php爲什麼這樣寫php標籤?原諒我,如果我問了一個愚蠢的問題,我是一個新手。 – user1645961

+0

因爲它允許你編寫HTML/CSS而不必通過PHP回顯。 –

+0

謝謝,我不知道。不明白jsExample(){} – user1645961