0

I have a form with nine checkboxes in it, each passing a value of 1 to a submit page that updates SQL based on whether a checkbox was checked.

if (isset($_POST["cinn"])) {
    $cinn = $_POST["cinn"];
} else {
    $cinn = 0;
} if (isset($_POST["suga"])) {
    $suga = $_POST["suga"];
} else {
    $suga = 0;
} if (isset($_POST["bana"])) {
    $bana = $_POST["bana"];
} else {
    $bana = 0;
} if (isset($_POST["toba"])) {
    $toba = $_POST["toba"];
} else {
    $toba = 0;
} if (isset($_POST["spic"])) {
    $spic = $_POST["spic"];
} else {
    $spic = 0;
} if (isset($_POST["mint"])) {
    $mint = $_POST["mint"];
} else {
    $mint = 0;
} if (isset($_POST["sour"])) {
    $sour = $_POST["sour"];
} else {
    $sour = 0;
} if (isset($_POST["nutt"])) {
    $nutt = $_POST["nutt"];
} else {
    $nutt = 0;
} if (isset($_POST["glas"])) {
    $glas = $_POST["glas"];
} else {
    $glas = 0;
}

You can already tell how bad this looks. Because I want to insert either 0 or 1 into SQL, from my understanding I need to do a check if the $_POST name of each checkbox is valid (checked), and set a variable from that. It would be real nice to use an array here, but since each checkbox has a different name, I don't think I can.

I would love something like the below instead of the garbage I currently have:

//checkbox sends either a 1 if checked or 0 if unchecked via $_POST
//no if/else complex is needed; individual variables aren't even needed
//can just reference $_POST instead of initializing

Ternary operators would reduce the amount of lines, but this whole boolean check I imagine is unnecessary.

$cinn = isset($_POST["cinn"]) ? $_POST["cinn"] : 0;
$suga = isset($_POST["suga"]) ? $_POST["suga"] : 0;
$bana = isset($_POST["bana"]) ? $_POST["bana"] : 0;
$toba = isset($_POST["toba"]) ? $_POST["toba"] : 0;
$spic = isset($_POST["spic"]) ? $_POST["spic"] : 0;
$mint = isset($_POST["mint"]) ? $_POST["mint"] : 0;
$sour = isset($_POST["sour"]) ? $_POST["sour"] : 0;
$nutt = isset($_POST["nutt"]) ? $_POST["nutt"] : 0;
$glas = isset($_POST["glas"]) ? $_POST["glas"] : 0;
gator
  • 3,465
  • 8
  • 36
  • 76
  • 1
    Take a look [at this answer](http://stackoverflow.com/a/4997271/643850) sounds like that's what you want to do. – d3c0y Feb 05 '14 at 08:20

2 Answers2

4

First, you can in fact use checkboxes with different names and an array. The trick is knowing how to setup your form checkboxes to allow you to parse all the checkboxes as a single array.

<form action="" method="post">
<input type="checkbox" name="boxes[]" value="cinn">Cinn<br />
<input type="checkbox" name="boxes[]" value="suga">Suga<br />
<input type="checkbox" name="boxes[]" value="bana">Bana<br />
<input type="checkbox" name="boxes[]" value="spic">Spic<br />
<input type="checkbox" name="boxes[]" value="toba">Toba<br />
<input type="checkbox" name="boxes[]" value="sour">Sour<br />
<input type="checkbox" name="boxes[]" value="nutt">Nutt<br />

<input type="submit" name="Submit"/>
</form>
<?php
if ($_POST) {
    print '<pre>'; print_r($_POST); print '</pre>';
}
?>

Please notice how the <input type="checkbox"> element has the name of name="boxes[]" and pay close attention to the open/close square brackets after the word boxes[]. This will allow multiple checkbox values to be passed through the form within one array, instead of each as their own key/value pair. This is a critical difference and it should not be overlooked.

You should then be able to process these checkboxes in a number of more convenient ways. Like @hindmost's suggestion, you could use a foreach loop to iterate through a list of names of checkboxes you know to look out for, then use variable-variables to set new variable names and values dynamically.

That would work, although I think the only reason you would have needed that solution is if you couldn't use the checkboxes as an array themselves. Since with the solution I've provided gives you more flexibility with your data, I don't recommend using variable-variables unless absolutely necessary, which would be very rare. I highly recommend searching for a simpler solution now that you can weild all your checkbox data in a single array.

Armin
  • 1,736
  • 4
  • 19
  • 35
  • 1
    This is a good answer, just try adding a foreach loop like foreach($_POST['boxes'] as $box) and each $box will contain the value from the input when used within the loop. – d3c0y Feb 05 '14 at 08:26
  • 2
    Yes, that is correct, @d3c0y. Although this is exactly the same thing >>> print '
    '; print_r($_POST); print '
    '; <<< which is already in my code above.
    – Armin Feb 05 '14 at 08:27
2

Try this code:

$a_fields = array('cinn', 'suga', 'bana', ...);
foreach ($a_fields as $name) {
    $$name = isset($_POST[$name])? $_POST[$name] : 0;
}
hindmost
  • 7,125
  • 3
  • 27
  • 39
  • Interesting. I had no idea about variable variables. Thanks! – gator Feb 05 '14 at 08:10
  • Variable variables are definitely a solution, but you could also write them into an array. Arrays are usually more usable than variable variables (e.g. being able to count, loop over them, etc). – Berry Langerak Feb 05 '14 at 08:20
  • 1
    Variable variables are bad. Avoid them. Whenever you feel the need to have the name of a variable to be dynamic, use an array. The array's key is the "name" of your variable, and you do not get access accidentially to some values from something else - or even worse, overwrite a global variable. – Sven Feb 05 '14 at 08:21
  • @Sven I hope your comment does not intended to me. I just offered a solution within the terms of the problem – hindmost Feb 05 '14 at 08:25
  • 1
    Offering solutions without talking about the drawbacks connected is not very helpful IMO. If you know variable variables are not the best element, at least mention it somewhere. Offer alternatives. – Sven Feb 05 '14 at 08:51