Results 1 to 4 of 4

Thread: [RESOLVED] Checkboxes submit

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Resolved [RESOLVED] Checkboxes submit

    I have 4 checkboxes in my form.Each one has a different value.When a user selects,let's say 2 of them and submits the form I need to output the sum total of the selected checkboxes.

    This is how my form looks like:
    HTML Code:
    <form name="form1" method="post" action="unos.php" 
     <input type="checkbox" name="cb1" value="355">HP DESKJET D2660
     <input type="checkbox" name="cb2" value="300">HP DESKJET D1560
     <input type="checkbox" name="cb3" value="420">HP DESKJET 6940
     <input type="checkbox" name="cb4" value="450">HP OFFICEJET 6000
    
    <input name="ime" type="text" id="Text1" style="position: absolute" />
    <input name="prezime" type="text" id="Text2" style="position: absolute" />
     <input name="adresa" type="text" id="Text3" style="position: absolute" />
    <input type="submit" name="Submit" value="OK" style="position: absolute; top: 812px; left: 375px;">
    And this is my unos.php:

    PHP Code:
    <?php
    $konekcija 
    mysql_connect($host,$user,$pass)
    or die (
    'Povezivanje sa serverom nije uspjelo!');

    mysql_select_db($baza) or die ('Odabir baze nije uspio!');

    for(
    $a=1;$a<4;$a++){

    if(isset(
    $_POST['cb'.$a]))
    {
    $total =;
    $amount $_POST['cb'.$a];
    $total += $amount;
    }
    }
    echo 
    $total;



    ?>
    So my basic idea is to loop through all checkboxes,see wich ones are set and add their values to $total.

    But I get some strange behaviour,like if I check first and second it will display "355300".That means it's not adding values.If I check second and fourth then only the second value is displayed?
    What am I doing wrong?

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Re: Checkboxes submit

    OK,I made some modifications to my code,so now it adds values,but why do I get "undefined variable total" msg?

    PHP Code:
    for($a=1;$a<5;$a++){

    if(isset(
    $_POST['cb'.$a]))
    {
    $amount $_POST['cb'.$a];
    $total += $amount//<------ error referes to this line of code
    }
    }
    echo 
    $total

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Checkboxes submit

    um, maybe because you haven't defined total?

    PHP Code:
    $total 0//added this.


    for($a=1;$a<5;$a++){

    if(isset(
    $_POST['cb'.$a]))
    {
    $amount $_POST['cb'.$a];
    $total += $amount//<------ error referes to this line of code
    }
    }
    echo 
    $total

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Re: Checkboxes submit

    ummm,yes you're right
    Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width