donglang1894 2015-06-06 11:26
浏览 116
已采纳

从订购的产品中创建提交表单(将表格包装成表格并再次将其显示为表格)

EDIT: Maybe its easier to create a new column in the database. Named: to_order. Then create an UPDATE Query for each product . Something like : UPDATE products SET to_order = '[VALUE_INPUT]' WHERE id = '[ID_PRODUCT]' The problem here is, where to excecute this query? How can I define the VALUE_INPUT and ID_PRODUCT for each row?

If this works, and I can UPDATE each row for that specific product etc. I can easily create a mysqli_fetch_assoc again where only the [Product_name],[to_order] will be given. Please help.


I'm working on a supplier order system. Where the manager of the restaurant can easily select a supplier, fill in how much stock he has now of that specific product. And it will automatically calculate how much you need to order.

This part is done.But now, once we fill in the form I want to get an overview of all products you need to order, and it needs to be printable.

For example in words explained:

  • We have some columns in our table [ID_product][ID_supplier][Product_name][Stock][Minimum][To_order]

  • ID_product, ID_supplier, Product_name and Minimum are all data from the database. Stock is what you need to fill in. And To_order is calculated by : Minimum-stock = To_order. (logic)

  • With a mysql_fetch_assoc command we can show all specific products with its specific id and minimum integer.

  • Now here is the part where my question is: Once everything is filled in, you need to click a button that refers to a next page. On this page your total input is shown, a full list. Like: [Product_name][To_order]

So on this page you get an overview of your form where you filled in all these values. So you get a list (how big depends on how much products you have in your database) with all the calculated inputs from 'To_order'.

My problem is, if I create a Form Action into my fetch_assoc, it can read all element names, but as soon you submit the form and go to the next page. All the data is lost. I need something where I can see the value from the previous page of that input. And then for all specific products.

My form.php (Where I need to fill in my stock in order to calculate the 'To_order' input). This is working fine.

    <table width="600" border="1" cellpadding"1" cellspacing= "1" class="flatTable">
                                <tr class="headingTr">
                                    <th>
                                        Supplier code
                                    </th>
                                    <th>
                                        Product
                                    </th>
                                        <th>
                                        Stock
                                    </th>

                                        <th>
                                        Minimum
                                    </th>

                                        <th>
                                        To order
                                    </th>
                                </tr>


                                <?php


                                while ($producten=mysqli_fetch_assoc($result_producten)) {
                                    echo "<tr>";
                                    echo "<td><form method='POST' action='lijst.php'><label name='".$producten['lev_id']."'>".$producten['lev_id']."</label></td>";
                                    echo "<td><label name='".$producten['productnaam']."'>".$producten['productnaam']."</label></td>";

                                        echo "<td>
      <input id='".$producten['minimum']."' name='".$producten['id']."' type='text' 
         oninput='calculateBestelling(this.value,this.name, this.id)' 
          onkeypress='return event.charCode >= 48 && event.charCode <= 57'/>
                  </td>";
                                    echo "<td><input id='".$producten['minimum']."' name='mytext2'  type='text' readonly='true' value='".$producten['minimum']."' /></td>";

                                    echo "<td><input id='".$producten['id']."' name='order[]' type='text' readonly='true'  /></td>";


                                    echo "</tr>";



                                }


                                ?>

<script>
var minimum;
var stock;
var order;
function calculateBestelling(val,name,id){
    minimum = document.getElementById(id).id;
    stock =  document.getElementById(id).value;
    document.getElementById(name).value = minimum - val;
    order = document.getElementById(name).value;

    if (order < 0) {


    document.getElementById(name).value = '0';
   }

}


</script>
                            </table>
                                    <p><input type='submit'/></p></form>

Then the next page (where the overview needs to be shown):

<header>
                        <h2>Complete order form - Supplier: (HERE THE SUPPLIER)</h2>

                    </header>
                    <div class="container">
                        <div class="row">

                            <table width="600" border="1" cellpadding"1" cellspacing= "1" class="flatTable">
                                <tr class="headingTr">
                                    <th>
                                        Product
                                    </th>
                                    <th>
                                        To order
                                    </th>

                                </tr>


                                <?php
                            //And I dont know what I need to do here. I want the values from the 3rd input the previous file. But how I can combine this with each row for a specific product?

                                while ($producten=mysqli_fetch_assoc($result_producten)) {
                                    echo "<tr>";
                                    echo "<td>PRODUCT HERE</td>";

                                    echo "<td><input id='toorder' name='order[]' type='text' readonly='true' value='(THE VALUE OF THE PREVIOUS FILE FOR THAT PRODUCT)' /></td>";


                                    echo "</tr>";


                                }


                                ?>

                            </table>



                        </div>
                    </div>

I created an image, where you simple can see what I have in my mind. URL TO IMAGE: http://i.stack.imgur.com/6AnTl.png

So we also need to check something like : if (!to_order > 0) { DONT SHOW ROW }

Feel free to change codes, maybe some other way that works? Like staying on the same page, and hide the stock and minimum values. So we only can see the ID's, Product names and To_order values?

  • 写回答

2条回答 默认 最新

  • dongzhan7909 2015-06-07 21:45
    关注

    After hours trying I finally figured it out. I used $_SESSIONS to store data in. Thats one thing. Then, made sure that the inputs are all array.

    while ($producten=mysqli_fetch_assoc($result_producten)) {
                                        echo "<form method='POST' action='verwerken.php'><tr>";
                                        echo "<td><input name='ids[]' type='text' readonly='true' value='".$producten['id']."' /></td>";
                                        echo "<td><input name='lev_id' type='text' readonly='true' value='".$producten['lev_id']."' /></td>";
                                        echo "<td><input name='producten[]' type='text' readonly='true' value='".$producten['productnaam']."' /></td>";
    
    
                                           echo "<td>
          <input tabindex='1' id='".$producten['minimum']."' name='".$producten['id']."' type='text' 
             oninput='calculateBestelling(this.value,this.name, this.id)' 
              onkeypress='return event.charCode >= 48 && event.charCode <= 57'/>
                      </td>";
                                        echo "<td><input id='".$producten['minimum']."'  type='text' readonly='true' value='".$producten['minimum']."' /></td>";
    
                                        echo "<td><input id='".$producten['id']."' name='test[]' type='text' readonly='true'  /></td>";
    
    
                                        echo "</tr>";
    
     }
    

    This is the while for the table. Once you filled in all your STOCK values, it will automatically calculate the value to order. Thats working fine. Now when you click the submit button a next page will be openend. This page only inserts a query into a database. For every row it will update the to_order value in the database.

    $order_test = $_SESSION['hoeveelheid'] =$_POST['test'];
        $ids_test = $_POST['ids'];
            $producten = $_SESSION['product'] = $_POST['producten'];
    
        foreach (array_combine($producten, $order_test) as $producten => $order_test) {
    
        echo 'Product: ' . $producten . ' - Te  bestellen: ' . $order_test.'<br>';
         mysqli_query($conn, "UPDATE producten SET bestellen = '".$order_test."' WHERE productnaam ='".$producten."'");
    
    }
    
        $_SESSION['lev_id'] = $_POST['lev_id'];
        $_SESSION['check'] = 'true';
    header('Location: bestelformulier.php');
    exit();
    
            }
    

    If the database is not that slow, you should not see this page. After the query is done, you will be redirected to the overview page. Bestelformulier.php.

    Here you simply mysqli_fetch_assoc the values from the database again. And you have an updated form.

    Thanks everyone for the help (:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?