php - Using a checkbox to pull data from database
I want a user to be able to choose a few ingredients via a checkbox and be able to request from its database its nutritional values. Have tried using a case switch function but nothing is working.
This is the checkbox
<?php
require_once 'includes/output.inc.php';
include_once 'header.php';
?>
<h1>Choose your ingredients down-below</h1>
<form action="includes/output.inc.php" method="get">
<div class="basic-select" style="width:100%;">
<!-- High Energy, Low Fiber Sources -->
<h2>High Energy, Low Fiber Sources:</h2>
<label class="container">Corn/Maize Grain<input type="checkbox" name="HiEn[]" class="check"><span class="checkmark"></span></label>
<label class="container">Barley Grain<input type="checkbox" name="HiEn[]" class="check"><span class="checkmark"></span></label>
<label class="container">Sorghum Grain (All Types)<input type="checkbox" name="HiEn[]" class="check"><span class="checkmark"></span></label>
<label class="container">Wheat Grain<input type="checkbox" name="HiEn[]"><span class="checkmark"></span></label>
<label class="container">Rice (Broken Polished)<input type="checkbox" name="HiEn[]" class="check"><span class="checkmark"></span></label>
<label class="container">Cassava Tuber (Fresh)<input type="checkbox" name="HiEn[]" class="check"><span class="checkmark"></span></label>
<label class="container">Sweet Potato Tubers<input type="checkbox" name="HiEn[]" class="check"><span class="checkmark"></span></label>
<label class="container">Bakery Waste (eg Bread)<input type="checkbox" name="HiEn[]" class="check"><span class="checkmark"></span></label>
<label class="container">Sugar Beet Molasses<input type="checkbox" name="HiEn[]" class="check"><span class="checkmark"></span></label>
<label class="container">Sugarcane Molasses<input type="checkbox" name="HiEn[]" class="check"><span class="checkmark"></span></label>
<button value="submit">Submit</button>
</form>
includes/output.inc.php:
$dbc = mysqli_connect("localhost", "user-name", "password", "db-name");
switch ($_POST['feedcalculator']) {
case 'Corn/Maize Grain':
$query = 'SELECT * FROM `ingredients_index` ORDER BY `ingredients_index`.`ingredients_id 1` ASC;';
break;
case 'Barley Grain':
$query = 'SELECT * FROM `ingredients_index` ORDER BY `ingredients_index`.`ingredients_id 2` ASC;';
break;
case 'Sorghum Grain (All Types)':
$query = 'SELECT * FROM `ingredients_index` ORDER BY `ingredients_index`.`ingredients_id 3` ASC;';
break;
case 'Wheat Grain':
$query = 'SELECT * FROM `ingredients_index` ORDER BY `ingredients_index`.`ingredients_id 14` ASC;';
break;
default:
exit('unexpected input');
break;
}
Nothing happened. I can't seem to find a way to connect them. I did try using this afterwards and I keep getting - Undefined array key "HiEn"
The code was changed to this:
<form action="includes/output.inc.php" method="get">
<h2>High Energy, Low Fiber Sources:</h2>
<label class="container">Corn/Maize Grain<input type="checkbox" name="HiEn[]" class="check" value="Corn/Maize Grain"><span class="checkmark"></span></label>
<label class="container">Barley Grain<input type="checkbox" name="HiEn[]" class="check" value="Barley Grain"><span class="checkmark"></span></label>
<label class="container">Sorghum Grain (All Types)<input type="checkbox" name="HiEn[]" class="check" value="Sorghum Grain (All Types)"><span class="checkmark"></span></label>
<button value="submit">Submit</button>
</form>
switch ($_POST['HiEn']) {
case 'Corn/Maize Grain':
$query = 'SELECT * FROM `ingredients_index` ORDER BY `ingredients_index`.`ingredients_id 1` ASC;';
break;
case 'Barley Grain':
$query = 'SELECT * FROM `ingredients_index` ORDER BY `ingredients_index`.`ingredients_id 2` ASC;';
break;
case 'Sorghum Grain (All Types)':
$query = 'SELECT * FROM `ingredients_index` ORDER BY `ingredients_index`.`ingredients_id 3` ASC;';
echo "Mom";
break;
case 'Wheat Grain':
$query = 'SELECT * FROM `ingredients_index` ORDER BY `ingredients_index`.`ingredients_id 14` ASC;';
break;
default:
exit('unexpected input');
break;
}
Answer
Solution:
In your code you are using $_Post['feedcalculator']
but there is no checkbox or any other control with this name under your form tag.
Secondly none of your checkboxes are given values you they cannot posted their respective values. Please make below change to code -
Change this -
$_POST['feedcalculator']
to this -
$_POST['HiEn']
Html Change -
Change this -
<label class="container">Corn/Maize Grain<input type="checkbox" name="HiEn[]" class="check"><span class="checkmark"></span></label>
to this -
<label class="container">Corn/Maize Grain<input type="checkbox" name="HiEn[]" class="check" value="Corn/Maize Grain"><span class="checkmark"></span></label>
Source