2017-06-01 38 views
0

我有形式顯示圖像和使用foreach輸入,並希望得到它在表單提交的價值,所以我可以implode它並保存到數據庫,但不知道我怎麼能做到這一點。到目前爲止,我在表單上提交的唯一值是最後一個字段的值。這裏是我的代碼顯示形式:Implode在表單中提交所有輸入值

<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>"> 
    <div class="row"> 
     <div class="col-md-12 col-xs-12"> 

     <?php 
      # Prepare the SELECT Query 
      $productimage = explode ("|", $productimage); 
      foreach ($productimage as $imgfile) { 
       echo '<div class="form-group"> 
          <div class="col-md-8 col-sm-8 col-xs-12"> 
           <div class="product-img"> 
            <img class="img-thumbnail" src="../content/uploads/' . $imgfile . '" width="120" height="120" /> 
            <input class="form-control" type="text" name="imgtitle"> 
            <input type="hidden" name="imgurl" value="' . $imgfile . '"> 
           </div> 
          </div> 
         </div>'; 
      } 
     ?> 

     <div class="form-group"> 
      <div class="col-md-12 col-sm-12 col-xs-12"> 
       <button type="submit" class="btn btn-primary" name="btn-saveimg">Save</button> 
      </div> 
     </div> 

     </div> 
    </div> 
</form> 

在表單提交,我要使用此代碼來獲取POST值:

if (isset($_POST['btn-saveimg'])) { 

    $imgurlUpdate = $_POST['imgurl']; 
    $imgtitleUpdate = $_POST['imgtitle']; 

    $arr = array($imgurlUpdate,'#',$imgtitleUpdate); 

    print_r($arr); 
} 

上的print_r,唯一的結果我得到的是表單的最後一個值。

Array ([0] => image4.jpg [1] => # [2] => title4) 

我試圖用破滅實現這樣的結果:

image1.jpg#title1|image2.jpg#title2|image3.jpg#title3|image4.jpg#title4 

,我可以保存到數據庫中,如果例如我有4個圖像。任何想法?

+0

你的print_r()輸出? –

+0

嘿@MuhammadUsman謝謝你的提問。我只是更新了我的問題,並添加print_r() – RaffyM

+0

的輸出print_r($ _ POST)的輸出嗎? –

回答

1

我認爲問題出在您使用的輸入標籤中,您正在存儲多個imgurl & imgtitle。你應該將數組中的名稱值存儲起來。

'<div class="form-group"> 
         <div class="col-md-8 col-sm-8 col-xs-12"> 
          <div class="product-img"> 
           <img class="img-thumbnail" src="../content/uploads/' . $imgfile . '" width="120" height="120" /> 
           <input class="form-control" type="text" name="imgtitle[]"> 
           <input type="hidden" name="imgurl[]" value="' . $imgfile . '"> 
          </div> 
         </div> 
        </div>';' 

當您獲取php方提取值的值作爲數組元素。 我希望這可以幫助。