PDA

Click to See Complete Forum and Search --> : [RESOLVED] Checkboxes submit


BlackRiver
Jun 24th, 2009, 12:18 PM
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:

<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
$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 =0 ;
$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?

BlackRiver
Jun 24th, 2009, 12:27 PM
OK,I made some modifications to my code,so now it adds values,but why do I get "undefined variable total" msg?

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;

kows
Jun 24th, 2009, 02:23 PM
um, maybe because you haven't defined total?

$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;

BlackRiver
Jun 24th, 2009, 03:57 PM
ummm,yes you're right :)
Thank you!