我正在用html和php开发表单,页面中有多个部分,每个部分都需要一个提交按钮。但是我做不到。使用多个提交按钮,它可以工作。任何人都可以帮我解决这个问题。我需要创建一个带有单个提交按钮的表单。这是示例代码。

    <html lang="en">
        <head>
        <meta charset="UTF-8">
        <title>Add Records Form</title>
        </head>
        <body>
        <table style="width:100%">
        <tr>
        <th rowspan="2">
        <div class="container">
            <div class="row">
            <div class="col-md-8 col-md-offset-2">
            <form action="insert1.php" method="post" enctype="multipart/form-data">
            <div class="form-group">
            <label class="control-label">NCBI Accession number</label>
            <input type="text" name="Gene_NCBI_Accession_number" placeholder="NCBI Accession number" class="form-control">
            </div>
            <div class="form-group">
            <label class="control-label">locus tag</label>
            <input type="text" name="Gene_name_locus_tag" placeholder="locus tag" class="form-control">
            </div>

            </th>

            <td>
            <div class="container">
            <div class="row">
            <div class="col-md-8 col-md-offset-2">
            <form action="insert1.php" method="post" enctype="multipart/form-data">
            <div class="form-group">
            <label class="control-label"><b>Antibiotic</label>
            <input type="text" name="Antibiotic_name" placeholder="Antibiotic name" class="form-control">
            </div>
            </td>
            <td>
            <div class="form-group">
            <label class="control-label"><b>Name</label>
            <input type="text" name="Name" placeholder="Name" class="form-control" required>
            </div>
            </td>
          </tr>
          <tr>
              <td rowspan="2">
        <h4> Please fill in information for other related data or give your comments if any</h4>
        <br>
        <textarea rows="7" cols="100" name="comment" form="usrform">
        Enter text here...</textarea>

              </td>
              <td>
                  </div>
                                         <div class="form-group">
                                <label class="control-label"><b>Protein function</label>
                                <input type="text" name="Protein_function" placeholder="Protein_function" class="form-control">
                            </div>
                            <div class="form-group">
                                <label class="control-label">Biological process</label>
                                <input type="text" name="Biological_process" placeholder="Biological process" class="form-control">
                            </div>

              </td>

          </tr><br>
            <br>
            <br>
            <br>
            <br>
            <br>
                 </form></table>
             <input type="submit" value="Add Records">
        <?php include 'footer.php'; ?>
        </body>
        </html>

        <?php
        /* Attempt MySQL server connection. Assuming you are running MySQL
        server with default setting (user 'root' with no password) */
        $link = mysqli_connect("abc", "abc", "abc", "abc");

        // Check connection
        if($link === false){
            die("ERROR: Could not connect. " . mysqli_connect_error());
        }

        // Escape user inputs for security
        $Gene_NCBI_Accession_number = mysqli_real_escape_string($link, $_REQUEST['Gene_NCBI_Accession_number']);
        $Gene_name_locus_tag = mysqli_real_escape_string($link, $_REQUEST['Gene_name_locus_tag']);
        $Antibiotic_name = mysqli_real_escape_string($link, $_REQUEST['Antibiotic_name']);
        $Name = mysqli_real_escape_string($link, $_REQUEST['Name']);
        $Protein_function = mysqli_real_escape_string($link, $_REQUEST['Protein_function']);
        $Biological_process= mysqli_real_escape_string($link, $_REQUEST['Biological_process']);


        // attempt insert query execution
        $sql = "INSERT INTO contact_form_info (Gene_NCBI_Accession_number, Gene_name_locus_tag, Antibiotic_name, Name, Protein_function, Biological_process) VALUES ('$Gene_NCBI_Accession_number', '$Gene_name_locus_tag', '$Antibiotic_name', '$Name','$Protein_function', '$Biological_process' )";
        if(mysqli_query($link, $sql)){
            #echo "Records added successfully.";
        } else{
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
        }

        // close connection
        mysqli_close($link);
        ?>

最佳答案

您的提交按钮不在表格之外,这就是为什么它不起作用的原因。请参考下面的代码来解决您的问题。

<html lang="en">
        <head>
        <meta charset="UTF-8">
        <title>Add Records Form</title>
        </head>
        <body>
        <table style="width:100%">
        <tr>
        <th rowspan="2">
        <div class="container">
            <div class="row">
            <div class="col-md-8 col-md-offset-2">
            <form action="insert1.php" method="post" enctype="multipart/form-data">
            <div class="form-group">
            <label class="control-label">NCBI Accession number</label>
            <input type="text" name="Gene_NCBI_Accession_number" placeholder="NCBI Accession number" class="form-control">
            </div>
            <div class="form-group">
            <label class="control-label">locus tag</label>
            <input type="text" name="Gene_name_locus_tag" placeholder="locus tag" class="form-control">
            </div>

            </th>

            <td>
            <div class="container">
            <div class="row">
            <div class="col-md-8 col-md-offset-2">
            <form action="insert1.php" method="post" enctype="multipart/form-data">
            <div class="form-group">
            <label class="control-label"><b>Antibiotic</label>
            <input type="text" name="Antibiotic_name" placeholder="Antibiotic name" class="form-control">
            </div>
            </td>
            <td>
            <div class="form-group">
            <label class="control-label"><b>Name</label>
            <input type="text" name="Name" placeholder="Name" class="form-control" required>
            </div>
            </td>
          </tr>
          <tr>
              <td rowspan="2">
        <h4> Please fill in information for other related data or give your comments if any</h4>
        <br>
        <textarea rows="7" cols="100" name="comment" form="usrform">
        Enter text here...</textarea>

              </td>
              <td>
                  </div>
                                         <div class="form-group">
                                <label class="control-label"><b>Protein function</label>
                                <input type="text" name="Protein_function" placeholder="Protein_function" class="form-control">
                            </div>
                            <div class="form-group">
                                <label class="control-label">Biological process</label>
                                <input type="text" name="Biological_process" placeholder="Biological process" class="form-control">
                            </div>

              </td>

          </tr><br>
            <br>
            <br>
            <br>
            <br>
            <br><input type="submit" value="Add Records">
                 </form></table>

        <?php include 'footer.php'; ?>
        </body>
        </html>

        <?php
        /* Attempt MySQL server connection. Assuming you are running MySQL
        server with default setting (user 'root' with no password) */
        $link = mysqli_connect("abc", "abc", "abc", "abc");

        // Check connection
        if($link === false){
            die("ERROR: Could not connect. " . mysqli_connect_error());
        }

        // Escape user inputs for security
        $Gene_NCBI_Accession_number = mysqli_real_escape_string($link, $_REQUEST['Gene_NCBI_Accession_number']);
        $Gene_name_locus_tag = mysqli_real_escape_string($link, $_REQUEST['Gene_name_locus_tag']);
        $Antibiotic_name = mysqli_real_escape_string($link, $_REQUEST['Antibiotic_name']);
        $Name = mysqli_real_escape_string($link, $_REQUEST['Name']);
        $Protein_function = mysqli_real_escape_string($link, $_REQUEST['Protein_function']);
        $Biological_process= mysqli_real_escape_string($link, $_REQUEST['Biological_process']);


        // attempt insert query execution
        $sql = "INSERT INTO contact_form_info (Gene_NCBI_Accession_number, Gene_name_locus_tag, Antibiotic_name, Name, Protein_function, Biological_process) VALUES ('$Gene_NCBI_Accession_number', '$Gene_name_locus_tag', '$Antibiotic_name', '$Name','$Protein_function', '$Biological_process' )";
        if(mysqli_query($link, $sql)){
            #echo "Records added successfully.";
        } else{
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
        }

        // close connection
        mysqli_close($link);
        ?>

关于php - 带有php的HTML表单不能与单个提交按钮一起使用。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51517172/

10-11 01:21