将数据存储在php中的数组中,具有多个提交和eco自我

时间:2014-04-11 17:17:49

标签: php arrays forms submit self

尝试从多次提交中打印数据,我希望将名称和卷号打印/传递给另一个表单。这些数据将来自多个提交。在以下代码中,仅打印最后提交的数据。我哪里错了。

`

<?php
$name[] = $_POST['name'];
$rollno[] = $_POST['rollno'];
$arrlength=count($name);
for($x=0;$x<$arrlength;$x++)
  {
  echo $name[$x]." - - - - - -".$rollno[$x] ;
  echo "<br>";
  }
?>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
  <tr>
    <td>Name</td>
    <td><input type="text" name="name" id="textfield" /></td>
  </tr>
  <tr>
    <td>Roll No.</td>
    <td><input type="text" name="rollno" id="textfield2" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="button" id="button" value="Submit" /></td>
  </tr>
</table>
</form>

`

2 个答案:

答案 0 :(得分:0)

Your input types are not array type. Use LIKE THIS
 <input type="text" name="name[1]" id="textfield" />
 <input type="text" name="name[2]" id="textfield" />
 .
 .
 .
 <input type="text" name="name[n]" id="textfield" />

 IN PHP CODE
 foreach( $_POST['name'] as $v ) {
    echo $v;
  }

答案 1 :(得分:0)

想通了。这是修订后的代码。

<?php
$name = $_POST['name'];
$rollno = $_POST['rollno'];
$arrlength=count($name);
for($x=0;$x<$arrlength;$x++)
  {
  echo $name[$x]."--".$rollno[$x] ;
  echo "<br>";
  }
?>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
  <tr>
    <td>Name</td>
    <td><?php
    $arrlength1=count($name);
for($x=0;$x<$arrlength1;$x++)
  {
  echo "<input type='hidden' name='name[]' value='$name[$x]' /> " ;
  echo "<input type='hidden' name='rollno[]' value='$rollno[$x]' /> " ;
   }
        ?>
        <input type="text" name="name[]" id="textfield" /></td>
  </tr>
  <tr>
    <td>Roll No.</td>
    <td><input type="text" name="rollno[]" id="textfield2" /></td>
  </tr>
  <tr>
    <td></td>
    <td><input type="submit" name="button" id="button" value="Submit" /></td>
  </tr>
</table>
</form>