|
-
Jun 24th, 2009, 12:18 PM
#1
Thread Starter
Hyperactive Member
[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 =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?
-
Jun 24th, 2009, 12:27 PM
#2
Thread Starter
Hyperactive Member
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;
-
Jun 24th, 2009, 02:23 PM
#3
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;
-
Jun 24th, 2009, 03:57 PM
#4
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|