duanraotun1674 2015-09-02 16:24 采纳率: 100%
浏览 38
已采纳

停止提交空字段

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>
  • 写回答

2条回答 默认 最新

  • dongmeng0317 2015-09-02 16:39
    关注

    $_POST['id'] will always be set if it's on your form and it is submitted, regardless if it's empty or not. If you want to check if it's empty, then use

        if(empty($_POST['id'])){
            // give error
        }
        else{
            // do something on success
        }
    

    Alternatively, you could set the input field to be required in the html code. Doing so will make it so the form will not be submitted unless that field has an input.


    Try this:

    if(isset($_POST['action']) && $_POST['action'] === 'add'){
    
    /*******************************************************/    
    // Check if input is empty / null
    if(empty($_POST['id'])){
        $error = '*Please enter an ID number.';
    }
    else{
    /*******************************************************/ 
    
        // 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;  
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿