2011-07-05 48 views
0

這裏下面的代碼輸出值是以div標籤顯示的。但我想顯示在文本框中。另一個我想要的第一個文本框值來自數據庫使用PHP。請幫幫我。Javascript計算

<html> 
<head> 
<style> 
.test { 
    color:red; 
} 
#total { 
    font-size:40px; 
} 
h1 { 
    margin: 0 0 10px 0; 
    text-decoration: underline; 
    text-align: center; 
} 
h2 { 
    margin: 0 0 10px 0; 
} 
</style> 
<script> 
    calculate = function(totalElement) 
    { 
     if (totalElement) 
     { 
      var calculation = ''; 
      var overall = ''; 
      var fields = new Array(); 
      var theElement = document.getElementById(totalElement); 
      var userInputs = myform.elements; 
      var the_type = ''; 
      for (var f = 0; f < userInputs.length; f++) 
      { 
       if (userInputs[f].className=='special_value') 
       { 
        if (userInputs[f].type=='select-one') 
        { 
         if(userInputs[f].options[userInputs[f].selectedIndex].value) 
         { 
          fields[f] = userInputs[f].options[userInputs[f].selectedIndex].value; 
         } 
         else 
         { 
          fields[f] = 0; 
         } 
        } 
        else if(userInputs[f].type=='radio' || userInputs[f].type=='checkbox') 
        { 
         if (userInputs[f].checked) 
         { 
          fields[f] = userInputs[f].value; 
         } 
         else 
         { 
          fields[f] = 0; 
         } 
        } 
        else 
        { 
         if (userInputs[f].value) 
         { 
          fields[f] = userInputs[f].value; 
         } 
         else 
         { 
          fields[f] = 0; 
         } 
        } 
       } 
      } 

      for (var i=0; i<fields.length; i++) 
      { 
       calculation += fields[i]; 

       if (i!=fields.length-1) 
       { 
        calculation += '+'; 
       } 
      } 

      if (calculation!='') 
      { 
       overall = eval(calculation).toFixed(2); 
      } 

      if (overall!='') 
      { 
       theElement.innerHTML = '&pound;'+overall; 
      } 
     } 
    } 
</script> 
</head> 
<body> 
<form name="myform"> 
<input onKeyPress="calculate('total');" onBlur="calculate('total');" class="special_value" type="text" name="price"> <Br/> 
<input onKeyPress="calculate('total');" onBlur="calculate('total');" class="special_value" type="text" name="price2"> <Br/> 
<input onKeyPress="calculate('total');" onBlur="calculate('total');" class="special_value" type="text" name="price4"> <Br/> 
<div id="total"></div> 
<input onClick="calculate('total');" type="button" name="button" value="re-calculate"> 
</form> 
</body> 
</html> 
+0

呃,你的代碼不是全部那裏。複製粘貼問題? – tjameson

+0

爲什麼這個標籤爲「php」? – wonk0

+0

是的。但我想要解決方案。如果你有手段幫助我。 – boopathi

回答

0

要顯示一個文本框,是容易的,而不是:

theDiv.innerHTML = value; 

theTextbox.value = value; 

用PHP數據填充它,你應該使用Ajax,請研究一下吧。 希望這有助於。歡呼聲

0

試試這個可以正常工作。

<?php 

    $dbPrice = 20;// replace this by your query result 
?> 
<html> 
<head> 
<style> 
.test { 
    color:red; 
} 
#total { 
    font-size:40px; 
} 
h1 { 
    margin: 0 0 10px 0; 
    text-decoration: underline; 
    text-align: center; 
} 
h2 { 
    margin: 0 0 10px 0; 
} 
</style> 
<script> 
    calculate = function(totalElement) 
    { 
     if (totalElement) 
     { 
      var calculation = ''; 
      var overall = ''; 
      var fields = new Array(); 
      var theElement = document.getElementById(totalElement); 
      var userInputs = myform.elements; 
      var the_type = ''; 
      for (var f = 0; f < userInputs.length; f++) 
      { 
       if (userInputs[f].className=='special_value') 
       { 
        if (userInputs[f].type=='select-one') 
        { 
         if(userInputs[f].options[userInputs[f].selectedIndex].value) 
         { 
          fields[f] = userInputs[f].options[userInputs[f].selectedIndex].value; 
         } 
         else 
         { 
          fields[f] = 0; 
         } 
        } 
        else if(userInputs[f].type=='radio' || userInputs[f].type=='checkbox') 
        { 
         if (userInputs[f].checked) 
         { 
          fields[f] = userInputs[f].value; 
         } 
         else 
         { 
          fields[f] = 0; 
         } 
        } 
        else 
        { 
         if (userInputs[f].value) 
         { 
          fields[f] = userInputs[f].value; 
         } 
         else 
         { 
          fields[f] = 0; 
         } 
        } 
       } 
      } 

      for (var i=0; i<fields.length; i++) 
      { 
       calculation += fields[i]; 

       if (i!=fields.length-1) 
       { 
        calculation += '+'; 
       } 
      } 

      if (calculation!='') 
      { 
       overall = eval(calculation).toFixed(2); 
      } 

      if (overall!='') 
      { 
       theElement.value = overall; 
      } 
     } 
    } 
</script> 
</head> 
<body> 
<form name="myform"> 
<input onKeyPress="calculate('total');" onBlur="calculate('total');" class="special_value" value="<?php echo $dbPrice;?>" type="text" name="price"> <Br/> 
<input onKeyPress="calculate('total');" onBlur="calculate('total');" class="special_value" type="text" name="price2"> <Br/> 
<input onKeyPress="calculate('total');" onBlur="calculate('total');" class="special_value" type="text" name="price4"> <Br/> 
&pound;<input class="special_value" value="" id="total" type="text" name="total"> <Br/> 
<!-- <div id="total"></div> --> 
<input onClick="calculate('total');" type="button" name="button" value="re-calculate"> 
</form> 
</body> 
</html> 
0

插入格輸入的標籤:

<div id="total"><input type="text" id="theresultdiv" value="0" /></div> 

相反theElement.innerHTML的= '£' +整體;你應該放置這個代碼:

document.getElemenbById('theresultdiv').value = overall; 

正如你所建議的,你可以使用AJAX從數據庫中獲取數據。 但在你的情況下,最好能夠使用簡單的代碼:

<?php 
// we connect to example.com and port 3307 
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password'); 
if (!$link) { 
    die('Could not connect: ' . mysql_error()); 
} 
// make foo the current db 
$db_selected = mysql_select_db('foo', $link); 
if (!$db_selected) { 
    die ('Can\'t use foo : ' . mysql_error()); 
} 
$rResult = mysql_query("SELECT start_value FROM your_table WHERE start_value > 0 LIMIT 1", $link); 
$iResult = mysql_fetch_query($rResult) ; 
mysql_close($link); 
?> 

把上面的代碼在你的文件的開頭。 而不是0在價值屬性的地方:

<?php echo($iResult); ?>