The code between the asterisk lines checks for an empty field, and displays an error message above the form field.
Similar code that checks if an ID already exists in the cart is just below it. The difference between the two is that the code in question does not function as intended. It interprets an error, it sees an empty field was submitted, and triggers its error message.
The problem is that it also submits the empty string into the cart. I cannot figure out why.
I included all the code, if anyone wanted to test it. I hope someone sees what I cannot see. Thank you.
<?php
session_start();
// If no cart exists, create $_SESSION['cart'] array
if(!isset($_SESSION['cart'])){
$_SESSION['cart'] = array();
}
// Add item to array
if(isset($_POST['action']) && $_POST['action'] === 'add'){
/***************************************************/
// Check if input is empty / null
if(empty($_POST['id'])){
$error = '*Please enter an ID number.';
}
/***************************************************/
// Check if form data exists
if(isset($_POST['id'])){
$id = $_POST['id'];
}
// Check if ID already in cart
if(isset($_SESSION['cart'][$id])){
$error = '*Item already in cart.';
}
// Add new ID to array (hard-code some data for test file)
$newitem = array(
'id' => $id,
'part_number' => '369A7170-11',
'quantity' => '1'
);
// Add new data to cart with ID as key
$_SESSION['cart'][$id] = $newitem;
}
// Add item to array
if(isset($_POST['action']) && $_POST['action'] === 'update'){
// Check if input is empty / null
if(empty($_POST['id'])){
$error = '*Please select item and quantity.';
include 'error.html.php';
exit();
}
// Check if form data exists
if(isset($_POST['id']) && isset($_POST['quantity'])){
$id = $_POST['id'];
$quantity = (int)$_POST['quantity'];
}
$_SESSION['cart'][$id]['quantity'] = $quantity;
}
// Remove item from array
if(isset($_POST['action']) && $_POST['action'] === 'remove'){
// Check if form data exists
if(isset($_POST['id'])){
$id = $_POST['id'];
}
unset($_SESSION['cart'][$id]);
}
// Empty cart
if(isset($_POST['action']) && $_POST['action'] === 'empty'){
unset($_SESSION['cart']);
}
// Initialize $count variable; get item count
$count = '';
if(isset($_SESSION['cart'])) $count = count($_SESSION['cart']);
// Display results
if(isset($_SESSION['cart'])){
$show_cart = var_dump($_SESSION['cart']);
echo $show_cart;
}
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Cart</title>
</head>
<body>
<h3>Cart Management</h3>
<p style="color:#ff0000;"><?php if(isset($error)) echo htmlentities($error, ENT_QUOTES); ?></p>
<p>Items in cart: <?php if(isset($count) && $count > 0)echo htmlentities($count, ENT_QUOTES); else echo 'none'; ?></p>
<form action="" method="post">
<label for="id"> </label>
<input type="text" name="id" id="id" placeholder="Enter ID number" autofocus>
<input type="hidden" name="action" value="add">
<input type="submit" value="Add">
</form>
<form action="" method="post">
<label for="id"> </label>
<select name="id" id="id">
<option value="">Select ID</option>
<?php foreach($_SESSION['cart'] as $key => $item): ?>
<option value="<?php echo htmlentities($item['id'], ENT_QUOTES); ?>"><?php echo htmlentities($item['id'], ENT_QUOTES); ?></option>
<?php endforeach; ?>
</select>
<input type="hidden" name="action" value="remove">
<input type="submit" value="Remove">
</form>
<form action="" method="post">
<label for="id"> </label>
<select name="id" id="id">
<option value="">Select ID</option>
<?php foreach($_SESSION['cart'] as $key => $item): ?>
<option value="<?php echo htmlentities($item['id'], ENT_QUOTES); ?>"><?php echo htmlentities($item['id'], ENT_QUOTES); ?></option>
<?php endforeach; ?>
</select><br>
<label for="quantity"> </label>
<input type="text" name="quantity" id="quantity" size="2">
<input type="hidden" name="action" value="update">
<input type="submit" value="Update quantity">
</form>
<form action="" method="post">
<input type="hidden" name="action" value="empty">
<input onclick="return confirm('Are you sure you want to empty the cart?');" type="submit" value="Empty cart">
</form>
</body>
</html>