2015-03-31 26 views
0

我必須提交具有動態列的codeIgniter表單。我如何使用循環從控制器中獲取它們。CodeIgniter:用於獲取包括圖像的值的控制器

這是我的看法頁

<?php echo form_open_multipart('main_controller/do_insert');?> 
<div id="mainDiv"> 
<div><input type="text" name="name0"/></div> 
<div><input type="file" name="img0"/></div> 
</div> 
<input type="button" value="add an entry" onClick="add('0')"> 
<input type="submit" value="save all"/> 
<?php from_close();?> 

<script> 
function add(x) 
{ 
var count=x;count++; 
var str1="<div><input type='text' name='name"+count+"'/></div>" 
var str2="<div><input type='file' name='img"+count+"'/></div>" 
var str3="<input type='button' value='add an entry' onClick='add(`1`)'>"; 
$("#mainDiv").append(str1+str2+str3); 
} 
</script>  

注:「x」的值可能不正確,但它不是有關在這裏工作,可以通過一個全局變量解決它。

我的問題是,如何獲取包括圖像在內的所有值的代碼控制器,並在提交表單時將其保存到表中。

+0

使用Ajax和重定向到所需的控制器 – Ghostman 2015-03-31 06:25:00

+0

糾正這種'from_close ();'as' form_close()'。 – Manish 2015-03-31 06:37:06

回答

1

我的建議, 使用文件名作爲數組字段。

<?php echo form_open_multipart('main_controller/do_insert');?> 
    <div id="mainDiv"> 
    <div class='group'> 
     <div><input type="text" name="name[]"/></div> 
     <div><input type="file" name="img[]"/></div> 
    </div> 
    </div> 
    <input type="button" id="add an entry"> 
    <input type="submit" value="save all"/> 
    <?php from_close();?> 

腳本的樣子..

<script> 
$('#add an entry').on('click', function() { 
     var cloneRow = $('.group').clone(); 
     cloneRow.find("input").val("").end(); 
     cloneRow.appendTo('#mainDiv'); 
</script> 

然後控制器:

foreach($this->input->post() as $post){ 
//do action on your data...here 
} 
foreach($_FILES['userfile'] as $key => $value) 
{ 
//images data 
} 
0
$i = 0; 
foreach($this->input->post() as $post) { 
      echo $post; //Or echo $this->input->post('name'.$i); 
      print_r($_FILES['img'.$i]); 
      $i++; 
     } 
+0

雖然這可能會回答這個問題,但在回答中插入一些文字以解釋您所做的事情總是一個好主意。閱讀[如何寫出一個好答案](http://stackoverflow.com/help/how-to-answer)。 – jurgemaister 2015-03-31 07:31:23

+0

好的,謝謝@jurgemaister – Manish 2015-03-31 07:46:06

相關問題