The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> IF Calculations based on dropdown list
coryjacques
post Dec 13 2018, 02:10 PM
Post #1





Group: Members
Posts: 2
Joined: 13-December 18
Member No.: 26,771



Hello!

This is probably a little bit HTML and a lot a bit... something else (I'm new to HTML/Java/etc).

I have a code (below) to set up a two-question form. I'm looking to calculate excel-style based on the values a user selects. Here's the code:

CODE
<!DOCTYPE html>
<html>
<head>
    <title>How much will your plan cost?</title>
</head>
<body>
<table>
<tr></tr>
    <td><div align="Center"><h5>How much will your plan cost?</h5></td></tr>
    <Form action="processingscript.php" method="post">
    <tr>
    <td><p>How old are you?<select>
            <Option>Under 24</Option>
            <option>25-29</option>
            <option>30-34</option>
            <Option>35-39</option>
            <Option>40-44</option>
            <Option>45-49</option>
            <Option>50-54</option>
            <Option>55-59</option>
            <Option>60-64</option>
            <Option>65-69</option>

            </select></P></td></tr>
    <tr><td><p>How much of a benefit amount are you looking for?<Select>
        <Option> $10000</option>
<Option> 20000</option>
<Option> 30000</option>
<Option> 40000</option>
<Option> 50000</option>
<Option> 60000</option>
<Option> 70000</option>
<Option> 80000</option>
<Option> 90000</option>
<Option> 100000</option>
<Option> 110000</option>
<Option> 120000</option>
<Option> 130000</option>
<Option> 140000</option>
<Option> 150000</option>
<Option> 160000</option>
<Option> 170000</option>
<Option> 180000</option>
<Option> 190000</option>
<Option> 200000</option>
<Option> 210000</option>
<Option> 220000</option>
<Option> 230000</option>
<Option> 240000</option>
<Option> 250000</option>
<Option> 260000</option>
<Option> 270000</option>
<Option> 280000</option>
<Option> 290000</option>
<Option> 300000</option>
<Option> 310000</option>
<Option> 320000</option>
<Option> 330000</option>
<Option> 340000</option>
<Option> 350000</option>
</Select><br>
<tr><td><div align="center"><input type="submit" value="Calculate!"></div></tr></td>
    </Form>
</table>
</center>
</body>
</html>


Here's the table I'm trying to pull from - I'm essentially looking for something along the lines of "If the first box says 25-29, .04 will be multiplied by the value in the second box" for example.


Age Band | Multiple
<24 | $0.04
25-29 | $0.04
30-34 | $0.05
35-39 | $0.08
40-44 | $0.12
45-49 | $0.21
50-54 | $0.36
55-59 | $0.59
60-64 | $0.79
65-69 | $1.25


If it helps, in excel this would be something along the lines of (if box 1 is a1 and box 2 is a2): =if(or(a1="<24",a1="25-29"),a2*.04,if(a1="30-34",a2*.05, .....etc.

Any tips?

This post has been edited by coryjacques: Dec 13 2018, 02:17 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Dec 13 2018, 03:26 PM
Post #2


.
********

Group: WDG Moderators
Posts: 9,628
Joined: 10-August 06
Member No.: 7



QUOTE(coryjacques @ Dec 13 2018, 08:10 PM) *

Hello!

Hello!

QUOTE
This is probably a little bit HTML and a lot a bit... something else (I'm new to HTML/Java/etc).

The form submits the data to processingscript.php, so you might do the processing in a PHP script at that URL and let the script generate an HTML page with the result. In addition you could use javascript to show a preview before the form is actually submitted, but this is not necessary.

What is your server doing with the data once it's submitted?

QUOTE
Here's the table I'm trying to pull from - I'm essentially looking for something along the lines of "If the first box says 25-29, .04 will be multiplied by the value in the second box" for example.

A simple way might be to use the multiplier as the value of each OPTION element, like this:

CODE
<Option value="0.04">Under 24</Option>
<option value="0.04">25-29</option>
<option value="0.05">30-34</option>

(and something similar on the second SELECT menu's OPTION elements). When the form is submitted, the PHP script can then use the OPTION values directly, without the need for IF/ELSE conditions (but don't forget to sanitize the submitted data to prevent malicious code injection).



User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
coryjacques
post Dec 19 2018, 02:10 PM
Post #3





Group: Members
Posts: 2
Joined: 13-December 18
Member No.: 26,771



Thank you! I know absolutely nothing about PHP, but I'll look up some lessons and see if I can figure it out!

Our server isn't doing anything with the data once submitted - this is actually meant to make up for a few shortcomings in our new HRIS; it's no more than a calculator and reference.

This post has been edited by coryjacques: Dec 19 2018, 02:12 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Dec 19 2018, 03:02 PM
Post #4


.
********

Group: WDG Moderators
Posts: 9,628
Joined: 10-August 06
Member No.: 7



QUOTE(coryjacques @ Dec 19 2018, 08:10 PM) *

Our server isn't doing anything with the data once submitted - this is actually meant to make up for a few shortcomings in our new HRIS; it's no more than a calculator and reference.

In that case you might write it with javascript as well (and you don't need to submit any form, you just need the SELECT menus and a button for calculating). For example:

CODE
<script type="text/javascript">
function calculate_benefits()
{
    var age=document.getElementById('age').value;
    var benefit=document.getElementById('benefit').value;
    document.getElementById('result').value=age*benefit;
}
</script>

<select id="age">
<Option value="0.04">Under 24</Option>
<option value="0.04">25-29</option>
<option value="0.05">30-34</option>
</select>

<Select id="benefit">
<Option value="10000">$10000</option>
<Option value="20000">$20000</option>
<Option value="30000">$30000</option>
</select>

<p><input type="button" onclick="calculate_benefits();" value="Calculate"> <input type="text" id="result" value=""></p>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 19th March 2024 - 03:57 AM