data not posted [closed]

£可爱£侵袭症+ 提交于 2020-04-07 08:44:12

问题


Oke so i have a php form to post data to my database. It went ok before i decided to use collapsible button for each of category. After i add collapsible, it went ok at first, but suddenly the quantity and note not posted to my database. Below are my code. Please help me. I want to know what went wrong Thanks in advance

<body>
<?php include('navbar.php'); ?>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.collapsible {
  background-color: brown;
  color: white;
  cursor: pointer;
  padding: 18px;
  width: 100%;
  border-color: grey;
  text-align: left;
  outline: none;
  font-size: 15px;
}

.active, .collapsible:hover {
  background-color: #555;
}

.content {
  padding: 0 18px;
  display: none;
  overflow: hidden;
  background-color: #f1f1f1;
}
</style>
</head>
<body>

<div class="container">
    <h1 class="page-header text-center">ORDER</h1>
    <form method="POST" action="purchase.php">
    <h3 class="page-header text-center">KARIMATA</h3>
    <button type="button" class="collapsible">PATIN</button>
    <div class="content">
        <table class="table table-striped table-bordered">
            <thead>
                <th class="text-center"><input type="checkbox" id="checkAll"></th>
                <th>Category</th>
                <th>Product Name</th>
                <th>Price</th>
                <th>Quantity</th>
                <th>Note</th>
            </thead>
            <tbody>
                <?php 
                    $sql="select * from product left join category on category.categoryid=product.categoryid where product.categoryid=1";
                    $query=$conn->query($sql);
                    $iterate=0;
                    while($row=$query->fetch_array()){
                        ?>
                        <tr>
                            <td class="text-center"><input type="checkbox" value="<?php echo $row['productid']; ?>||<?php echo $iterate; ?>" name="productid[]" style=""></td>
                            <td><?php echo $row['catname']; ?></td>
                            <td><?php echo $row['productname']; ?></td>
                            <td class="text-right">IDR <?php echo number_format($row['price'], 2); ?></td>
                            <td><input type="text" class="form-control" name="quantity_<?php echo $iterate; ?>"></td>
                            <td><input type="text" class="form-control" name="note_<?php echo $iterate; ?>"></td>
                        </tr>
                        <?php
                        $iterate++;
                    }
                ?>
            </tbody>
        </table>
        </div>

            <button type="button" class="collapsible">GURAME</button>
    <div class="content">
        <table class="table table-striped table-bordered">
            <thead>
                <th class="text-center"><input type="checkbox" id="checkAll"></th>
                <th>Category</th>
                <th>Product Name</th>
                <th>Price</th>
                <th>Quantity</th>
                <th>Note</th>
            </thead>
            <tbody>
                <?php 
                    $sql="select * from product left join category on category.categoryid=product.categoryid where product.categoryid=2";
                    $query=$conn->query($sql);
                    $iterate=0;
                    while($row=$query->fetch_array()){
                        ?>
                        <tr>
                            <td class="text-center"><input type="checkbox" value="<?php echo $row['productid']; ?>||<?php echo $iterate; ?>" name="productid[]" style=""></td>
                            <td><?php echo $row['catname']; ?></td>
                            <td><?php echo $row['productname']; ?></td>
                            <td class="text-right">IDR <?php echo number_format($row['price'], 2); ?></td>
                            <td><input type="text" class="form-control" name="quantity_<?php echo $iterate; ?>"></td>
                            <td><input type="text" class="form-control" name="note_<?php echo $iterate; ?>"></td>
                        </tr>
                        <?php
                        $iterate++;
                    }
                ?>
            </tbody>
        </table>
        </div>

            <button type="button" class="collapsible">AYAM</button>
    <div class="content">
        <table class="table table-striped table-bordered">
            <thead>
                <th class="text-center"><input type="checkbox" id="checkAll"></th>
                <th>Category</th>
                <th>Product Name</th>
                <th>Price</th>
                <th>Quantity</th>
                <th>Note</th>
            </thead>
            <tbody>
                <?php 
                    $sql="select * from product left join category on category.categoryid=product.categoryid where product.categoryid=3";
                    $query=$conn->query($sql);
                    $iterate=0;
                    while($row=$query->fetch_array()){
                        ?>
                        <tr>
                            <td class="text-center"><input type="checkbox" value="<?php echo $row['productid']; ?>||<?php echo $iterate; ?>" name="productid[]" style=""></td>
                            <td><?php echo $row['catname']; ?></td>
                            <td><?php echo $row['productname']; ?></td>
                            <td class="text-right">IDR <?php echo number_format($row['price'], 2); ?></td>
                            <td><input type="text" class="form-control" name="quantity_<?php echo $iterate; ?>"></td>
                            <td><input type="text" class="form-control" name="note_<?php echo $iterate; ?>"></td>
                        </tr>
                        <?php
                        $iterate++;
                    }
                ?>
            </tbody>
        </table>
        </div>
            <button type="button" class="collapsible">DORI</button>
    <div class="content">
        <table class="table table-striped table-bordered">
            <thead>
                <th class="text-center"><input type="checkbox" id="checkAll"></th>
                <th>Category</th>
                <th>Product Name</th>
                <th>Price</th>
                <th>Quantity</th>
                <th>Note</th>
            </thead>
            <tbody>
                <?php 
                    $sql="select * from product left join category on category.categoryid=product.categoryid where product.categoryid=4";
                    $query=$conn->query($sql);
                    $iterate=0;
                    while($row=$query->fetch_array()){
                        ?>
                        <tr>
                            <td class="text-center"><input type="checkbox" value="<?php echo $row['productid']; ?>||<?php echo $iterate; ?>" name="productid[]" style=""></td>
                            <td><?php echo $row['catname']; ?></td>
                            <td><?php echo $row['productname']; ?></td>
                            <td class="text-right">IDR <?php echo number_format($row['price'], 2); ?></td>
                            <td><input type="text" class="form-control" name="quantity_<?php echo $iterate; ?>"></td>
                            <td><input type="text" class="form-control" name="note_<?php echo $iterate; ?>"></td>
                        </tr>
                        <?php
                        $iterate++;
                    }
                ?>
            </tbody>
        </table>
        </div>

            <button type="button" class="collapsible">UDANG</button>
    <div class="content">
        <table class="table table-striped table-bordered">
            <thead>
                <th class="text-center"><input type="checkbox" id="checkAll"></th>
                <th>Category</th>
                <th>Product Name</th>
                <th>Price</th>
                <th>Quantity</th>
                <th>Note</th>
            </thead>
            <tbody>
                <?php 
                    $sql="select * from product left join category on category.categoryid=product.categoryid where product.categoryid=5";
                    $query=$conn->query($sql);
                    $iterate=0;
                    while($row=$query->fetch_array()){
                        ?>
                        <tr>
                            <td class="text-center"><input type="checkbox" value="<?php echo $row['productid']; ?>||<?php echo $iterate; ?>" name="productid[]" style=""></td>
                            <td><?php echo $row['catname']; ?></td>
                            <td><?php echo $row['productname']; ?></td>
                            <td class="text-right">IDR <?php echo number_format($row['price'], 2); ?></td>
                            <td><input type="text" class="form-control" name="quantity_<?php echo $iterate; ?>"></td>
                            <td><input type="text" class="form-control" name="note_<?php echo $iterate; ?>"></td>
                        </tr>
                        <?php
                        $iterate++;
                    }
                ?>
            </tbody>
        </table>
        </div>

            <button type="button" class="collapsible">CUMI</button>
    <div class="content">
        <table class="table table-striped table-bordered">
            <thead>
                <th class="text-center"><input type="checkbox" id="checkAll"></th>
                <th>Category</th>
                <th>Product Name</th>
                <th>Price</th>
                <th>Quantity</th>
                <th>Note</th>
            </thead>
            <tbody>
                <?php 
                    $sql="select * from product left join category on category.categoryid=product.categoryid where product.categoryid=6";
                    $query=$conn->query($sql);
                    $iterate=0;
                    while($row=$query->fetch_array()){
                        ?>
                        <tr>
                            <td class="text-center"><input type="checkbox" value="<?php echo $row['productid']; ?>||<?php echo $iterate; ?>" name="productid[]" style=""></td>
                            <td><?php echo $row['catname']; ?></td>
                            <td><?php echo $row['productname']; ?></td>
                            <td class="text-right">IDR <?php echo number_format($row['price'], 2); ?></td>
                            <td><input type="text" class="form-control" name="quantity_<?php echo $iterate; ?>"></td>
                            <td><input type="text" class="form-control" name="note_<?php echo $iterate; ?>"></td>
                        </tr>
                        <?php
                        $iterate++;
                    }
                ?>
            </tbody>
        </table>
        </div>

        <br><br>
        <div class="row">
            <div class="col-md-3">
                <input type="text" name="customer" class="form-control" placeholder="Nomor Meja" required>
            </div>
            <div class="col-md-2" style="margin-left:-20px;">
                <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Save</button>
            </div>
        </div>

    </form>
    <br><br>
</div>

<script>
var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
  coll[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var content = this.nextElementSibling;
    if (content.style.display === "block") {
      content.style.display = "none";
    } else {
      content.style.display = "block";
    }
  });
}
</script>
<script type="text/javascript">
    $(document).ready(function(){
        $("#checkAll").click(function(){
            $('input:checkbox').not(this).prop('checked', this.checked);
        });
    });
</script>
</body>
</html>

EDIT : And here are my followup code on purchase.php

<?php
    include('conn.php');
    if(isset($_POST['productid'])){

        $customer=$_POST['customer'];
        $sql="insert into purchase (customer, date_purchase) values ('$customer', NOW())";
        $conn->query($sql);
        $pid=$conn->insert_id;

        $total=0;

        foreach($_POST['productid'] as $product):
        $proinfo=explode("||",$product);
        $productid=$proinfo[0];
        $iterate=$proinfo[1];
        $sql="select * from product where productid='$productid'";
        $query=$conn->query($sql);
        $row=$query->fetch_array();

        if (isset($_POST['quantity_'.$iterate])){
            $subt=$row['price']*$_POST['quantity_'.$iterate];
            $total+=$subt;

            $sql="insert into purchase_detail (purchaseid, productid, quantity, note) values ('$pid', '$productid', '".$_POST['quantity_'.$iterate]."', '".$_POST['note_'.$iterate]."')";
            $conn->query($sql);
        }
        endforeach;

        $sql="update purchase set total='$total' where purchaseid='$pid'";
        $conn->query($sql);
        header('location:order_print.php');     
    }
    else{
        ?>
        <script>
            window.alert('Please select a product');
            window.location.href='order.php';
        </script>
        <?php
    }
?>

UPDATE : oke here's something that make it work : each category must be filled with value. If i added quantity value at least one for each category, then the value will be added to my databse. Now the question is : how can i still input the value despite some of the collapsible category remain blank ?

来源:https://stackoverflow.com/questions/60830326/data-not-posted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!