为什么id不显示在$ _POST数组中?

时间:2016-12-16 15:11:09

标签: php

我的代码是:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>SomuFinance - Personal Finance Manager</title>
    <link rel="stylesheet" type="text/css" href="indexStyle.css">
    <script src="scripts/jquery-3.1.0.min.js"></script>
    <style type="text/css">
        #addItemContainer {
            background-color: rgba(204,207,232,1);
        }
    </style>
    <script type="text/javascript">
        var flag=0;
    </script>
</head>
<body>
    <form id="list" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <div id="container">
            <input type="submit" class="button" name="edit" id="edit" value="Edit" />
            <input type="hidden" id="action" name="action">
            <table id="listDB">
                <tr>
                    <th>Select</th>
                    <th>ID</th>
                    <th>Category ID</th>
                    <th>Shop</th>
                    <th>Item</th>
                    <th>Quantity</th>
                    <th>Unit</th>
                    <th>Price Based On</th>
                    <th>MRP</th>
                    <th>Seller's Price</th>
                    <th>Last Updated On</th>
                </tr>
                <?php
                    $dbc =  mysqli_connect('localhost','root','atlantis2016','itemDB')
                                or die("Error Connecting to Database");

                    $query1 = "SELECT DISTINCT category FROM grocery";
                    $result1 = mysqli_query($dbc, $query1)
                                or die("Error Querying Database");

                    while($row = mysqli_fetch_array($result1))
                    {
                        $category = $row['category'];
                        $query2 = "SELECT * FROM grocery WHERE category='$category' ORDER BY item ASC";
                        $result2 = mysqli_query($dbc, $query2)
                                or die("Error Querying Database");

                        echo '<tr>';
                            echo '<td class="catHead" colspan=11>'.$category.'</td>';
                        echo '</tr>';
                        $catCount=1;

                        while($inRow = mysqli_fetch_array($result2))
                        {
                            $id = $inRow['id'];
                            $shop = $inRow['shop'];
                            $item = $inRow['item'];
                            $qnty = $inRow['quantity'];
                            $unit = $inRow['unit'];
                            $price_based_on = $inRow['price_based_on'];
                            $mrp = $inRow['MRP'];
                            $sellers_price = $inRow['sellers_price'];
                            $last_updated_on = $inRow['last_updated_on'];

                            echo '<tr>';
                                echo '<td><input type="checkbox" id="selected" value="' . $id . '" name="selected[]" /></td>';
                                echo '<td>'.$id.'</td>';
                                echo '<td>'.$catCount.'</td>';
                                echo '<td>'.$shop.'</td>';
                                echo '<td class="leftAligned">'.$item.'</td>';
                                echo '<td>'.$qnty.'</td>';
                                echo '<td>'.$unit.'</td>';
                                echo '<td>'.$price_based_on.'</td>';
                                echo '<td class="pri">₹'.$mrp.'</td>';
                                echo '<td class="pri">₹'.$sellers_price.'</td>';
                                echo '<td>'.$last_updated_on.'</td>';
                            echo '</tr>';

                            $catCount++;
                        }
                    }
                ?>
            </table>
        </div>
    </form>

    <div class="dialogBG">
        <div id="addItemContainer" class="dialog">
            <div class="closeDialog"></div>
            <h1>Edit Item</h1>
            <form id="data" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
                <?php
                    if(isset($_POST['action']))
                    {
                        if($_POST['action']=='edit')
                        {
                            echo '<script>flag=1;</script>';
                            foreach ($_POST['selected'] as $edit_id) 
                            {
                                $query = "SELECT * FROM grocery WHERE id = $edit_id";
                                $result = mysqli_query($dbc, $query)
                                            or die('Error querying database.');
                                break;
                            }

                            $inRow = mysqli_fetch_array($result);

                            $id = $inRow['id'];
                            $shop = $inRow['shop'];
                            $category = $inRow['category'];
                            $item = $inRow['item'];
                            $qnty = $inRow['quantity'];
                            $unit = $inRow['unit'];
                            $price_based_on = $inRow['price_based_on'];
                            $mrp = $inRow['MRP'];
                            $sellers_price = $inRow['sellers_price'];
                            $last_updated_on = $inRow['last_updated_on'];

                        }
                    }
                ?>
                <div class="leftAligned">
                    <label for="id">ID : </label>
                    <input type="text" id="id" name="id" value="<?php echo $id; ?>" required>
                    <div class="inp">
                        <label for="id_disp">ID : </label>
                        <input type="text" id="id_disp" name="id_disp" value="<?php echo $id; ?>" required disabled>
                    </div> <br>
                    <div class="inp">
                        <label for="shop">Shop : </label>
                        <input type="text" id="shop" name="shop" value="<?php echo $shop; ?>" required>
                    </div> <br>
                    <div class="inp">
                        <label for="category">Category : </label>
                        <input type="text" id="category" name="category" value="<?php echo $category; ?>" required>
                    </div> <br>
                    <div class="inp">
                        <label for="item">Item : </label>
                        <input type="text" id="item" name="item" value="<?php echo $item; ?>" required>
                    </div> <br>
                    <div class="inp">
                        <label for="qnty">Quantity : </label>
                        <input type="text" id="qnty" name="qnty" value="<?php echo $qnty; ?>" required>
                    </div> <br>
                    <div class="inp">
                        <label for="unit">Unit : </label>
                        <input type="text" id="unit" name="unit" value="<?php echo $unit; ?>" required>
                    </div> <br>
                    <div class="inp">
                        <label for="price_based_on">Price based on : </label>
                        <select name="price_based_on" id="price_based_on">
                            <option value="kilos">Kilos</option>
                            <option value="packet">Packet</option>
                            <option value="bottle">Bottle</option>
                            <option value="box">Box</option>
                            <option value="piece">Piece</option>
                        </select>
                    </div> <br>
                    <div class="inp">
                        <label for="mrp">MRP (₹) : </label>
                        <input type="text" id="mrp" name="mrp" value="<?php echo $mrp; ?>" required>
                    </div> <br>
                    <div class="inp">
                        <label for="sellers_price">Seller's Price (₹) : </label>
                        <input type="text" id="sellers_price" value="<?php echo $sellers_price; ?>" name="sellers_price" required>
                    </div> <br>
                    <div class="inp">
                        <label for="last_updated_on">Last Updated on : </label>
                        <input type="date" id="last_updated_on" name="last_updated_on" value="<?php echo $last_updated_on; ?>" required>
                    </div>
                </div>
                <div class="inp">
                    <input id="insertButton" type="submit" name="insertButton" value="Insert">
                </div>
            </form>
            <div id="message">
                <?php
                    if(isset($_POST['insertButton']))
                    {

                        $id = $_POST['id'];
                        $shop = $_POST['shop'];
                        $category = $_POST['category'];
                        $item = $_POST['item'];
                        $qnty = $_POST['qnty'];
                        $unit = $_POST['unit'];
                        $price_based_on = $_POST['price_based_on'];
                        $mrp = $_POST['mrp'];
                        $sellers_price = $_POST['sellers_price'];
                        $last_updated_on = $_POST['last_updated_on'];
                        $result=null;

                        foreach ($_POST as $key => $value)
                        {
                            echo "<script> alert('$key contains $value'); </script>";
                        }

                        $query = "UPDATE grocery SET shop='$shop', category='$category', item='$item', quantity='$qnty', unit='$unit', price_based_on='$price_based_on', mrp='$mrp', sellers_price='$sellers_price', last_updated_on='$last_updated_on' WHERE id=$id";

                        if(!empty($shop)&&!empty($category)&&!empty($item)&&is_numeric($qnty)&&!empty($unit)&&is_numeric($mrp)&&is_numeric($sellers_price)&&!empty($last_updated_on))
                        {
                            $result = mysqli_query($dbc, $query)
                                                or die(mysqli_error($dbc));
                        }

                        if($result)
                        {
                            echo '<span class="success">Item Edited Successfully!</span>';  
                        }
                        else
                        {
                            echo '<span class="failure">Failed to insert Item.</span>';
                        }
                        //header("Refresh:2");
                    }
                ?>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        $(document).ready(function(){
            $('.button').click(function(event){
                if($(this).val()=="Delete")
                {
                    $("#deleteConfirmDialog").show(200).parent(".dialogBG").fadeIn(200);
                    $("#action").val('confirmDelete');
                }
                else if($(this).val()=="Edit")
                {
                    event.preventDefault();
                    $("#action").val('edit');
                    $("#list").submit();
                }
            });

            if(flag===1)
            {
                console.log("This shouldn't be there if the page reloads!");
                $("#addItemContainer").show(200).parent(".dialogBG").fadeIn(200);
            }

            $(".closeDialog").click(function (e){
                $(this).parent(".dialog").hide('200').parent(".dialogBG").fadeOut('200');
            });
            $(".cancelButton").click(function (e){
                $(this).parent(".dialog").hide('200').parent(".dialogBG").fadeOut('200');
            });
            $("#insertButton").click(function(event) {
                $("#id").attr('disabled', 'false');
            });
        });
    </script>

    <?php
        echo "<pre>" . var_dump($_POST) . "</pre>";
        mysqli_close($dbc);
    ?>
</body>
</html>

接下来,我使用:

警告$ _POST的内容
foreach ($_POST as $key => $value)
{
    echo "<script> alert('$key contains $value'); </script>";
}

它显示除idid_disp以外的所有键值。现在,id_disp被禁用,所以我理解为什么它从$ _POST数组中省略了,但为什么id iteself,一个文本字段被它忽略了?为什么会这样?

0 个答案:

没有答案
相关问题