views:

97

answers:

2

Hi all, 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 would 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!

A: 

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.

James Black
He actually only has one form in the source. I think he meant fields in that instance
Rob Allen
that sounds like a good suggestion, but I dont know hardly any java... Is it possible without?
Ryan
and yes, sorry I meant fields
Ryan
@Ryan - javascript and java are very different, and you can look at the jquery tutorials to get an idea how to add event handlers, but you would need to know a little javascript to do the calculations and then use the val() function in jquery to display the new value. jQuery will make this much easier.
James Black
+1  A: 

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"&gt;&lt;/script&gt;
    </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?

budzor
well, I could do it that way for the recipient of the template, however, I want the user to be able to see the result on the page. For instance, If I enter "5" in the first field, and "6" in the second, In my "total" field, I want it to display "11" without clicking submit
Ryan
check UPDATE in my anwser
budzor
let me test it real quick, sorry Im still learning this forum
Ryan
thats perfect, Thanks, I have to modify it a bit but it works fine, thanks!
Ryan