将两个或多个提交按钮组合为一个

时间:2019-01-16 06:01:04

标签: javascript php html

我为网页的不同部分提供了提交按钮。提交按钮用于使用表单字段中的文本值更新表单和数据库。当前,每个提交按钮仅在自己的部分中更新表单(与各自的PKEY ID“ consideration_no”绑定)。我想一键更新所有部分的表格。

从下面的代码中可以看到,有2个提交按钮。我试图通过ID将两个链接在一起,但对我来说不起作用。

    // Include config file
    require_once "config.php";

    // Define variables and initialize with empty values
    $question = $answer = "";
    $question_err = $answer_err = "";

     if(isset($_POST["dg_no"]) && !empty($_POST["dg_no"])){

        //counter for array
        $counter = 0;

        // Get hidden input value
        $dg_no = $_POST['dg_no'];
        $consideration_no = $_REQUEST['consideration_no'];
        $answer = $_POST['answer'];

        // Check input errors before inserting in database
        if(empty($answer_err)){

            // Validate address address
            $input_answer = trim($_POST["answer"]);
            if(empty($input_answer)){
                $answer_err = "Please enter an answer.";     
            } else{
                $answer = $input_answer;
                $answer1[$counter] = $input_answer;
            }   

            // Prepare an Submit statement
            $sql = 'Update "PDPC".consideration SET answer=:answer WHERE consideration_no = :consideration_no';
            if($stmt = $pdo->prepare($sql)){

                $stmt->bindParam(":answer", $param_answer);
                $stmt->bindParam(":consideration_no", $param_consideration_no);
                //$stmt->bindParam(":dg_no", $param_dg_no);

                //Set Parameter in while loop, hence new set of parameter for every new form is created and executed. 
                //Could change the counter loop to a dynamic loop with foreach array. 
                while ($counter<15){    
                $param_answer = $answer[$counter];
                $param_consideration_no = $consideration_no[$counter];
                $stmt->execute();
                //$param_dg_no = $dg_no;
                // Attempt to execute the prepared statement

                    //debugggggg
    /*              $message = $consideration_no[$counter];
                    $message1 = $answer[$counter];
                    $message2 = 'lol';
                    echo "<script type='text/javascript'>alert('$message, $message1, $message2 ');</script>";  */

                    $counter++;
                    //apparently redirecting can be placed in the loop, and fields will still get changed.
                    //header("location: home1.php?dm_no=".$_GET["dm_no"]);
                    header("location: home1.php?dm_no=".$_GET["dm_no"]);

                }

            }
                    if($stmt->execute()){
                    //Records Submitd successfully. Redirect to landing page
                    header("location: home1.php?dm_no=".$_GET["dm_no"]);
                    exit();
                } else{
                    echo "Something went wrong. Please try again later.";
                }

            // Close statement
            unset($stmt);
        }

        // Close connection
        unset($pdo);
    } else{
        /* --- DISPLAY/READ TABLE, SEE SECTIONS AND ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
        // Check existence of dg_no parameter before processing further
        if(isset($_GET["dg_no"]) && !empty(trim($_GET["dg_no"]))){

            // Get URL parameter
            $dg_no =  trim($_GET["dg_no"]);

            // Prepare a select statement
            $sql = 'SELECT * FROM "PDPC".consideration WHERE (dg_fkey = :dg_no AND code_no = 1) ORDER BY consideration_no';
            if($stmt = $pdo->prepare($sql)){
                // Bind variables to the prepared statement as parameters
                $stmt->bindParam(":dg_no", $param_no);

               // Set parameters
               //$param_no = $dg_no;
                $param_no = trim($_GET["dg_no"]);


                // Attempt to execute the prepared statement
                if($stmt->execute()){
                    if($stmt->rowCount() > 0){

                    SubSection($subsection1_1); //Consent Collection Subsection
                        while($row = $stmt->fetch()){
                        // Retrieve individual field value

                        $consideration_no = $row["consideration_no"];
                        $question = $row["question"];
                        $answer = $row["answer"];
                        $dg_no = $_GET['dg_no'];

                        //...time to show the questions and answers with the while loop...
                        ?>

                              <form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
                                <div class="form-group <?php echo (!empty($answer_err)) ? 'has-error' : ''; ?>">
                                    <label><?php echo $question; ?></label>
                                    <input type="text" name="answer[]" class="form-control" value="<?php echo $answer; ?>">
                                    <span class="help-block"><?php echo $answer_err;?></span>
                                    <input type="hidden" name="consideration_no[]"  value="<?php echo $consideration_no; ?>"/>
                                    <input type="hidden" name="dg_no" value="<?php echo $dg_no; ?>"/>
                                </div>
                        <?php

                        }

                        //...after the loop, show the Submit and Cancel button, coz we only need 1 set each section. 
                        ?>  

                                <input type="Submit" name = "$consideration_no[]" class="btn btn-primary" value="Submit">
                                <a href="javascript:history.go(-1)" class="btn btn-default">Cancel</a>
                               </form>
                            </div>
                        <?php
                    } 
                }           
                else{
                    echo "Oops! Something went wrong. Please try again later.";
                }
            }

        Section($section2); //Collection section
            // Prepare a select statement
            $sql = 'SELECT * FROM "PDPC".consideration WHERE (dg_fkey = :dg_no AND code_no = 2) ORDER BY consideration_no';
            if($stmt = $pdo->prepare($sql)){
                // Bind variables to the prepared statement as parameters
                $stmt->bindParam(":dg_no", $param_no);

               // Set parameters
               //$param_no = $dg_no;
                $param_no = trim($_GET["dg_no"]);

                // Attempt to execute the prepared statement
                if($stmt->execute()){
                    if($stmt->rowCount() > 0){

                    SubSection($subsection2); //Consent Collection Subsection
                        while($row = $stmt->fetch()){
                        // Retrieve individual field value

                        $consideration_no = $row["consideration_no"];
                        $question = $row["question"];
                        $answer = $row["answer"];
                        $dg_no = $_GET['dg_no'];

                        //...time to show the questions and answers with the while loop...
                        ?>
                              <form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
                                <div class="form-group <?php echo (!empty($answer_err)) ? 'has-error' : ''; ?>">
                                    <label><?php echo $question; ?></label>
                                    <input type="text" name="answer[]" class="form-control" value="<?php echo $answer; ?>">
                                    <span class="help-block"><?php echo $answer_err;?></span>
                                    <input type="hidden" name="consideration_no[]" value="<?php echo $consideration_no; ?>"/>
                                    <input type="hidden" name="dg_no" value="<?php echo $dg_no; ?>"/>
                                </div>
                        <?php
                        }

                        //...after the loop, show the Submit and Cancel button, coz we only need 1 set each section. 


                        ?>
                                <input type="Submit" name = "$consideration_no[]" class="btn btn-primary" value="Submit">
                                <a href="javascript:history.go(-1)" class="btn btn-default">Cancel</a>
                               </form>
                            </div>
                        <?php   
                    } 
                }           

                else{
                    echo "Oops! Something went wrong. Please try again later.";
                }
            }


            // Close statement
            unset($stmt);
            // Close connection
            unset($pdo);
        }  

        else{
            // URL doesn't contain dg_no parameter. Redirect to error page
            header("location: error.php");
            exit();
        }   
    }

I want it to update all the fields, in different sections, with one submit button

2 个答案:

答案 0 :(得分:1)

您的代码有点难以阅读,但是据我了解,您正在尝试将两个或多个表单提交合并为一个。很安静

<form method="POST" action="save.php">
   <input type=text name=name[] />
   <input type=text name=name[] />
</form>

通过使用[]来标识输入元素,您可以拥有多个具有相同名称的值,您可以在其中以数组的形式从PHP脚本访问它们。

例如,上面的示例将产生如下的数组

<?php
   print_r($_POST['name']); //("name" => Array....

这对您来说足够清楚了吗?如果不发表评论,我会解释更多。作为一个侧面说明,我建议您研究使用模板引擎以及编码项目中的框架。

答案 1 :(得分:0)

这是我将html分离到新文件中时看到的内容。我试图删除多余的表格,但是当我打开最后一个可折叠部分时,它立即执行了一个提交动作并将我带回到主页。

    <button class="collapsible"><?php echo $section ?></button>
    <div class="content">

    <button class="collapsible"><?php echo $subsection ?></button>
     <form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">

                <?php
                //while loop start
                ?>

                      <form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
                        <div class="form-group <?php echo (!empty($answer_err)) ? 'has-error' : ''; ?>">
                            <label><?php echo $question; ?></label>
                            <input type="text" name="answer[]" class="form-control" value="<?php echo $answer; ?>">
                            <span class="help-block"><?php echo $answer_err;?></span>
                            <input type="hidden" name="consideration_no[]"  value="<?php echo $consideration_no; ?>"/>
                            <input type="hidden" name="dg_no" value="<?php echo $dg_no; ?>"/>
                        </div>
                <?php
                //while loop ends
                ?>  

                        <input type="Submit" name = "$consideration_no[]" class="btn btn-primary" value="Submit">
                        <a href="javascript:history.go(-1)" class="btn btn-default">Cancel</a>
                       </form>
                    </div>
                <?php
                //while loop start
                ?>
                      <form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
                        <div class="form-group <?php echo (!empty($answer_err)) ? 'has-error' : ''; ?>">
                            <label><?php echo $question; ?></label>
                            <input type="text" name="answer[]" class="form-control" value="<?php echo $answer; ?>">
                            <span class="help-block"><?php echo $answer_err;?></span>
                            <input type="hidden" name="consideration_no[]" value="<?php echo $consideration_no; ?>"/>
                            <input type="hidden" name="dg_no" value="<?php echo $dg_no; ?>"/>
                        </div>
                <?php
                //while loop ends
                ?>  
                        <input type="Submit" name = "$consideration_no[]" class="btn btn-primary" value="Submit">
                        <a href="javascript:history.go(-1)" class="btn btn-default">Cancel</a>
                       </form>
                    </div>
                <?php   
相关问题