我试图提交一个表格,但提交的价值仅属于第一个

时间:2017-05-29 06:30:51

标签: javascript php html forms form-submit

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

"alpha"

知道如何解决这个问题? enter image description here

4 个答案:

答案 0 :(得分:2)

问题是合适的,因为在while循环中你用相同的id(表格)填充数据,解析,在php中创建一个变量让我们说$ formId = 0并且intialise为0并且evertime用while循环增加它

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

答案 1 :(得分: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 :(得分:1)

您的表单必须具有唯一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>

答案 3 :(得分:1)

每个元素必须具有唯一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在提交之前设置值。