Need to stop empty field from submitting and display error message on the same page.
I would appreciate any assistance. Here's the entire shopping cart code. Part in question between lines of asterisks. Thanks!
// 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(($_POST['id']) == ''){
$error = '*Please enter an ID number.';
include 'error.html.php';
exit();
}
/*******************************************************/
// 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;
}
// 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;
}
/************** TEST ******************************/
//$_SESSION['quantity'][$id] = $quantity;
if(isset($_SESSION['cart'])) echo 'ID: ' . $_SESSION['cart'][$_POST['id']]['id'] . '<br>';
if(isset($_SESSION['cart'])) echo 'Qty: ' . $_SESSION['cart'][$_POST['id']]['quantity'] . '<br>';
$new_quantity = 5;
if(isset($_SESSION['cart'])) $_SESSION['cart'][$_POST['id']]['quantity'] = $new_quantity;
if(isset($_SESSION['cart'])) echo 'Updated Qty: ' . $_SESSION['cart'][$_POST['id']]['quantity'];
/************** // END TEST ***********************/
?><!DOCTYPE hml>
<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">ID:</label>
<input type="text" name="id" id="id" autofocus>
<input type="hidden" name="action" value="add">
<input type="submit" value="Add">
</form>
<form action="" method="post">
<label for="id">By 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">
<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>