如何使用codeigniter在post中发送数组

时间:2013-03-21 07:40:36

标签: php javascript codeigniter

我已根据以下内容在codeigniter框架中发送了数组:

for (var i=1; i<=selected; i++) {
           <div style="float: left; padding-left: 13px; padding-right: 12px; padding-top: 7px; margin-top: 0px;">'+i+'
</div>
<input type="text" name="unitName[]" id="unitName'+i+'" style="width:189px;" required />
<input type="text" name="ownerName[]" id="ownerName'+i+'" style="width:241px;" />
<input type="text" name="salutation[]" id="salutation'+i+'" style="width:137px;" /><br />
       }

当我尝试使用以下内容发布时:

$ownerNames = $this->input->post('ownerName');
if (is_array($ownerNames)) {
    foreach( $ownerNames as $ownerName ) {
    echo "Owner Name is : " . $ownerName;
    }
} else {echo "Owner is not array";}

这是我的整个控制器,所有帖子都是:

if ($this->form_validation->run() == FALSE) {
                    $this->load->view('newblock');
                } else {
                    $registrarName = $this->input->post('registrarName');
                    $blockName = $this->input->post('blockName');
                    $serviceType = $this->input->post('serviceType');
                    $number = $this->input->post('number');
                    $email = $this->input->post('email');
                    $address1 = $this->input->post('address1');
                    $address2 = $this->input->post('address2');
                    $address3 = $this->input->post('address3[]');
                    $town = $this->input->post('town');
                    $postCode = $this->input->post('postCode');
                    $blockUnits = $this->input->post('blockUnits');


                    echo print_r($_POST);

                    $unitNames = $this->input->post('unitName', TRUE);
                    echo $unitNames[0].'<br />';
                    if (is_array($unitNames)) {
                        foreach( $unitNames as $unitName ) {
                        echo "unit Name is : " . $unitName;
                        }
                    } else {
                        echo "unit is not array";
                    }


                    $ownerNames = $this->input->post('ownerName', TRUE);
                    echo $ownerNames[0].'<br />';
                    if (is_array($ownerNames)) {
                        foreach( $ownerNames as $ownerName ) {
                        echo "Owner Name is : " . $ownerName;
                        }
                    } else {
                        echo "Owner  is not array";
                    }


                    $salutations = $this->input->post('salutation', TRUE);
                    echo $salutations[0].'<br />';
                    if (is_array($salutations)) {
                        foreach( $salutations as $salutation ) {
                        echo "salutation is : " . $salutation;
                        }
                    } else {
                        echo "salutation  is not array";
                    }

显示“所有者不是数组”;表示数组是空的,经过使用print_r发现的调试后,该数组确实是空的,没有发布任何内容......

1 个答案:

答案 0 :(得分:6)

试试这个

测试控制器

  $ownerNames = $this->input->post('ownerName');
  if (is_array($ownerNames)) {
    foreach ($ownerNames as $ownerName => $k) {
      echo "Owner Name is : " . $k . "<br/>";
    }
  } else {
    echo "Owner is not array";
  }

和测试视图

<form method="post">
  <input type="text" name="ownerName[]" />
  <input type="text" name="ownerName[]" />
  <input type="text" name="ownerName[]" />
  <input type="submit" value="ownerName" />
</form>

//输出

Owner Name is : name1
Owner Name is : name2
Owner Name is : name3