【问题标题】:Combining two or more submit buttons into one将两个或多个提交按钮合二为一
【发布时间】:2019-01-16 06:01:03
【问题描述】:

我为网页的不同部分设置了提交按钮。提交按钮用于使用表单字段中的文本值更新表单和数据库。目前,每个提交按钮仅在它们自己的部分中更新表单(与它们各自的 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

【问题讨论】:

    标签: javascript php html


    【解决方案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....
    

    这对你来说够清楚了吗?如果不发表评论,我会解释更多。作为旁注,我建议您考虑使用模板引擎,以及您的编码项目中的框架。

    【讨论】:

    • 您好,感谢您回复我!我有点明白你的意思。我现在正在尝试它,我可能做错了,因此它仍然对我不起作用,但我正在尝试删除中间的 ,使其成为一种形式而不是多种形式,但事实并非如此为我工作。我已经在我的代码中使用了 name=consideration_no[]。
    • 对我的编码表示歉意。我是一个没有经验的编码员,我是 php 和 html 的新手。我会牢记您的建议以备将来使用
    • 没有理由道歉,我们都是从0开始的,我建议你尝试将html和php分开到不同的文件中,这样更便于阅读。您还可以做的是从一个部分中完全删除
      ,然后扩展您留下的表单以引入任何其他逻辑
    • 我将 html 分离到一个新文件中以查看逻辑,并尝试删除中间的
      ,但是,当我打开最后一个可折叠部分时,它会立即执行提交操作并将我带回主页。
    【解决方案2】:

    这是我将 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   
    

    【讨论】:

      猜你喜欢
      • 2011-06-12
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 2017-05-07
      • 2012-09-13
      • 2023-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多