php - 如何在 codeigniter 中发布动态输入标签

标签 php javascript html codeigniter

我正面临一个严重的困境,想知道这怎么可能。我有一个内置于 codeigniter 框架的表单。表单末尾是一个输入标签,它接受一个数字并附加一个新表单,其中包含输入标签的确切行作为最后一个输入标签的值。我已经使用 javascript 完成了这个,代码是

$('#formName9').on('change', function() {
       var selected = $(this).val();
       $('#subForm').empty();
       $("#subForm").append('<table border="" style="border:none; background:#f2f2f1; margin-bottom:10px; border-collapse: collapse; margin-top: 25px; box-shadow: 9px 4px 8px rgba(50, 50, 50, 0.75);" cellspacing="0" cellpadding="0">            <tr>              <td style="text-align: center; width: 30px; border-width: 0px;"><img src="<?php echo  base_url(); ?>assets/img/icon_house.png" ></td>              <td style="text-align: center; border-width: 0px;"><img src="<?php echo  base_url(); ?>assets/img/table_divider.png" /></td>              <td style="text-align: center; width: 200px; border-width: 0px;"><strong>Unit Name</strong></td>              <td style="text-align: center; border-width: 0px;"><img src="<?php echo  base_url(); ?>assets/img/table_divider.png" /></td>              <td style="text-align: center; width: 250px; border-width: 0px;" ><strong>Owner Name</strong></td>              <td style="text-align: center; border-width: 0px;"><img src="<?php echo  base_url(); ?>assets/img/table_divider.png" /></td>              <td style="text-align: center;width: 150px; border-width: 0px;"><strong>Owner Salutation</strong></td> </tr> </table>');
       for (var i=1; i<=selected; i++) {
           $('#subForm').append('<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'+i+'" id="ownerName'+i+'" style="width:241px;" /><input type="text" name="salutation'+i+'" id="salutation'+i+'" style="width:137px;" /><br />');
       }
    });

你可以看到我根据循环对所有附加标签使用了不同的命名约定。

现在需要发布这些值以便 PHP[SERVER] 做进一步的工作,但是我将如何发布这些动态值?我尝试了以下操作,但这只会挂起我的本地服务器,不会再继续了

$ownerName = array();
                    for ($i=0; $i<=$blockUnits ; $i+1) { 
                        $ownerName[$i] = "ownerName".$i;
                        $this->input->post('ownerName[$i]');
                    } 

最佳答案

在添加新的输入文本框时,为这些字段命名,如下所示。

例如,如果用户想添加 5 个字段,那么输出将变成这样

<input type="text" name="ownerName[]" />
<input type="text" name="ownerName[]" />
<input type="text" name="ownerName[]" />
<input type="text" name="ownerName[]" />
<input type="text" name="ownerName[]" />

然后在 PHP 中,您可以获得这些字段的值,例如

$ownerNames = $_POST['ownerName'];

foreach( $ownerNames as $ownerName ) {
    echo "Owner Name is : " . $ownerName;
}

当您在 CodeIgniter 中尝试时,代码看起来像这样(获取 post 变量)

$ownerNames = $this->input->post('ownerName');

foreach( $ownerNames as $ownerName ) {
    echo "Owner Name is : " . $ownerName;
}

关于php - 如何在 codeigniter 中发布动态输入标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15540362/

相关文章:

php - Highcharts 3.0 版本更改导出选项?

php - 将字符串转换为可执行表达式 PHP?

javascript - jquery获取元素的某些类名,该类名分配了多个类

PHP-Mysql查询: Display number of orders by day and store in variables

php - PHP 中的静态类是不好的做法吗?

javascript - 从时间表中删除休息时间

javascript - Jquery查找具有相同属性值的所有div并将它们的id值加在一起

html - 如何设置管理面板的内容区域占据剩余宽度

javascript - 制作幻灯片旋转

html - 在包装器中并排放置 2 个 div 时遇到问题