douluan5738 2015-09-02 20:07
浏览 38
已采纳

代码看到错误,发送错误消息,但将表单数据提交到会话购物车

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">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</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> 
  • 写回答

2条回答 默认 最新

  • dsjmrpym220113739 2015-09-02 20:27
    关注

    So you set $error, and print it and nothing else. And this line (that I think this is the line that adds it to the cart) will be executed anyway:

    $_SESSION['cart'][$id] = $newitem;
    

    You have to check the error before doing that. For example:

    if(!isset($error))
        $_SESSION['cart'][$id] = $newitem;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 hexo+github部署博客
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?