2009-04-25 84 views
1

我有一個PHP頁面,我有一些JavaScript代碼有一個運行總計的一些字段。我很好地複製了我的工作測試,並修改了一些適合的代碼。以及它不工作,我似乎無法工作。有什麼明顯的東西我失蹤或有其他原因,爲什麼它沒有運行?Javascript將無法運行在PHP頁面

<?php 

//server connection info 

?> 
<html> 
<head> 
<title>Survey</title> 
<link rel="stylesheet" type="text/css" href="styles.css" /> 

     <script type="text/javascript"> 
      function Total() 
      { 

       var a=document.getElementById("a").value; 
       var b=document.getElementById("b").value; 
       var c=document.getElementById("c").value; 
       var d=document.getElementById("d").value; 

       a=parseInt(a); 
       b=parseInt(b); 
       c=parseInt(c);  

       var total=a+b+c; 



       document.getElementById("total").value=total; 


      } 


     </script> 


</head> 


<body> 
<h1>QUALITY OF LABOR SURVEY</h1> 
<p /> 
<h2>ABOUT YOUR COMPANY</h2> 


<div class="Wrapper"> 

    <form id="Main" method="post" action="Process.php"> 

     <div class="Question"> 
       1. In what state and county is your business located? (click below) 
     </div> 
     <div class='answer'> 
     <?php 
      $tsql = "select 
       StateCountyID, 
       State, 
       County 
       from dbo.StateCounty 
       where State='MO' 
         and Active='True' 
       order by State"; 

      $tsql2 = "select 
       StateCountyID, 
       State, 
       County 
       from dbo.StateCounty 
       where State='IL' 
         and Active='True' 
       order by State"; 


      /* Execute the query. */ 
      $stmt = sqlsrv_query($conn, $tsql); 
      if ($stmt) 
      { 

       echo "<span><select name='ListMO'>"; 
       echo "<option value='0'>MO-County</option>"; 

       while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)) 
       { 

        echo "<option value='".$row[0]."'>"; 
        echo $row[2]; 
        echo "</option>"; 

       } 

       echo "</select></span>"; 

      } 
      else 
      { 
        echo "Error in statement execution.\n"; 
        die(print_r(sqlsrv_errors(), true)); 
      } 


      $stmt2 = sqlsrv_query($conn, $tsql2); 
      if ($stmt2) 
      { 

       echo "<span><select name='ListIL'>"; 
       echo "<option value='0'>IL-County</option>"; 

       while($row = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_NUMERIC)) 
       { 
        echo "<option value='".$row[0]."'>"; 
        echo $row[2]; 
        echo "</option>"; 

       } 

       echo "</select></span>"; 

      } 
      else 
      { 
       echo "Error in statement execution.\n"; 
       die(print_r(sqlsrv_errors(), true)); 
      } 
     ?> 
      </div> 



     <table width="700px"> 
      <tr> 
      <td style="font-size: 1.2em; font-weight: bold;"> 
       ABOUT YOUR EMPLOYMENT 
      </td> 
      <td style="font-weight: bold;"> 
       (Exclude Temporary Employees Throughout Survey) 

      </td> 
      </tr> 

     </table> 

     <p /> 
     <b>Please estimate the following:</b> 







     <p /> 




     <div class="Question"> 
       4. Number of Full-Time Hourly Employees (Eligible for Full-Time Benefits) 
     </div> 

     <div class="Answer"> 
       <input type="text" id="a" value="0" onchange="Total();" /> 
     </div> 

     <div class="Question"> 
       5. Number of Part-Time Hourly Employees(Not Eligible for Full-Time Benefits) 
     </div> 

     <div class="Answer"> 
       <input type="text" id="b" value="0" onchange="Total();" /> 
     </div> 

     <div class="Question"> 
       6. Salaried Employees 
     </div> 

     <div class="Answer"> 
       <input type="text" id="c" value="0" onchange="Total();" /> 
     </div> 





     <div class="Question"> 
       7. Is this your current number of employees? If not, change responses to 4, 5, and 6. 
     </div> 

     <div class="Answer"> 
       <input type="text" id="total" value="0" onchange="Total();" /> 
     </div> 

回答

8
var d=document.getElementById("d").value; 

產生錯誤,因爲你似乎不具有與元素id="d"

+0

好眼力那裏,SG – TheTXI 2009-04-25 16:02:39

+0

當我看着我以爲它說另一次是第一個*聳聳肩* – DForck42 2009-04-25 16:16:08

1

有幾件事情來檢查。

  • 它不適用於所有瀏覽器嗎?
  • 您是否確認在單獨的文件中確切的代碼有效
  • 您是否嘗試將腳本移動到頁面底部?

您還應該在Firefox中使用調試器,甚至可以爲Firefox找到類似Firebug的工具。幫助我們找到這樣的問題。

0

請儘量縮短你的問題!隔離它!

我也強烈建議使用Firebug

你會發現在你的HTML中沒有時間:)

0

答案你沒有元素「d」。如果您註釋掉行:

var d=document.getElementById("d").value; 

它應該工作正常

4

我也想給一些建議,以改善你的代碼。

調試JavaScript的

  • Firefox擴展Firebug是絕對的調試JavaScript的一絕。

https://addons.mozilla.org/en-US/firefox/addon/1843

書面方式使用Javascript

  • 在一個單獨的文件將JavaScript的,例如:myjavascript.js,包括從你的HTML頁面文件。這將減少你的頁面重量,你將有更清晰的代碼分離。
  • 使用javascript框架來幫助您生成更好的javascript,例如jquery或yui(Yahoo!用戶界面庫)。

http://jquery.com/
http://developer.yahoo.com/yui/

書面方式PHP

  • 更好地把PHP在單獨的文件中分離PHP/HTML的,包括它。例如:

http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html/