OK I have a form that I was asked if I could help with. This needs to be calculated by checking boxes. You can see a sample at

www.derolds.com/calc20.php

I have tried this several different ways. just cant seam to get it. This is for kitchen cabinets. So first box enter amount. How many cabinets do you need enter amount. Then check boxes to select mods. wood, Laminate, and so on all boxes have a pre defined price. You get the idea. And he needs about 20 boxes [ATTACH=CONFIG]4997[/ATTACH]

<?php
if (count($_POST) > 0 && isset($_POST["calc20TextArea1"])){
        $sum = (floatval($_POST["editbox1"])* floatval($_POST["checkbox1"]))+ floatval($_POST["checkbox2"])+ floatval($_POST["checkbox3"])+ floatval($_POST["checkbox4"])+ floatval($_POST["checkbox5"])+ floatval($_POST["checkbox6"])+ floatval($_POST["checkbox7"])+ floatval($_POST["checkbox8"])+ floatval($_POST["checkbox9"])+ floatval($_POST["checkbox10"])+ floatval($_POST["checkbox11"]);
     echo "Total is $ $sum";
}
?>

    You need to explain this further. What do you need to accomplish? What did you try? What are the problems you encountered? Be specific and detailed.

    Generally speaking, this is simply arithmetic, and you need to figure out how to format it as an algebraic expression. Unfortunately, you have to do most of the work, because none of us know what your equation is supposed to look like.

    Some immediate questions:

    1) Your variable [font=monospace]$sum[/font] is calculated, in part, using multiplication. Is this intentional?

    2) You are trying to use many POST values, but you don't check that any of them exist beforehand. (This is especially important when some of the form fields are checkboxes - if they're not checked, they won't exist. I would also highly recommend using more descriptive field names (e.g., "laminate" instead of "checkbox5"), to reduce possible confusion and mistakes in handling them.)

    3) Why [man]floatval[/man]? PHP converts between types automatically when necessary.

    4) Your code implies that the values for you equation are contained in the html form, and are passed to your script. If so, your customers could manipulate pricing at will by changing the values in each field before submitting the form. The form should simply be a way to select options, with the figures determined server-side.

      Ok this is just a simple on page calculator. First box is where they can enter value of how many cabinets they have. Then there is check boxes for
      add-ons. The check boxes are predefined with hidden values. Check box one is $75, Box two is $250, Box 3 is $180 and so on. Total Of all Boxes that are checked times the amount of cabinets and the total is posted on the page. This is just an on page estimate. I no I have my times in the wrong place. I think it should go at the end. Something like this. Lets say you have 10 options but you only want 4 options and 10 cabinets.

      0+0+0+250+0+180+0+0+75+50*10=5550. I can get the first value times the check box to work when I select more boxes it just adds them. When I move times to the last position in the code. I just get an error. I up loaded a picture to give an idea of what he needs. Here is a test page www.derolds.com/calc20.php .

        Ok what I have. I can add three check boxes together . When I add a fourth does not add it in. And when I try to multiply the first box by the checkboxes it only works with the first box

          chip;11037749 wrote:

          &#8230; Then there is check boxes for add-ons. The check boxes are predefined with hidden values

          This is what I was getting at: go to your test page and right click, then click "View Source". There's the values. They are not "hidden" at all. They can be changed by anyone with the motivation to do so, and if you don't do any double-checking on the server, you won't ever realize it. If you're using this form for any serious purpose -to generate a quote, for example- then this would be a big problem: someone gets free cabinets.

          chip;11037749 wrote:

          0+0+0+250+0+180+0+0+75+50*10=5550

          No, the correct answer is 1,005.

          chip;11037751 wrote:

          Ok what I have. I can add three check boxes together . When I add a fourth does not add it in. And when I try to multiply the first box by the checkboxes it only works with the first box

          Again, when a checkbox is not checked, it does not exist. You need to check if each index exists before you try to use it. For example:

          <?php
          
          $checkbox1 = isset( $_POST["checkbox1"] )? 75:  0;
          $checkbox2 = isset( $_POST["checkbox2"] )? 250: 0;
          $checkbox3 = isset( $_POST["checkbox3"] )? 180: 0;
          // and so on
          
          $sum = $checkbox1 + $checkbox2 + $checkbox3;

          You can use [font=monospace][man]var_dump/man[/font] to check what values are actually being sent to the script from the form.

            Write a Reply...