2014-09-26 62 views
1

所以我有这个程序,用户建立一个数据库表。首先,我问他们想要多少领域。获取多个环文本框的值

first.php

<html> 
<form name="formCreateFields" method="post" align="center" action="second.php"> 
    <p>Number of fields: &nbsp; <input type ="text" name="fieldsNum"/> &nbsp; 
    <input type="submit" name="submitFieldsNum" value=" Submit "/></p><br> 
</form> 
</html> 

然后我循环中的字段,取决于它们的输入的上方。

second.php

<?php 

echo "<form name='formSetupFields' method='post' align='center' action='third.php'>"; 

for ($z=1; $z<=$_POST['fieldsNum']; $z++) { 
    echo "<table align='center'> 
     <tr> 
      <th rowspan=2> <big> $z </big> &nbsp; &nbsp;</th> <th>Name</th> <th>Type</th> <th>Length</th> 
     </tr> 
     <tr> 
      <td><input type='text' name='fieldName$z'></td> 
      <td><input type='text' name='fieldType$z'></td> 
      <td><input type='text' name='fieldLength$z'></td> 
     </tr> 
     </table><br><br>"; 
} 

<input type='submit' name='submitFieldSetup'> 
</form>"; 
?> 

我在这之后的问题。我一直试图通过将它们放在一个数组中并使用foreach来查看它们,但似乎无法获取任何地方。我认为可以使用类似$_POST['fieldName$z']的东西,但我想我错了。

我只需要找出如何获取第二个文件中的所有输入。有任何想法吗?提前致谢! :)

回答

1

如果fieldsNum是一个数字,for应该罚款在这种情况下,只要适当的串联值:

echo "<form name='formSetupFields' method='post' align='center' action='third.php'>"; 
    echo "<table align='center'>"; 
     echo ' 
     <tr> 
      <th rowspan=2></th> <th>Name</th> <th>Type</th> <th>Length</th> 
     </tr>'; 
     for ($z = 1; $z <= $_POST['fieldsNum']; $z++) { 
      echo " 
       <tr> 
        <td><input type='text' name='inputs[$z][name]'></td> 
        <td><input type='text' name='inputs[$z][type]'></td> 
        <td><input type='text' name='inputs[$z][length]'></td> 
       </tr> 
       "; 

     } 
    echo '</table><br><br>'; 
    echo "<input type='submit' name='submitFieldSetup'>"; 
echo '</form>'; 
third.php

即可;

if(isset($_POST['inputs'])) { 
    $inputs = $_POST['inputs']; 
    foreach($inputs as $input) { 
     echo $input['name']; 
     echo $input['type']; 
     echo $input['length']; 
    } 
} 
+0

哦,谢谢你的正确连接。但是在这之后我将如何获取输入? – Suika 2014-09-26 07:31:48

+0

@Suika然后,就像一个表单提交,'action =“third.php”',最有可能的输入将会去那里 – Ghost 2014-09-26 07:32:54

+0

是的,但我希望得到foreach循环显示所有输入的帮助。这就是我卡住的地方。 – Suika 2014-09-26 07:42:14