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;