简体   繁体   English

我正在尝试提交一些表格,但是提交的值仅属于第一个

[英]I am trying to submit a form out of a few but the value get submitted only belongs to the first one

I am retriving some information from the database to represent courses as you can see in the image . 我正在从数据库中检索一些信息,以代表课程,如您在图中看到的。 Then I want to provide the user with a button to click on for more information . 然后,我想为用户提供一个按钮,以单击以获取更多信息。 In order to know which post is clicked on I have created a form and I put the course id inside and then I submit that form . 为了知道单击哪个帖子,我创建了一个表格,将课程ID放在里面,然后提交该表格。 But the problem is once I submit I get the same id for all the posts which is the very first 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>

Any idea how can I fix that ? 知道我该如何解决吗? 在此处输入图片说明

The problem is appropriate as in while loop evertime you are populating the data with the same id (form) , to resolve , create a variable in php let's say $formId = 0 and intialise with 0 and evertime increment it with while loop 这个问题是适当的,因为在while循环中,每次您使用相同的id(form)填充数据,以解决问题,在php中创建一个变量,比如说$ formId = 0并以0初始化,然后再通过while循环对其进行递增

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

Since all your forms have the same id, javascript will pick the first one up and submit that. 由于您所有的表单都具有相同的ID,因此javascript会选择第一个表单并将其提交。

You will have to give them all different id's and then submit the one user clicks to get the desired result. 您必须为他们提供所有不同的ID,然后提交一个用户点击以获取所需的结果。

Example : Your echo would change to give different IDs to different forms and an argument to the function. 示例:您的回声将发生变化,以使不同的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>' ...

And the script will change accordingly : 脚本将相应地更改:

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

PS - This might not be the best way to do it, but a good start. PS-这可能不是最好的方法,但是是一个好的开始。

You must be have an unique id for your forms: 您的表单必须具有唯一的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>

Every element must have a unique ID or none at all. 每个元素都必须具有唯一的ID或根本没有。 If you remove the ID you would need a different approach to finding the particular form. 如果删除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>

Using javascript is one solution - you could have a submit button within each form styled to resemble plain text. 使用javascript是一种解决方案-您可以在每个表单中都设置一个提交按钮,样式类似于纯文本。 Alternatively, you could have a single form and use javascript to set the values prior to submission. 或者,您可以使用一个表单,然后使用javascript在提交之前设置值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在我的弹出窗口中提交表单并获取已提交的价值 - Trying to submit a form in my popup and get value that was submitted 获取没有提交按钮的已提交表单的价值 - Get value of a submitted form without submit button 表单提交后,如何检查提交的整数值? - After form submit how can i check submitted value integer or not? 表单仅检查提交功能的第一个值? - Form only checking first value on submit function? 从函数中传递的提交表单中获取字段值 - Get field value out of submitted form passed in functions 表单只能使用“提交”按钮提交 - Form can only be submitted with Submit button 我正在尝试使用表单选择选项获取当前日期,并在提交后显示接下来的7天 - I am trying to get current date with form select option and display next seven days on submit 使用单独的提交按钮呈现多个表单,但仅提交第一实例的表单变量 - Rendering multiple forms with individual submit buttons but only form variables of first instance is being submitted 我无法在提交时将值表单JSP页面获取到Controller类 - I am not able to get the value form JSP page to Controller Class on Submit PHP foreach 打印表单 | 所有表单都提交给一个ajax代码,但只有第一个表单有效 - PHP foreach prints out forms | all forms are submitted to one ajax code, but just the first form works
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM