1

一个非常大的表格

我有一个包含大约 100 个输入的表单,我将其发送到 php 文件以提交数据。我正在尝试找到对提交文件进行编码的最佳方法,而我不必对每个帖子值进行编码。

<?php
/*

  UserFrosting Version: 0.2.0
  By Alex Weissman
  Copyright (c) 2014

  Based on the UserCake user management system, v2.0.2.
  Copyright (c) 2009-2012

  UserFrosting, like UserCake, is 100% free and open-source.

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the 'Software'), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

 */

require_once("../models/config.php");

if (!securePage(__FILE__)) {
    // Forward to index page
    addAlert("danger", "Whoops, looks like you don't have permission to view that page.");
    header("Location: 404.php");
    exit();
}

setReferralPage(getAbsoluteDocumentPath(__FILE__));
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">

        <title>Life Medical Response Dashboard</title>

        <?php require_once("includes.php"); ?>

    </head>

    <body>

        <div id="wrapper">

            <!-- Sidebar -->
            <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
            </nav>

            <div id="page-wrapper">
                <div class="row">
                    <div id='display-alerts' class="col-lg-12">

                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-12">
                        <h1>Dashboard <small>User Overview</small></h1>
                        <ol class="breadcrumb">
                            <li class="active"><i class="fa fa-dashboard"></i> Dashboard</li>
                        </ol>

                    </div>
                </div><!-- /.row -->

                <div class="row">
                    <div class="col-lg-12">
                        <div class="panel panel-primary">
                            <div class="panel-heading">
                                <h3 class="panel-title"><i class="fa fa-ambulance"></i> Options </h3>
                            </div>
                            <div class="panel-body">

                                <a class="btn btn-primary" href="add_stock.php" role="button">Request Stock</a>
                                <a class="btn btn-primary" href="remove_stock.php" role="button">Remove Stock</a>
                            </div>
                        </div>
                    </div>
                </div><!-- /.row -->

                <div class="row">
                    <div class="col-lg-12">
                        
                        <div class="panel panel-primary">
                          
                            <div class="panel-heading">
                                <h3 class="panel-title"><i class="fa fa-medkit"></i> Supplies </h3>
                            </div>
                            <div class="panel-body">
                                <div class="col-lg-6">
                            Date
                        </div>
                                <div class="col-lg-6">
                            Station
                        </div>
                                  <div class="col-lg-6">
                                      <form action="supply_request_submit.php">
                                          <input type="date" class="form-control" id="date" >
                        </div>
                        <div class="col-lg-6">
                            <select class="form-control">
                                 <?php
                                                include 'connection.php';
                                                $station = "SELECT * FROM stations";
                                                $result_station = mysql_query($station);
                                                while ($row_station = mysql_fetch_array($result_station)) {
                                                    ;
                                                    ?>
                                <option value="<?php echo $row_station['id'] ?>"><?php echo $row_station['station'] ?></option>
                                                <?php }?>
                                
                            </select>

                        </div>


                                <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                                    <div class="panel panel-default">
                                        <div class="panel-heading" role="tab" id="headingOne">
                                            <h4 class="panel-title">
                                                <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                                                    Airway Supplies
                                                </a>
                                            </h4>
                                        </div>
                                        <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
                                            <div class="panel-body">
                                                <?php
                                                include 'connection.php';
                                                $airway_supplies = "SELECT * FROM supplies WHERE category = 2";
                                                $result_airway_supplies = mysql_query($airway_supplies);
                                                while ($row_airway_supplies = mysql_fetch_array($result_airway_supplies)) {
                                                    ;
                                                    ?>
                                                    <div class="col-md-2">
                                                        <?php echo $row_airway_supplies['name'] ?> 
                                                    </div>
                                                    <div class="col-md-2">
                                                        <label class="sr-only" for="stock_<?php echo $row_airway_supplies['id'] ?>" form="supply">"></label>
                                                        <div class="input-group">
                                                            <div class="input-group-addon">Stock</div>
                                                            <input type="text" class="form-control" id="stock_<?php echo $row_airway_supplies['id'] ?>" form="supply" > 
                                                        </div>
                                                    </div>
                                                    <div class="col-md-2">
                                                        <label class="sr-only" for="request_<?php echo $row_airway_supplies['id'] ?>" form="supply">Amount (in dollars)</label>
                                                        <div class="input-group">
                                                            <div class="input-group-addon">Request</div>
                                                            <input type="text" class="form-control" id="request_<?php echo $row_airway_supplies['id'] ?>" form="supply" > 
                                                        </div>
                                                    </div>


                                                <?php } ?>

                                            </div>
                                        </div>
                                    </div>
                                    <div class="panel panel-default">
                                        <div class="panel-heading" role="tab" id="headingTwo">
                                            <h4 class="panel-title">
                                                <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
                                                    Trauma Equipment
                                                </a>
                                            </h4>
                                        </div>
                                        <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
                                            <div class="panel-body">
                                                <?php
                                                include 'connection.php';
                                                $airway_supplies = "SELECT * FROM supplies WHERE category = 3";
                                                $result_airway_supplies = mysql_query($airway_supplies);
                                                while ($row_airway_supplies = mysql_fetch_array($result_airway_supplies)) {
                                                    ;
                                                    ?>
                                                    <div class="col-md-2">
                                                        <?php echo $row_airway_supplies['name'] ?> 
                                                    </div>
                                                    <div class="col-md-2">
                                                        <label class="sr-only" for="stock_<?php echo $row_airway_supplies['id'] ?>" form="supply">Amount (in dollars)</label>
                                                        <div class="input-group">
                                                            <div class="input-group-addon">Stock</div>
                                                            <input type="text" class="form-control" id="stock_<?php echo $row_airway_supplies['id'] ?>" form="supply"> 
                                                        </div>
                                                    </div>
                                                    <div class="col-md-2">
                                                        <label class="sr-only" for="request_<?php echo $row_airway_supplies['id'] ?>"></label>
                                                        <div class="input-group">
                                                            <div class="input-group-addon">Request</div>
                                                            <input type="text" class="form-control" id="request_<?php echo $row_airway_supplies['id'] ?>" form="supply"> 
                                                        </div>
                                                    </div>


                                                <?php } ?>

                                            </div>
                                        </div>
                                    </div>
                                    <div class="panel panel-default">
                                        <div class="panel-heading" role="tab" id="headingThree">
                                            <h4 class="panel-title">
                                                <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
                                                    IV / Medication Administration
                                                </a>
                                            </h4>
                                        </div>
                                        <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
                                            <div class="panel-body">
                                                <?php
                                                include 'connection.php';
                                                $airway_supplies = "SELECT * FROM supplies WHERE category = 4";
                                                $result_airway_supplies = mysql_query($airway_supplies);
                                                while ($row_airway_supplies = mysql_fetch_array($result_airway_supplies)) {
                                                    ;
                                                    ?>
                                                    <div class="col-md-2">
                                                        <?php echo $row_airway_supplies['name'] ?> 
                                                    </div>
                                                    <div class="col-md-2">
                                                        <label class="sr-only" for="stock_<?php echo $row_airway_supplies['id'] ?>" form="supply"></label>
                                                        <div class="input-group">
                                                            <div class="input-group-addon">Stock</div>
                                                            <input type="text" class="form-control" id="stock_<?php echo $row_airway_supplies['id'] ?>" form="supply"> 
                                                        </div>
                                                    </div>
                                                    <div class="col-md-2">
                                                        <label class="sr-only" for="request_<?php echo $row_airway_supplies['id'] ?>" form="supply"></label>
                                                        <div class="input-group">
                                                            <div class="input-group-addon">Request</div>
                                                            <input type="text" class="form-control" id="request_<?php echo $row_airway_supplies['id'] ?>" form="supply"> 
                                                        </div>
                                                    </div>


                                                <?php } ?>

                                            </div>
                                        </div>
                                        <div class="panel panel-default">
                                            <div class="panel-heading" role="tab" id="headingFour">
                                                <h4 class="panel-title">
                                                    <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseFour" aria-expanded="false" aria-controls="collapseFour">
                                                        Miscellaneous Items
                                                    </a>
                                                </h4>
                                            </div>
                                            <div id="collapseFour" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingFour">
                                                <div class="panel-body">
                                                    <?php
                                                    include 'connection.php';
                                                    $airway_supplies = "SELECT * FROM supplies WHERE category = 5";
                                                    $result_airway_supplies = mysql_query($airway_supplies);
                                                    while ($row_airway_supplies = mysql_fetch_array($result_airway_supplies)) {
                                                        ;
                                                        ?>
                                                        <div class="col-md-2">
                                                            <?php echo $row_airway_supplies['name'] ?> 
                                                        </div>
                                                        <div class="col-md-2">
                                                            <label class="sr-only" for="stock_<?php echo $row_airway_supplies['id'] ?>" form="supply"></label>
                                                            <div class="input-group">
                                                                <div class="input-group-addon">Stock</div>
                                                                <input type="text" class="form-control" id="stock_<?php echo $row_airway_supplies['id'] ?>" form="supply"> 
                                                            </div>
                                                        </div>
                                                        <div class="col-md-2">
                                                            <label class="sr-only" for="request_<?php echo $row_airway_supplies['id'] ?>" form="supply"></label>
                                                            <div class="input-group">
                                                                <div class="input-group-addon">Request</div>
                                                                <input type="text" class="form-control" id="request_<?php echo $row_airway_supplies['id'] ?>" form="supply"> 
                                                            </div>
                                                        </div>


                                                    <?php } ?>

                                                </div>
                                            </div>
                                            <div class="panel panel-default">
                                                <div class="panel-heading" role="tab" id="headingFive">
                                                    <h4 class="panel-title">
                                                        <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseFive" aria-expanded="false" aria-controls="collapseFive">
                                                            Vital Sign Equipment
                                                        </a>
                                                    </h4>
                                                </div>
                                                <div id="collapseFive" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingFive">
                                                    <div class="panel-body">
                                                        <?php
                                                        include 'connection.php';
                                                        $airway_supplies = "SELECT * FROM supplies WHERE category = 6";
                                                        $result_airway_supplies = mysql_query($airway_supplies);
                                                        while ($row_airway_supplies = mysql_fetch_array($result_airway_supplies)) {
                                                            ;
                                                            ?>
                                                            <div class="col-md-2">
                                                                <?php echo $row_airway_supplies['name'] ?> 
                                                            </div>
                                                            <div class="col-md-2">
                                                                <label class="sr-only" for="stock_<?php echo $row_airway_supplies['id'] ?>" form="supply"></label>
                                                                <div class="input-group">
                                                                    <div class="input-group-addon">Stock</div>
                                                                    <input type="text" class="form-control" id="stock_<?php echo $row_airway_supplies['id'] ?>" form="supply"> 
                                                                </div>
                                                            </div>
                                                            <div class="col-md-2">
                                                                <label class="sr-only" for="request_<?php echo $row_airway_supplies['id'] ?>" form="supply"></label>
                                                                <div class="input-group">
                                                                    <div class="input-group-addon">Request</div>
                                                                    <input type="text" class="form-control" id="request_<?php echo $row_airway_supplies['id'] ?>" form="supply"> 
                                                                </div>
                                                            </div>


                                                        <?php } ?>

                                                    </div>
                                                </div>



                                            </div>
                                            <div class="panel panel-default">
                                                <div class="panel-heading" role="tab" id="headingSix">
                                                    <h4 class="panel-title">
                                                        <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseSix" aria-expanded="false" aria-controls="collapseSix">
                                                            Splinting Equipment
                                                        </a>
                                                    </h4>
                                                </div>
                                                <div id="collapseSix" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingSix">
                                                    <div class="panel-body">
                                                        <?php
                                                        include 'connection.php';
                                                        $airway_supplies = "SELECT * FROM supplies WHERE category = 7";
                                                        $result_airway_supplies = mysql_query($airway_supplies);
                                                        while ($row_airway_supplies = mysql_fetch_array($result_airway_supplies)) {
                                                            ;
                                                            ?>
                                                            <div class="col-md-2">
                                                                <?php echo $row_airway_supplies['name'] ?> 
                                                            </div>
                                                            <div class="col-md-2">
                                                                <label class="sr-only" for="stock_<?php echo $row_airway_supplies['id'] ?>"></label>
                                                                <div class="input-group">
                                                                    <div class="input-group-addon">Stock</div>
                                                                    <input type="text" class="form-control" id="stock_<?php echo $row_airway_supplies['id'] ?>" form="supply"> 
                                                                </div>
                                                            </div>
                                                            <div class="col-md-2">
                                                                <label class="sr-only" for="request_<?php echo $row_airway_supplies['id'] ?>"></label>
                                                                <div class="input-group">
                                                                    <div class="input-group-addon">Request</div>
                                                                    <input type="text" class="form-control" id="request_<?php echo $row_airway_supplies['id'] ?>" form="supply"> 
                                                                </div>
                                                            </div>


                                                        <?php } ?>

                                                    </div>
                                                </div>



                                            </div>
                                        </div>





                                        </ul>
                                    </div>
                                </div>
                                <div> <input type="submit" class="btn btn-primary" value="Submit" form="supply"></div>
                            
                            </div>
                        </div><!-- /.row -->

                    </div><!-- /#page-wrapper -->

                </div><!-- /#wrapper -->

                <script>
                    $(document).ready(function () {
                        alertWidget('display-alerts');

                        // Load the header
                        $('.navbar').load('header.php', function () {
                            $('.navitem-dashboard').addClass('active');
                        });
                    });
                </script>
                </body>
                </html>

<?php

$date = $_POST['date'];
$station = $_POST['station']
?>
<?php

include 'connection.php';
$supplies = "SELECT * FROM supplies";
$result_supplies = mysql_query($supplies);
while ($row_supplies = mysql_fetch_array($result_supplies)) {
    ;

}
?>

4

4 回答 4

1

考虑在表单变量上使用“名称”属性。(检查您的标记,您缺少一个 div - 我可能放错了位置)。

while ($row_airway_supplies = mysql_fetch_array($result_airway_supplies)) { ?>
    <div class="col-md-2">
        <?php echo $row_airway_supplies['name'] ?> 
    </div>
    <div class="col-md-2">
        <label class="sr-only" for="stock_<?php echo $row_airway_supplies['id'] ?>" form="supply">"></label>
        <div class="input-group">
            <div class="input-group-addon">Stock</div>
                <input type="text" class="form-control" id="stock_<?php echo $row_airway_supplies['id'] ?>" form="supply" name="stock[<?php echo $row_airway_supplies['id'] ?>]"> 
            </div>
        </div>
        <div class="col-md-2">
            <label class="sr-only" for="request_<?php echo $row_airway_supplies['id'] ?>" form="supply">Amount (in dollars)</label>
            <div class="input-group">
                <div class="input-group-addon">Request
                    <input type="text" class="form-control" id="request_<?php echo $row_airway_supplies['id'] ?>" form="supply" stock[<?php echo $row_airway_supplies['id'] ?>]> 
                </div>
            </div>
        </div>
    </div>
<?php } ?>

请注意以下事项:

<input type="text" class="form-control" id="stock_<?php echo $row_airway_supplies['id'] ?>" form="supply" name="stock[<?php echo $row_airway_supplies['id'] ?>]">
<!-- and -->
<input type="text" class="form-control" id="request_<?php echo $row_airway_supplies['id'] ?>" form="supply" stock[<?php echo $row_airway_supplies['id'] ?>]> 

通过添加 name = stock[...] 属性,表单变量将作为数组发布到您的提交处理程序。然后,您可以按如下方式处理它们:

foreach ($_FORM['stock'] as $supplies_id => $supplies_values {
   if (!empty($supplies_values) {
       echo "You submitted {$supplies_values} for id {$supplies_id}.<br />";
   }
}
于 2015-12-28T08:15:46.340 回答
0

为了不$_POST[]单独获取每个值,您可以使用extract()

extract($_POST);

它将每个$_POST键变成一个变量。

例如,不要这样做:

$date = $_POST['date'];
$station = $_POST['station'];

extract将自动$date生成$station。(以及所有其他键)

查看提取功能的本手册

我必须承认,考虑到它的惊人程度,我对这个功能没有太多探索感到惊讶。

于 2015-12-28T04:38:11.087 回答
0

保持表字段名称和表单输入字段名称相同。写一个函数:

/**
         * @param array $fields_values
         * @param string $table
         * @return string
         */
        function _insertQuery(array $fields_values, $table) {
            $fields = implode(', ', array_keys($fields_values));
            $values = implode(', ', array_values($fields_values));
            return $query = "INSERT INTO {$table} ({$fields}) VALUES ({$values})";
        }
于 2015-12-28T04:51:57.223 回答
0

尝试使用Jquery'sserialize方法,它会根据您的表单返回一个用于发布的字符串,然后您可以使用提交表单AJAX

例如:

<form id="form1> <input name="username" value="hi"> <input name="password" value="1"> </form>

然后在jquery上:

var data = $('#form1').serialize();

现在的数据应该是:

username=hi&password=1

(注意:POST 名称基于 HTML 标记上的输入名称)

然后使用 AJAX 将其发布到您的 php 脚本中:

$.ajax({ url: "yourphpscripthere.php", //<-- PHP Script where you want to post your data type: "POST", data: data, //<-- this is the posted string success: function(data){ //do something when request is finished. } });

然后在你的yourphpscripthere.php

你可以这样做:

$fields = array();
 $values = array();
 foreach($_POST as $key => $value){
      array_push($fields, $key);
      array_push($values, $value);
 }

$sql = "INSERT INTO table (".implode(", ", $fields).") VALUES (".implode(", ", $values).")"

mysql_query($sql);

注意:HTML 标记中输入表单的名称应与字段相同

于 2015-12-28T05:05:23.510 回答