开发者

Simple calculation php [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

hye guy, Actually i'm still new in programming. just wanna asking what wrong with this code? i'm trying to make calculation inside a form. Really appreciate if your guys can help. Thank you.

<?php
$script = "
window.addEvent('domready', function() {
function calculate() {

var value1 = $('value1').value;
var value2 = $('value2').value;
var value3 = $('value3').value;
var value4 = $('value4').value;
var value5 = $('value5').value;


var total = ( value1 + value2 + value3 + value4 + value5);


// Check that the result is a finite number. If so, display the results.
if (开发者_运维知识库!isNaN(monthly) &&
  (monthly != Number.POSITIVE_INFINITY) &&
  (monthly != Number.NEGATIVE_INFINITY)) {


  total.value = round(value1 +value2 +value3 +value4 + value5);

} else {
  // Otherwise, the user's input was probably invalid, so don't
  // display anything.

  total.value = '';

}
} 

// rounds number to two decimal places.
function round(x) {
return Math.round(x*100)/100;
}
$('compute').addEvent('click', calculate );
$('value1').addEvent('change', calculate );
$('value2').addEvent('change', calculate );
$('value3').addEvent('change', calculate );
$('value4').addEvent('change', calculate );
$('value5').addEvent('change', calculate );


 });
";
$doc =& JFactory::getDocument();
$doc->addScriptDeclaration($script);
 ?>
<table>
<tr><td colspan="3"><b>Enter Amount Information:</b></td></tr>
<tr>
  <td>1)</td>
  <td>Amount of the 1 :</td>
  <td><input type="text" name="value1" id="value1" size="12" ></td>
 </tr>

<tr>
  <td>2)</td>
  <td>Amount of the 2 :</td>
  <td><input type="text" name="value2" id="value2" size="12" ></td>
</tr>

<tr>
  <td>3)</td>
  <td>Amount of the 3 :</td>
  <td><input type="text" name="value3" id="value3" size="12" ></td>
</tr>

<tr>
  <td>4)</td>
  <td>Amount of the 4 :</td>
  <td><input type="text" name="value4" id="value4" size="12" ></td>
</tr>
<tr>
  <td>5)</td>
  <td>Amount of the 5 :</td>
  <td><input type="text" name="value5" id="value5" size="12" ></td>
</tr>

<tr><td colspan="3">
  <input type="button" value="Compute" id='compute' " >
</td></tr>
<tr><td colspan="3">
  <b>Payment Information:</b>
</td></tr>


<tr>
  <td>5)</td>
  <td>Your total will be:</td>
  <td><input type="text" name="total" id="total" size="12" readonly='readonly'></td>
</tr>

</table>
?>

Regards, ADik mat


it should be

 $('#value1').val()

instead of

 $('value1').value

if you are using jquery and selecting elements by id


1. Fix a mistake

remove ?> at the end of the code

2. wrong tag closure

change to this code

<input type="button" value="Compute" id='compute' />

3. Instead of using Window.event('domready')

Use

$(document).ready(function() { } );

If you're already using jQuery.

4. It is always good to put a table in

<body></body>

tag.

That means you should start with

<html> 
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
    </head>
    <body>
        //put some contents like table
    </body>
</html>

5. I think pretty much of your javascript code can be reduced by using below snippet.

$(':text').change(function() {
    var sum = 0;
    $(this).each(function() {
        sum += $(this).val();
    });
    $("#total").val(sum);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜