首页 > 解决方案 > 表单一键提交多次,多次运行相关action=""脚本

问题描述

这是我正在尝试做的事情:

我正在为我的工作开发一个库存管理系统,这个想法是当我们需要订购某些东西时,它的状态在 mySQL 数据库中设置为“订购”,其中包含我们所有的库存项目。我们有一个“订单队列”页面,显示所有需要订购的东西。不同之处在于,我们需要为每个供应商提供两张不同的表格:一张用于采购订单,一张用于询价(例如,购买原材料并不总是固定价格,因此 PO 和 RFQ 需要不同的格式)。然后对于每个供应商表都有一个提交按钮,上面写着“推送供应商名称PO/RFQ”。

按下此按钮会将您带到第二个页面,该页面创建 PO 并将电子邮件发送给供应商。我知道这不是这里的问题,因为无论此脚本中有什么内容,当按下按钮时它都会运行三次。

这是一些代码:

$suppTable = mysqli_query($conn,"SELECT * FROM `suppliers` WHERE (`user` = '" . $user_login . "')");

这将创建存储所有供应商信息的数组,并且都是正确的。

<div class="trackheader" style="border-radius:5px 5px 0px 0px; border: 2px solid #CC3333;">
    <h3 style="color:white !important; margin-left: 10px; vertical-align: middle; display: inline-block;">Display:</h3>
</div>
<div class="trackcont3" style="border-radius:0px 0px 5px 5px; border: 2px solid #CC3333; margin-bottom:20px;">
    <form action="" method="POST" id="rec">
        <?php
        while($row1 = mysqli_fetch_assoc($suppTable)){
            echo "<input type='checkbox' id='" . $row1['name'] . "' name='" . $row1['name'] . "' value='" . $row1['name'] . "' onchange='this.form.submit()'";
            if(isset($_POST['' . $row1['name'] . ''])){ 
                echo "checked='checked'"; 
            }
            echo ">";
            echo "<label for='" . $row1['name'] . "'>" . $row1['name'] . "</label>";
        }
        ?>   
    </form>
</div>

请忽略内联 CSS - 一旦它实际工作,它将被添加到样式表中......这是一个非常简单的片段:它为数据库中的每个供应商创建显示切换,这样它们就不会一次全部显示出来。

现在来看看有趣的东西:

<?php
$loopTable = mysqli_query($conn,"SELECT * FROM `suppliers` WHERE (`user` = '" . $user_login . "')");  //this creates another array with each supplier tied to the user account
    while($row2 = mysqli_fetch_assoc($loopTable)){                                                        //this while loop creates two tables for each supplier: one with items that need to be ordered in a PO and another table for RFQ
        if(isset($_POST['' . $row2['name'] . ''])){                                                       //controls display from the above form
                $display['' . $row2['name'] . ''] = "block";
            } else {
                $display['' . $row2['name'] . ''] = "none";
            }
echo "<div style='display:" . $display['' . $row2['name'] . ''] . " ;'>";
            echo "<div class='trackheader' style='border-radius:5px 5px 0px 0px; border: 2px solid #CC3333;'>
                        <h3 style='color:white !important; margin-left: 10px; vertical-align: middle; display: inline-block;'>" . $row2['name'] . " PO</h3>
                    </div>";
            echo "<div class='trackcont2'><table style='width:100%; !important'><tbody style='width:100%; !important'>
                    <tr style='width:100% !important;'>
                        <th style='width:16.6%;'><h3 style='color:grey !important;'>ALEX ID</th>
                        <th style='width:16.6%;'><h3 style='color:grey !important;'>Description</th>
                        <th style='width:16.6%;'><h3 style='color:grey !important;'>Supplier P/N</th>
                        <th style='width:16.6%;'><h3 style='color:grey !important;'>Order QTY</th>
                        <th style='width:16.6%;'><h3 style='color:grey !important;'>Price</th>
                        <th style='width:16.6%;'><h3 style='color:grey !important;'>Total</th>
                    </tr>";
                $i = 0;
                $listTablePO = mysqli_query($conn,"SELECT * FROM `inventory` WHERE (`user` = '" . $user_login . "') AND (`supplier` = '" . $row2['name'] . "') AND (`status` = 'order') AND (`method` = 'PO')"); //this creates an array with all database items from the appropriate supplier which need ordered (and go in the PO table)
                $total = 0; //tallies a running total of the price
                while($row3 = mysqli_fetch_assoc($listTablePO)){ //this while loop creates the table of all parts that need ordered and their necessary info
                    if($i % 2 == 0){
                        echo "<tr class='odd' style='text-align: center;'>";
                    } else {
                        echo "<tr class='even' style='text-align: center;'>";
                    }
                                echo "<td>" . $row3['part_name'] . "</td>";
                                echo "<td>" . $row3['description'] . "</td>";
                                if($row3['order_link'] == null){
                                    echo "<td>" . $row3['supplier_part_no'] . "</td>";
                                } else {
                                    echo "<td><a href='" . $row3['order_link'] . "' target='_blank'>" . $row3['supplier_part_no'] . "</a></td>";
                                }
                                if($row['order_override'] == 0){
                                    echo "<td>" . $row3['order_qty'] . "</td>";
                                    $qty = $row3['order_qty'];
                                } else {
                                    echo "<td>" . $row3['order_override'] . "</td>";
                                    $qty = $row3['order_override'];
                                }
                                echo "<td>$" . sprintf('%.2lf', $row3['price']) . "</td>";
                                $total = ($total + ($qty * $row3['price']));
                                echo "<td>$" . sprintf('%.2lf', $total) . "</td>";
                            echo "</tr>";
                        $i++; 
                    }
            echo "</tbody></table></div>
                    <form action='/po-mailing/' method='POST' name='" . $row2['name'] ."PO'>
                        <input type='hidden' value='" . $row2['name'] . "' name='PO' id='PO'></input>
                        <button type='submit' class='addbutton' style='margin-top: -20px !important; margin-bottom: 20px;'>Push " . $row2['name'] . " PO</button>
                    </form>
            </div>"; //the above snippet handles creating the appropriate form and submit button. All this needs to do is route to the appropriate page (/po-mailing/) and pass the correct supplier name. 
                     //From there I can get all the necessary info from the SQL database and don't need to pass any other info through the form.
                     //I suspect this is where my issue lies, but all I need is an individual Push PO button for each supplier that directs to /po-mailing/ and passes the supplier info to the page. The problem here is it is doing it three times...

下一个代码与上面基本相同,但用于 RFQ 表。跳过这部分

// the same code is essentially repeated for RFQ for each supplier with minor format changes (it is also irrelevant as I have only been testing with the PO side)
            echo "<div style='display:" . $display['' . $row2['name'] . ''] . " ;'>";
            echo "<div class='trackheader' style='border-radius:5px 5px 0px 0px; border: 2px solid #CC3333;'>
                        <h3 style='color:white !important; margin-left: 10px; vertical-align: middle; display: inline-block;'>" . $row2['name'] .  " RFQ</h3>
                    </div>";
            echo "<div class='trackcont2'><table style='width:100% !important;'><tbody style='width:100%; !important'>
                    <tr style='width:100% !important;'>
                        <th style='width:16.6%;'><h3 style='color:grey !important;'>ALEX ID</th>
                        <th style='width:16.6%;'><h3 style='color:grey !important;'>Description</th>
                        <th style='width:16.6%;'><h3 style='color:grey !important;'>Supplier P/N</th>
                        <th style='width:16.6%;'><h3 style='color:grey !important;'>Order QTY</th>
                        <th style='width:16.6%;'><h3 style='color:grey !important;'>Last Price</th>
                        <th style='width:16.6%;'><h3 style='color:grey !important;'>Estimate</th>
                    </tr>";
                    $i = 0;
                    $listTableRFQ = mysqli_query($conn,"SELECT * FROM `inventory` WHERE (`user` = '" . $user_login . "') AND (`supplier` = '" . $row2['name'] . "') AND (`status` = 'order') AND (`method` = 'RFQ')");
                    while($row5 = mysqli_fetch_assoc($listTableRFQ)){
                        if($i % 2 == 0){
                            echo "<tr class='odd' style='text-align: center;'>";
                        } else {
                            echo "<tr class='even' style='text-align: center;'>";
                        }
                                echo "<td>" . $row5['part_name'] . "</td>";
                                echo "<td>" . $row5['description'] . "</td>";
                                if($row5['order_link'] == null){
                                    echo "<td>" . $row5['supplier_part_no'] . "</td>";
                                } else {
                                    echo "<td><a href='" . $row5['order_link'] . "' target='_blank'>" . $row5['supplier_part_no'] . "</a></td>";
                                }
                                if($row['order_override'] == 0){
                                    echo "<td>" . $row5['order_qty'] . "</td>";
                                    $qty = $row5['order_qty'];
                                } else {
                                    echo "<td>" . $row5['order_override'] . "</td>";
                                    $qty = $row5['order_override'];
                                }
                                echo "<td>$" . sprintf('%.2lf', $row5['price']) . "</td>";
                                $total = ($total + ($qty * $row5['last_price']));
                                echo "<td>$" . sprintf('%.2lf', $total) . "</td>";
                            echo "</tr>";
                        $i++; 
                    }
            echo "</tbody></table></div>
            <form action='/rfq-mailing/' method='POST' id='" . $row2['name'] ."RFQ'>
                <input type='hidden' value='" . $row2['name'] . "' name='" . $row2['name'] . "' id='" . $row2['name'] . "'></input>
                <button type='submit' class='addbutton' style='margin-top: -20px !important; margin-bottom: 20px;'>Push " . $row2['name'] . " RFQ</button>
            </form>
            </div>";
        }
        ?>

希望有人能发现我在哪里搞砸了.. FWIW 我也使用 Wordpress 作为系统的后端(只是一种处理帐户、创建页面等的简单方法)

标签: phphtmlmysqlwordpress

解决方案


我找到了解决方案:

我的 wordpress 主题似乎多次加载每个页面的标题,因此问题不在于代码本身。在每次加载时,它都会提交信息。

if(isset($_POST['submitbuttonexample'])){}在解决问题中包含必要的信息。


推荐阅读