Jump to content

Form Calculation


pauldonnelly23

Recommended Posts

Hi,

I hope you can help.

 

I have a form with about 15 checkboxes. I am using the following code to count the checkboxes and multiply it by £200.

 

My problem is two of the 15 checkboxes have different prices and I dont know how to add them to the price.

 

Current Code:

$checkboxCount = isset($_POST['course1']) ? count($_POST['course1']) : 0;
$checkboxCount1 = isset($_POST['course2']) ? count($_POST['course2']) : 0;
$checkboxCount2 = isset($_POST['course3']) ? count($_POST['course3']) : 0;
$checkboxCount3 = isset($_POST['course4']) ? count($_POST['course4']) : 0;
$countchecked = $checkboxCount+$checkboxCount1+$checkboxCount2+$checkboxCount3;
$total = $countchecked*200;

 

I hope you understand what I need and I hope you can help.

 

Kind regards,

Paul

 

Link to comment
Share on other sites

$checkboxCount = isset($_POST['course1']) ? count($_POST['course1']) : 0;
$checkboxCount1 = isset($_POST['course2']) ? count($_POST['course2']) : 0;
$checkboxCount2 = isset($_POST['course3']) ? count($_POST['course3']) : 0;
$checkboxCount3 = isset($_POST['course4']) ? count($_POST['course4']) : 0;
$countchecked = $checkboxCount+$checkboxCount1+$checkboxCount2+$checkboxCount3;
$checkBoxCountExtra = $extracheckone * $price + $extrachecktwo * $pricetwo;
$total = $countchecked*200 + $checkBoxCountExtra;

 

That should do it

Link to comment
Share on other sites

Thanks for your prompt reply Areoswat.

The checkboxes are in an array. e.g. course1, course2, course3 and course4.

Will your code work for this? How do I specify a specific checkbox from an array?

 

I guess i'm sort of confused. course1, course2, course3 and course4 are sets of checkboxes? That would explain why you are counting them

Link to comment
Share on other sites

If what I said was the case then after you do your calculations you need to check to see if the value of each of the checkboxes that have different prices are a part of whichever array they belong to. If they are then you need to decrement that count by 1 and add the price of whatever it is to the total. Do that for each of the two.

Link to comment
Share on other sites

It looks to me that "most" of the checkboxes represent items that each cost £200. That would explain why you are counting up all the checked items and multiplying by 200. Are you even using the value of the checkboxes? If no, then use the value for the prices of the checkboxes and then sum up all the values to get the total price.

 

If you are using the values of the checkboxes for some other purpose, then I have to assume you have a way of knowing what the cost of each item is that you can tie back to the actual value (e.g. title?). You would want to determine the price individually by looking up each checked value.

Link to comment
Share on other sites

If they are then you need to decrement that count by 1 and add the price of whatever it is to the total. Do that for each of the two.

 

Aeroswat, how would i go about this?

 

The ids of the two checkboxes in the array are:

pap4cbcs (which has a price of 270)

pap4fau (which has a price of 70)

 

The code im using to count at the minute is:


$checkboxCount = isset($_POST['course1']) ? count($_POST['course1']) : 0;
$checkboxCount1 = isset($_POST['course2']) ? count($_POST['course2']) : 0;
$checkboxCount2 = isset($_POST['course3']) ? count($_POST['course3']) : 0;
$checkboxCount3 = isset($_POST['course4']) ? count($_POST['course4']) : 0;
$countchecked = $checkboxCount+$checkboxCount1+$checkboxCount2+$checkboxCount3;
$total = $countchecked*200;

 

If you can help, id be very grateful.

 

kind regards,

Paul

Link to comment
Share on other sites

You cannot determine the ID of a checkbox in the POST data - only the Values. What are the values of the checkboxes? It would be very helpful if you showed the form that creates the checkboxes on the form.

 

As I stated previously, you should be calculating the cost more explicitly instead of just counting the checkboxes.

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.