【问题标题】:I am trying to submit a form out of a few but the value get submitted only belongs to the first one我正在尝试从几个表单中提交一个表单,但提交的值仅属于第一个
【发布时间】:2017-10-29 08:59:39
【问题描述】:

我正在从数据库中检索一些信息来表示课程,如图所示。然后我想为用户提供一个按钮来单击以获取更多信息。为了知道点击了哪个帖子,我创建了一个表单,并将课程 ID 放入其中,然后提交该表单。但问题是,一旦我提交,我会为所有帖子获得相同的 id,这是第一个 id

<?php 
       $name = $_SESSION["uname"];              
       $sql = "SELECT * FROM `courses` where course_lec = '$name'";
       $result = $conn->query($sql);
       if ($result->num_rows > 0) {
           while($row = $result->fetch_assoc()) {
              echo '
                     <div class="col-md-4">
                     <span class="fa-stack fa-4x">
                     <i class="fa fa-circle fa-stack-2x text-primary"></i>
                     <i class="fa fa-shopping-cart fa-stack-1x fa-inverse"></i>
                     </span>
                     </br>
                    <a href="javascript: functionTest()">Search</a>
                   <form name="myform" id="form"  action="checkMarks.php" method="post">
                    <input type= "text" value ="'. $row["course_id"].' " name="test"></input>
                    </form>
                    </br>
                    </div>';

                             }

                    }

?>
                <script >
                    function functionTest() {

                         document.getElementById("form").submit();

                            }


                </script>

知道如何解决这个问题吗?

【问题讨论】:

  • 每个表单都需要有一个唯一的 id - 或者根本没有。 javascript 函数只会根据它的 id 找到第一个表单......你需要一种不同的方法
  • 答案已经很对了。检查一下。如果可以的话,我对您的建议是将表格与您的课程分开。然后当用户点击时,使用 javascript 或 jquery 填充表单然后提交。

标签: javascript php html forms form-submit


【解决方案1】:

由于您所有的表单都有相同的 id,javascript 会选择第一个并提交。

您必须为他们提供所有不同的 id,然后提交一个用户点击以获得所需的结果。

示例:您的 echo 会发生变化,为不同的形式提供不同的 ID,并为函数提供一个参数。

... '<a href="javascript: functionTest('.$row["course_id"].')">Search</a>
   <form name="myform" id="form'.$row["course_id"].'"  action="checkMarks.php" method="post">
      <input type= "text" value ="'. $row["course_id"].' " name="test"></input>
   </form>' ...

脚本也会相应改变:

<script >
    function functionTest(course_id) {
        document.getElementById("form"+course_id).submit();
    }
</script>

PS - 这可能不是最好的方法,但却是一个好的开始。

【讨论】:

    【解决方案2】:

    这个问题是适当的,因为在 while 循环中,您每次使用相同的 id(表单)填充数据,要解决,在 php 中创建一个变量,假设 $formId = 0 并用 0 初始化,并且每次使用 while 循环递增它

    $formId = 0
        while(){
    echo"<form id="'.$formId.'" /form>"
    $formId++;
    }
    

    【讨论】:

      【解决方案3】:

      您的表单必须具有唯一 ID:

      <?php 
          $name = $_SESSION["uname"];              
          $sql = "SELECT * FROM `courses` where course_lec = '$name'";
          $result = $conn->query($sql);
          if ($result->num_rows > 0) {
              $form_id = 1;
              while($row = $result->fetch_assoc()) {
                  echo '
                  <div class="col-md-4">
                  <span class="fa-stack fa-4x">
                  <i class="fa fa-circle fa-stack-2x text-primary"></i>
                  <i class="fa fa-shopping-cart fa-stack-1x fa-inverse"></i>
                  </span>
                  </br>
                  <a href="javascript: functionTest()">Search</a>
                  <form name="myform" id="form-'.$form_id++.'"  action="checkMarks.php" method="post">
                  <input type= "text" value ="'. $row["course_id"].' " name="test" />
                  </form>
                  </br>
                  </div>';
      
              }
          }
      ?>
      <script >
          function functionTest() {
              document.getElementById("form").submit();
          }
      </script>
      

      【讨论】:

        【解决方案4】:

        每个元素都必须有唯一的 ID 或根本没有。如果您删除 ID,您将需要一种不同的方法来查找特定表单。

        <?php 
               $name = $_SESSION["uname"];              
               $sql = "SELECT * FROM `courses` where course_lec = '$name'";
               $result = $conn->query($sql);
               if ($result->num_rows > 0) {
                   while($row = $result->fetch_assoc()) {
                      echo '
                        <div class="col-md-4">
                            <span class="fa-stack fa-4x">
                                <i class="fa fa-circle fa-stack-2x text-primary"></i>
                                <i class="fa fa-shopping-cart fa-stack-1x fa-inverse"></i>
                            </span>
        
                            </br>
        
                            <a href="javascript: functionTest()">Search</a>
                            <!-- remove the ID -->
                            <form name="myform" action="checkMarks.php" method="post">
                                <input type= "text" value ="'. $row["course_id"].' " name="test"></input>
                            </form>
                            </br>
                        </div>';
        
                    }
               }
        
        ?>
        
        <script>
            function functionTest( event ){
                try{
                    var a=event.target;
                    var parent = a.parentNode;/* this will be the containing DIV */
                    /* search within container for a form */
                    var form=parent.querySelectorAll('form[name="myform"]')[0];
                    /* submit the data */
                    if( form )form.submit();
                }catch( err ){
                    alert(err)
                }
            }
        </script>
        

        使用 javascript 是一种解决方案 - 您可以在每个表单中设置一个提交按钮,其样式类似于纯文本。或者,您可以有一个表单并在提交之前使用 javascript 设置值。

        【讨论】:

          猜你喜欢
          • 2016-11-10
          • 1970-01-01
          • 2018-08-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-12
          • 2015-08-24
          • 1970-01-01
          相关资源
          最近更新 更多