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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀