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;
}
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>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.