[英]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>
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.