简体   繁体   English

将输入类型的文本放在数组中

[英]put input type text in an array

I have some text inputs . 我有一些text inputs The inputs are produced from a while loop. inputswhile循环产生。 Now I want put the values in an array. 现在,我想将values放入数组中。 Like this: 像这样:

array       values
mark['0']   input1
mark['1']   input2
mark['2']   input3

I tried this but not working. 我尝试了这个,但是没有用。

while($row=mysql_fetch_array($result)){
  <form class="form1" name="form1" method="post">
    <input type="text" name="mark[]"/>
  </form>
}
<form class="form1" name="form1" method="post">
    <button type="submit" name="correction"></submit>
</form>

And then 接着

if(isset($_POST['correction'])){
$grade=0;   
    $mark=$_POST['mark'];
    foreach($mark as $key =>$value ){
        $grade+=$value;
    }   
print $grade;
}

I get these errors: 我得到这些错误:

Notice: Undefined index: mark in C:\xampp\htdocs\virtual_exam\handy_correction.php on line 37

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\virtual_exam\handy_correction.php on line 38

The problem may be caused by two forms which arent connected to each other and if YES, how to joined them? 问题可能是由于两种形式相互连接造成的,如果是,如何将它们合并? If NO, how to do what i want? 如果没有,我该怎么办?

You should likely have only 1 form element, not one for every row you are trying to output, and certainly not a separate one for the form submission button. 您可能应该只有1个form元素,而您要输出的每一行都不应该有一个,当然也没有一个单独的供forms提交按钮使用的元素。

Your problem is that the actual form you are submitting has only one element in it - the submit button. 您的问题是您要提交的实际表单中只有一个元素-提交按钮。 Thus there are no input fields at all to post. 因此,根本没有要发布的输入字段。

You should generate your form like this: 您应该这样生成表单:

<form class="form1" name="form1" method="post">
<?php
while($row=mysql_fetch_array($result)){
?>
    <input type="text" name="mark[]"/>
<?php
}
?>
    <button type="submit" name="correction"></submit>
</form>

Change your form to this: 将您的表单更改为此:

<form class="form1" name="form1" method="post">

  <?php
    while ($row = mysql_fetch_array($result)) {
      echo '<input type="text" name="mark[]" />';
    }
  ?>

  <input type="submit" name="correction" value="Submit" />
</form>

And then: 接着:

if (isset($_POST['correction'])) {
  $grade = 0;   
  $mark  = $_POST['mark'];

  foreach ($mark as $key => $value) {
    $grade += $value;
  }

  echo $grade;
}

What you saying the last paragraph is correct, you are submitting the form1 which contains only the submit button, so mark doesn't exist in the PHP script that handles the POST. 您所说的最后一段是正确的,您正在提交仅包含提交按钮的form1,因此在处理POST的PHP脚本中不存在mark

so change the HTML to: 因此,将HTML更改为:

<form class="form1" name="form1" method="post">
<?php
while($row=mysql_fetch_array($result)){
?>
    <input type="text" name="mark[]"/>
<?php
}
?>
    <button type="submit" name="correction"></submit>
</form>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM