Forum Moderators: coopster

Message Too Old, No Replies

check boxes with different values

tricky stuff here

         

amdorsey

10:18 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



Heres the scenerio.

I have 4 check boxes (r1, r2, r3, r4)

$r1 = A
$r2 = B
$r3 = C
$r4 = NY

I then have a hidden field $region (to collect values to be entered into database)

<input name="region" type="hidden" value="<?php echo ("$r1 $r2 $r3 $r4");?>">

So $region is entered into the database. easy.

THAT WORKS FINE. The tricky part.

I have a second set of values that needs to be calculated at the same time.

$r1 also = 167
$r2 also = 61
$r3 also = 24
$r4 also = 12

I then have another hidden field called $quanity
$quanity = ("$r1 + $r2 + $r3 + $r4");
which returns the calculated value, so quanity = 86, or whatever the sum of the checkbox values is.

Lots of info, confusing?
Masters go to Work!

Thanks for any help at all!

ergophobe

3:32 am on Dec 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It seems like you have it in hand. What part are you getting hung up on?

BTW, if you want the sum you want something like

$quantity = $r1 + $r2 + $r3 + $r4;

amdorsey

5:02 am on Dec 7, 2004 (gmt 0)

10+ Year Member



The problem is 1 checkbox needs to retain 2 values (ex: $checkbox = A and 10, $checkbox = a, $checkbox = 10) Use Arrays, maybe?

$checkbox = array("A", "10");

But how do I pull a value out of the array?

DaButcher

12:23 pm on Dec 7, 2004 (gmt 0)

10+ Year Member



ok, let's see if I get this right:
you have checkboxes with values:
$r1 = A
$r2 = B
$r3 = C
$r4 = NY

Then the values should have values:
$r1 also = 167
$r2 also = 61
$r3 also = 24
$r4 also = 12

You could do:
$r['A'] = 167;
$r['B'] = 61;
$r['C'] = 24;
$r['NY'] = 12;

btw. do you have the integer values for A, B, C and NY in a database? If so, you can do the calculations in a sql query!

amdorsey

2:03 pm on Dec 7, 2004 (gmt 0)

10+ Year Member



How So? Any Examples?

leifans

2:41 pm on Dec 7, 2004 (gmt 0)

10+ Year Member



I think ergophobe got the point :)

ergophobe

1:58 am on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think I see what you're after. Something like this?

<form method="post" action="junk.php">
<p><input type="checkbox" name="fields[A]" value="123">Region A</p>
<p><input type="checkbox" name="fields[B]" value="345">Region B</p>
<p><input type="checkbox" name="fields[C]" value="678">Region C</p>
<p><input type="checkbox" name="fields[NY]" value="910">Region NY</p>
<p><input type="submit" name="sub1" value="Select Region(s)"></p>
</form>

<?

if(isset($_POST['fields']))
{
$content = "";
foreach($_POST['fields'] as $key => $val)
{
$content .= "\n<li>Region $key: $val</li>";
}
echo "<ul>" . $content . "</ul><pre>\n\n\n";
print_r($_POST);
echo "</pre>";
}

?>

amdorsey

3:40 am on Dec 8, 2004 (gmt 0)

10+ Year Member



Your way is much more efficient, but this is what I came up with this morning (est).

The form has the following check boxes...

<input name="r1" type="checkbox" value="A">
<input name="r2" type="checkbox" value="B">
<input name="r3" type="checkbox" value="C">
<input name="r4" type="checkbox" value="NY">

Then the page that processes the form data has...

<?php

if ($r1 == "A") {
$rNumber1 = "167";
} else {
$rNumber1 = "0";
}
if ($r2 == "B") {
$rNumber2 = "61";
} else {
$rNumber2 = "0";
}
if ($r3 == "C") {
$rNumber3 = "24";
} else {
$rNumber3 = "0";
}
if ($r4 == "NY") {
$rNumber4 = "12";
} else {
$rNumber4 = "0";
}

$finalNumber = ('$rNumber1' + '$rNumber2' + '$rNumber3' + '$rNumber4');

?>

SO $region is assigned values $r1, $r2, $r3, $r4

And the numerical calculation is $finalNumber

Thanks for all your help, though.