如何在PHP中的单个数组中获取表单字段

时间:2015-07-03 13:19:36

标签: php arrays forms multidimensional-array

我有一张表格

<form name="frm_passport" id="frm_passport" action="" method="POST">
  <input name="ws[surname]" id="surname" type="text" value="<?php echo (isset($_POST['ws']['surname']) && $_POST['ws']['surname'] != '') ? $_POST['ws']['surname'] : ''; ?>"/>
  <input name="ws[firstname]" id="firstname" type="text" value="<?php echo (isset($_POST['ws']['firstname']) && $_POST['ws']['firstname'] != '') ? $_POST['ws']['firstname'] : ''; ?>"/> 
  <input name="ws[prevname]" id="prevname" type="text" value="<?php echo (isset($_POST['ws']['prevname']) && $_POST['ws']['prevname'] != '') ? $_POST['ws']['prevname'] : ''; ?>"/>

    

这是表单提交后的代码

<?php
if (isset($_POST['Submit']) && $_POST['Submit'] != '') {
$_POST['ws']['surname'] = (isset($_POST['ws']['surname']) && $_POST['ws']['surname']!="")?$_POST['ws']['surname']:'';
$_POST['ws']['firstname'] = (isset($_POST['ws']['firstname']) && $_POST['ws']['firstname']!="")?$_POST['ws']['firstname']:'';
$_POST['ws']['prevname'] = (isset($_POST['ws']['prevname']) && $_POST['ws']['prevname']!="")?$_POST['ws']['prevname']:'';

echo $_POST['ws'];
  }
?>

在上面的代码中,echo返回一个数组。

在提交以下表单时,如何获得与$_POST['ws']相同的数组格式。那就是我想在提交时获取数组中的所有表单字段。

 <form name="frm_passport" id="frm_passport" action="" method="POST">
  <input name="surname" id="surname" type="text" />
  <input name="firstname" id="firstname" type="text"/> 
  <input name="prevname" id="prevname" type="text"/>
  <input type="submit" name="Submit" value="Submit" id="Submit" />
</form>

我不太了解数组。我是PHP的新手。请有人帮帮我。提前致谢

1 个答案:

答案 0 :(得分:1)

您需要更改输入字段的名称,如下所示

<form name="frm_passport" id="frm_passport" action="" method="POST">
    <input name="ws[surname]" id="surname" type="text" />
    <input name="ws[firstname]" id="firstname" type="text"/> 
    <input name="ws[prevname]" id="prevname" type="text"/>
    <input type="submit" name="Submit" value="Submit" id="Submit" />
</form>