duan0714 2011-07-01 16:13
浏览 41
已采纳

克隆表单元素并返回模板文件

I am building a website, where on the click of a link you can clone the form elements, what I am wanting to know is that, when I send the $_POST to my controller and check that the information submitted is correct, how do I then return a template that has enough elements so that errors can be rectified, so example my original form looks like this,

<fieldset class="entry">
<label for="email_address">Email Address</label>
<input type="text" name="email_address[]" value="" class="text small"/> 
<label for="firstname">Firstname</label>
<input type="text" name="firstname[]" value="" class="text small"/>                     
<label for="surname">Surname</label>                            <input type="text" name="surname[]" value="" class="text small"/>
</fieldset>

how can I return the correct amound of fieldsets based on the $_POST?

  • 写回答

1条回答 默认 最新

  • doujian3132 2011-07-01 16:25
    关注

    To answer this:

    How can I return the correct amount of fieldsets based on the $_POST?

    If each fieldset only has one instance of each bracketed[] field name, you can just count how many were submitted (of any of the fields).

    $number_of_fieldsets = count((array) $this->input->post('email_address'));
    

    I've used $this->input->post() (since you're using CI) in case the value is not set (it will return false), you may use some isset() logic instead if you wish.

    I've cast to array here in case the return value of $this->input->post('email_address') is false (count will return 0) or for some reason, a string (count will return 1). This is just a mild attempt at being defensive, you will probably want to handle unexpected results with your own methods.

    Once again, it doesn't matter which field you choose to count.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题