-1

我有 3 个文本字段,只有一个提交按钮可以一次发送文本字段的所有数据。但是如何在 php 中一次发送所有三个文本字段的数据?

     <form>
     for($x=0;$x<3;$x++){
     <input type="text" name="name">
     }
     <input type="submit" name="submit">
     </form>

现在我在 for 循环中有三个字段,我必须使用单个提交按钮从所有字段中提取数据。那么我该怎么做呢?

4

3 回答 3

0

您将它们全部放在同一个中<form>,并确保它们具有不同的name属性值(或值以 结尾[])。

于 2013-07-30T16:33:29.157 回答
0

例如,在输入上使用“名称”属性允许您执行此操作

<form action='submit.php' method='post'>
    <input type='text' name='one'></input>
    <input type='text' name='two'></input>
    <input type='text' name='three'></input>
    <input type='submit' name='submit' value='Submit!' />
</form>

在你的 PHP 中你会做这样的事情

<?php
    if(isset($_POST['submit'])){
        $inputOne = $_POST['one'];
        $inputTwo = $_POST['two'];
        $inputThree = $_POST['three'];

        //Do whatever you want with them
    }
?>

有更好的方法可以做到这一点,但这可能是最容易理解的


如果您希望所有输入具有相同的名称,请执行此操作

<input type='text' name='textinput[]'></input>

改用它并像这样遍历所有输入

<?php
    foreach($_POST['textinput'] as $input){
        //do something with $input
    }
?>
于 2013-07-30T16:38:39.797 回答
0

我相信您正在寻找的是这个注意名称字段后面的 [ ]

<form>
   for($x=0;$x<3;$x++) {
      <input type="text" name="name[]" />
   }
   <input type="submit" name="submit" />
</form>

然后检索值

$names = $_POST['name'];
foreach( $names as $name ) {
   print $name;
}
于 2013-07-31T02:37:22.077 回答