admin管理员组

文章数量:1279113

I have a php page and i have some javascript code to have a running total of some fields. i pretty well copied from a working test that i had and modified the code some to fit. well it doesn't work and i can't seem to work. is there something blatently obvious i'm missing or is there some other reason why it's not running?

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

I have a php page and i have some javascript code to have a running total of some fields. i pretty well copied from a working test that i had and modified the code some to fit. well it doesn't work and i can't seem to work. is there something blatently obvious i'm missing or is there some other reason why it's not running?

 <?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>
Share Improve this question asked Apr 25, 2009 at 15:58 DForck42DForck42 20.4k14 gold badges60 silver badges85 bronze badges
Add a ment  | 

5 Answers 5

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

produces error, because you don't seem to have element with id="d"

I would also like give to some advise to improve your code.

Debugging javascript

  • firefox extension firebug is absolutely a must for debugging your javascript.

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

Writting Javascript

  • Put javascript in a separate file, for example: myjavascript.js and include that file from your html page. This will reduce your page weight and you will have cleaner separation of code.
  • Use javascript framework to help you produce better javascript, for example jquery or yui(The Yahoo! User Interface Library).

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

Writting PHP

  • Better separation of PHP/HTML by putting PHP in seperate file and include it. For example:

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

A few things to check.

  • Does it not work in all browsers?
  • Have you confirmed that the exact code works when in a separate file
  • Have you tried to move the script to the bottom of the page?

You should also use the Debugger in Firefox or even find a tool like Firebug for Firefox. Helps a lot in finding issues like this.

Please try to shorten your question! Isolate it!

I can also highly remend using Firebug

You'll find the answer in no-time :)

You have no element "d" in your HTML. If you ment out the line:

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

It should work fine

本文标签: Javascript won39t run on php pageStack Overflow