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条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改