can you add two fields on a website?
I am trying to have a user imput data into several fields, then I want to be able to add that value and take a percentage of it. I realize that I could just add the variables, however I woul开发者_运维问答d like the user to be able to see the total and the percentage. If you would like to look at the site just for reference,
http://clubs.psu.edu/bk/sga/eform/budget.php thanks!im not sure what do you mean, but if you just want user to fill the form and then do some math on the results, you can do simply like that:
$total = $_POST['field1'] + $_POST['field2'] + ... etc.
then:
$percentage = /* here some math for computing percentage */;
and just display both in your template.
UPDATE
ok, so try something like that (with newest jQuery, just modify it to your needs):
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
1st number: <input type="text" id="f1" value="0"/><br/>
2nd number: <input type="text" id="f2" value="0"/><br/>
<p></p>
<script>
$("input").keyup(function () {
var add = parseInt($("#f1").val())+parseInt($("#f2").val());
var per = parseInt($("#f1").val())/parseInt($("#f2").val())*100;
$("p").html("add: "+add+"<br/>number1 is "+per+"% of number2");
}).keyup();
</script>
</body>
</html>
that is what u expect?
The javascript code doesn't care about the forms.
I would suggest you use jQuery just to make it easier to add an event handler to each of the input tags, but when any value is entered, just start to add to the total and calculate the percentage, but, don't show it until everything in where you want data entered is completed, unless you want to show a live value as they enter each value.
精彩评论