javascript 如何使用codeigniter在帖子中发送数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15541903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to send array in post using codeigniter
提问by Hunain
i have sent arrays in codeigniter framework according to the following:
我已经根据以下内容在 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 />
}
when i try to post it using following:
当我尝试使用以下内容发布它时:
$ownerNames = $this->input->post('ownerName');
if (is_array($ownerNames)) {
foreach( $ownerNames as $ownerName ) {
echo "Owner Name is : " . $ownerName;
}
} else {echo "Owner is not array";}
this is my entire controller, with all the posting:
这是我的整个控制器,所有的帖子:
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";
}
it is displaying "Owner is not an array"; which indicates the array is empty, after debugging i found by using print_r, that array was indeed empty, nothing was posted...
它显示“所有者不是数组”;这表明数组为空,调试后我使用print_r发现该数组确实为空,没有发布任何内容...
回答by Dino
try this
试试这个
test controller
测试控制器
$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";
}
and test view
和测试视图
<form method="post">
<input type="text" name="ownerName[]" />
<input type="text" name="ownerName[]" />
<input type="text" name="ownerName[]" />
<input type="submit" value="ownerName" />
</form>
// Output
// 输出
Owner Name is : name1
Owner Name is : name2
Owner Name is : name3