Jump to content

Form calculations


Nat

Recommended Posts

Hi,

 

I've designed a form that contains some radio buttons and checkboxes. For instance,

 

 

<p>Size:<br />

<input type="radio" id="sml" name="size" value="Small" /></label for="sml">Small</label><br />

<input type="radio" id="med" name="size" value="Medium" /></label for="med">Medium</label><br />

<input type="radio" id="lge" name="size" value="Large" /></label for="lge">Large</label><br /></p>

 

<p>Colours:<br />

<input type="checkbox" id="green"  name="hat[]" value="Green" /><label for="green">Green</label><br />

<input type="checkbox" id="red"  name="hat[]" value="Red" /><label for="red">Red</label><br />

</p>

 

Does anyone know how I can assign a dollar value to different options. So that if options are chosen it will calculate the price.

 

For instance a small, red hat would equal different price to a large, green hat.

 

Any guidance would be appreciated I can't find anything anywhere on this topic and I'm so lost.

 

 

 

Link to comment
Share on other sites

I assume you have the form linked to another page and that you're using the method=post?

 

If so you can either keep your code this way and you'd need an if clause to check what your value of ech button is.

 

But I'd recommend you first change your code into this:

 

<p>Size:

<input type="radio" id="size" name="size" value="Small" /></label for="sml">Small</label>

<input type="radio" id="size" name="size" value="Medium" /></label for="med">Medium</label>

<input type="radio" id="size" name="size" value="Large" /></label for="lge">Large</label>
</p>

<p>Colours:

<input type="checkbox" id="colour"  name="hat[]" value="Green" /><label for="green">Green</label>

<input type="checkbox" id="colour"  name="hat[]" value="Red" /><label for="red">Red</label>

</p>

 

That way you'll only be able to select one radio button and you can use an if on your target page to select your values

 

for instance

 

<?php
$size = $_GET['size'];
$colour = $_GET['colour'];

if($size == small)
{
$price = 10
}

 

and so on, an other solution would be to assign exact values to your radiobuttons

 

for example value="10"

 

that way you could use $_GET on your target page and use the variables immediatly without running them trough an if clause.

Link to comment
Share on other sites

I agree with Anzeo. I'd say that using an if clause would be the better way though.. leaving the values at small, large and medium etc.

It would make it easier to change it in the future.. instead of having to change the values of all the form elements you'd just need to change it once in each if statement..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.