Forum Moderators: open

Message Too Old, No Replies

need little bit of form help

         

firemaster

5:15 am on Mar 13, 2007 (gmt 0)

10+ Year Member



I've been messing with this for a little bit, looking at tutorials and source code, but really don't know.

I think it's pretty simple javascript/ajax. I just want a few questions with radio buttons. When the option is clicked it will update the price without refresh and change the price options on the other radios.
IE:

A:
[_] Option 1 [-$50]
[X] Option 2 [included in price]
[_] Option 3 [+$50]
[_] Option 4 [+$100]
B:
[_] Option 1 [-$50]
[_] Option 2 [-$100]
[X] Option 3 [included in price]
[_] Option 4 [+$50]
Total: $250

like I said I've been trying and the only real things I can find use select options and load the prices in text fields, I don't want a pull down menu and would like them to load as just a <span>.

thanks a lot for any help on the subject.
-Mike

firemaster

3:16 am on Mar 14, 2007 (gmt 0)

10+ Year Member



Okay, well since I haven't recieved a reply could someone at least give me some code that would make a radio button work and the innerhtml display work?

Also when I try to make the numbers add up use (Math) I get the results as 100100100 instead of 300, I do (amount + amount2 + amount3) * 1 and the amount = getResult("result").value

-FM-

daveVk

6:01 am on Mar 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think it would help if you posted the relevent part of your javascript so that problem can narrowed down a bit.

scriptmasterdel

8:46 pm on Mar 14, 2007 (gmt 0)

10+ Year Member



Is this the kind of thing you are refering to (didn't take me long to write):

<script>

function calculate()
{
var formPrices = new Array();
var formTypes = new Array();
var formFields = document.myform.elements;
var thePriceField = document.getElementById("price");
var calculation = thePriceField.innerHTML = 0;
var i = 0;

for (var f = 0; f < formFields.length; f++)
{
if (formFields[f].className == 'calculation' && formFields[f].type == 'radio' && formFields[f].checked)
{
if (formFields[f].value)
{
formPrices[i] = formFields[f].value;
formTypes[i] = formFields[f].getAttribute("ptype");
i++;
}
}
}

for (var i = 0; i < formPrices.length; i++)
calculation += (formTypes[i] + formPrices[i]);

var overalCalculation = eval(calculation);

if (overalCalculation <= 0)
overalCalculation = 0;

thePriceField.innerHTML = overalCalculation;
}

</script>

<form name="myform">

<h3>Question A</h3>
<input onClick="calculate();" name="price1" type="radio" ptype="+" value="100" class="calculation"> + 100<br />
<input onClick="calculate();" name="price1" type="radio" ptype="-" value="200" class="calculation"> - 200<br />
<input onClick="calculate();" name="price1" type="radio" ptype="-" value="300" class="calculation"> - 300<br />
<input onClick="calculate();" name="price1" type="radio" ptype="+" value="400" class="calculation"> + 400<br /><br />

<h3>Question B</h3>
<input onClick="calculate();" name="price2" type="radio" ptype="+" value="500" class="calculation"> + 500<br />
<input onClick="calculate();" name="price2" type="radio" ptype="+" value="600" class="calculation"> + 600<br />
<input onClick="calculate();" name="price2" type="radio" ptype="-" value="700" class="calculation"> - 700<br />
<input onClick="calculate();" name="price2" type="radio" ptype="+" value="800" class="calculation"> + 800<br /><br />

</form>

<h2>Total: £<span id="price">0</span></h2>

I guess there is a much more simplified way of doing this but this works none the less.

If you add the attribute ptype to the radio button and specify the calculation operator then it will use that when it calculates all totals.

It also uses a class name so that it doesn't read all elements of the form, this is a convienience really.

I hope this is what you meant.

Good luck.

Del

firemaster

9:43 pm on Mar 14, 2007 (gmt 0)

10+ Year Member



It's very close to but not exactly. A good step and will help. I'm trying to have it so that the the radio has a name

A:
[_] OptionName 1 [+$50]
[_] OptionName 2 [+100]
[_] OptionName 3 [+$150]
[_] OptionName 4 [+$200]
Total: $0

and when you choose say option 2 it changes as so:

A:
[_] Option 1 [-$50]
[X] Option 2 [included in price]
[_] Option 3 [+$50]
[_] Option 4 [+$100]
Total: $100

I'm mainly a PHP programmer so some of this looks similar, I really wish I knew js/ajax. Thanks, like I said that's a good start for what I'm trying, better help then any tutorials or codes I have found yet.
Thanks,
-Mike

firemaster

6:45 am on Mar 15, 2007 (gmt 0)

10+ Year Member



I've been trying to work on this with no success.. I'm not quiet sure how to go about the second problem... I was trying to copy your loop to make a new function that would do it... wrong


function np()
{
var formPrices2 = new Array();
var formFields2 = document.myform.elements;
var formID = new Array();
var x = 0;
for (var t = 0; t < formFields2.length; t++)
{
var formID[t] = document.getElementById(t);
if (formFields2[t].className == 'calculation' && formFields2[t].type == 'radio')
{
if (formFields2[t].value)
{
formPrices2[x] = formFields2[t].value;
x++;
}
}
}

for (var r = 0; r < formID; r++)
{
for (var y = 0; y < formTypes2.length; y++)
{
var calc2 = (formPrices2[y] - formPrices2[r]);
var overcalc2 = eval(calc2);
formID[y].innerHTML = overcalc2;
}
}
}
<input onClick="calculate(); np();" name="price1" type="radio" ptype="+" value="100" class="calculation"> Option 1 [+<span id="1">100</span>] <br />

this does absolutely nothing. I was trying to have it take the radio you click -+ other prices to make the new price, but no luck, I'm sure this is off key as well and not close to what it is supposed to be..

scriptmasterdel

3:15 am on Mar 16, 2007 (gmt 0)

10+ Year Member



This wouldn't be so difficult if you used a input field instead of a span, remember CSS is great and it could give the illusion of a standard element.

The reason it is easier is because as far as i know there is no way of getting the default / original value of the span once you have set the value, but with an input field you can use "defaultValue" and get the original value with ease.

If you still want to use a span then this could be done by caching the default value in javascript and then setting the value once the items have changed.

Let me know what you think and i will adapt on my previous post.

Thanks for being patient with me.

Del

firemaster

6:13 pm on Mar 16, 2007 (gmt 0)

10+ Year Member



I'm not really worried if it is an input or not, I think it would look better as a span, but like you said with the right styles it could be hidden well. Anyway that would make it work is fine with me.
thanks a lot,
-Mike

scriptmasterdel

12:36 am on Mar 18, 2007 (gmt 0)

10+ Year Member



I have stickied the code for this item as i feel it is too long to be posted on this forum, per T.O.S.

If you have any problems, please let me know.

Thank you,

Del

firemaster

9:03 am on Mar 18, 2007 (gmt 0)

10+ Year Member



Thanks a lot, I think I can manage from that... Hope I can get this language down as well as you have in the future.
-Mike