Wie zou mij kunnen helpen met mijn programma? Ik heb een zoekmachine gebouwd en heb ik hiervoor een associative array gebruikt en dat werkt prima (producten.php). De zoektermen heb ik vastgelegd in $producten["tags"], zie het programma hieronder wat ik heb geschreven.
Echter, wanneer ik "Aantal" invul en op de knop "Koop nu" klik krijg ik deze foutmelding in het bestand "factuur.php": Notice: Undefined variable: product.
Kortom: het lukt mij niet om de varaiabelen c.q. de elementen te posten van producten.php naar factuur.php. Hieronder de codes van beide bestanden.
Groeten,
Steve
producten.php:
<?php
$producten = [
[
"naam" => "Zak appels",
"prijs" => 2,
"tags" => ["appel", "fruit", "eten", "voedsel"],
"voorraad" => 50,
],
[
"naam" => "Rijst",
"prijs" => 1,
"tags" => ["diner", "eten", "voedsel"],
"voorraad" => 3
],
[
"naam" => "Gouden ring",
"prijs" => 500,
"tags" => ["juweel", "duur"],
"voorraad" => 0
],
[
"naam" => "Broccoli",
"prijs" => 1,
"tags" => ["diner", "eten", "voedsel"],
"voorraad" => 12
]
];
if (isset($_POST["zoekopdracht"])) {
$filter = $_POST["zoekopdracht"];
$kandidaat_producten = $producten;
$producten = [];
foreach($kandidaat_producten as $product) {
foreach($product["tags"] as $tag){
if ($tag == $filter) {
array_push($producten, $product); // voeg dat ene product $product aan de lijst $producten
}
}
}
}
?>
<!DOCTYPE HTML>
<html lang="nl">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<meta charset="utf-8">
<title>Producten</title>
<style type="text/css">
section {
border: 1px solid black;
width: 400px;
height: 180px;
background-color: lightblue;
margin:25px;
}
</style>
</head>
<body style="background-color:lightgreen">
<section>
<div>
<h1>Steve's webshop</h1>
<form action="producten.php" method="POST">
<input type="text" name="zoekopdracht">
<input type="submit" value="search">
</form>
</div>
<div>
<p> Koop hier je producten</p>
</div>
</section>
<?php
foreach($producten as $product) {
?>
<section>
<div>
<h2>
<?php
echo $product["naam"];
?>
</h2>
<small>Voorraad:
<?php
$voorraad = $product["voorraad"];
if($voorraad == 0) {
echo "leeg";
} elseif ($voorraad <= 10) {
echo "bijna op!";
} else {
echo "genoeg";
}
?>
</small>
</div>
<div>
<h2>
<?php
echo $product["prijs"] . " euro";
?>
</h2>
<form action="factuur.php " method="post">
Aantal <input type="text" name="aantal" size="5">
<?php
if ($product["voorraad"] > 0) {
echo "<button>Koop nu</button>";
} else {
echo "<button disabled>Niet beschikbaar</button>";
}
?>
</form>
</div>
</section>
<?php
}
?>
</body>
</html>
factuur.php:
<!DOCTYPE html>
<html>
<head>
<title>Factuur</title>
</head>
<body style="background-color:pink">
<h1>FACTUUR</h1>
<?php
$aantal = $_POST["aantal"];
if (($product["naam"] == "Zak appels")) {
echo "Zak appels!";
$tebetalen = $aantal * 2;
echo "Te betalen " . $tebetalen;
}
if (($product["naam"] == "Rijst")) {
echo "Rijst!";
$tebetalen = $aantal * 1;
echo "Te betalen " . $tebetalen;
}
if (($product["naam"] == "Broccoli")) {
echo "Broccoli!";
$tebetalen = $aantal * 1;
echo "Te betalen " . $tebetalen;
}
?>
</body>
</html>