这里下面的编码输出值显示在div标签中。但我想在文本框中显示。另一个我想要的第一个文本框值来自db使用PHP。请帮帮我。
<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 = '£'+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 :(得分:0)
要在文本框中显示,很容易,而不是:
theDiv.innerHTML = value;
DO
theTextbox.value = value;
要用php数据填充它,你应该使用Ajax,请研究一下。 希望这可以帮助。干杯
答案 1 :(得分: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/>
£<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>
答案 2 :(得分:0)
将输入标记插入div:
<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); ?>