2017-01-21 91 views
0

在以下代碼中,問題是隱藏輸入字段(名爲「res_id」)的值將固定到表的第一行。知道我在這裏運行一個foreach循環來生成res_id字段的值,我期望每次foreach循環開始時都有唯一的值。請注意表格數據字段或<td>呈現正確的值。換句話說,如果我嘗試在動作php頁面中回顯res_id的POST值,它將保持相同的值!希望我清楚。非常感激你的幫助。在表格內張貼PHP數據

<div> 
<form id="form_st_feedback" class="form-horizontal" action="st_fdbk.php" method="post" onsubmit="return confirm('Are you sure?');"> 
     <table class="table table-bordered"> 
      <thead> 
       <th style="width:10%; padding:0px; margin:0px;">Res. ID</th> 
       <th style="width:10%; padding:0px; margin:0px;">Class Date</th> 
       <th style="width:10%; padding:0px; margin:0px;">Tutor</th> 
       <th style="width:15%; padding:0px; margin:0px;">How was your class?</th> 
       <th style="width:55%; padding:0px; margin:0px;">Your comment</th> 
      </thead>    
      <tbody> 
      <?php 
    $tt=null; 
    $rr = null; 
    $mypdo2 = Database::connect(); 
    $mypdo2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    $mysql3 = "SELECT * FROM tbl_reservation WHERE res_st_id = $user_id AND res_del=FALSE AND teacher_cant_mkit=FALSE ORDER BY res_date"; 
    $myq3 = $mypdo->prepare($mysql3); 
    $myq3->execute(array()); 
    $mydata3 = $myq3->fetch(PDO::FETCH_ASSOC); 



     foreach ($mypdo2->query($mysql3) as $row) { 
      $res_id=$row['res_id']; 
      $mysql4 = "SELECT * FROM tbl_st_page_feedbk WHERE fb_res_id = ? LIMIT 1"; // linking to feedback table 
      $myq4 = $mypdo2->prepare($mysql4); 
      $myq4->execute(array($res_id)); 
      $mydata4 = $myq4->fetch(PDO::FETCH_ASSOC); 

       $tt=$row['res_date'].$row['res_time']; 
       $str_tt0= date('m/d/Y H:i', strtotime($tt)); 
       $str_tt=strtotime($str_tt0); 
       if ( ($str_tt - $timenow<=-600)) { //10 mins after class started 
       echo '<tr>'; 
        echo '<td>'. $res_id .'</td>'; 
        echo "<input type='hidden' name='res_id' value='$res_id'>"; 
        echo '<td>'. $row['res_date'].'<br />'.$row['res_time'] .'</td>'; 
        if ($row['res_tut_id']==10) {echo '<td>' . 'Kim' . '</td>';} 
        if ($row['res_tut_id']==16) {echo '<td>' . 'Morly' . '</td>';} 
        if ($row['res_tut_id']==22) {echo '<td>' . 'Arlene' . '</td>';} 
          if ($row['st_rep_feedbk']=='NO') { 
          echo '<td>' .'<select name="selected_quality"style="color:blue;"> 
               <option value="Excellent">Excellent (5/5)</option> 
               <option value="Good">Good (4/5)</option> 
               <option value="Fair">Fair (3/5)</option> 
               <option value="Poor">Poor (2/5)</option> 
               <option value="Very Poor">Very Poor (1/5)</option> 
             </select>' 
           .'</td>'; 
          echo '<td>' 
             .'<textarea id="comment_txt" name="comment_txt" style="color:#00f; display:inline; flloat:left;" rows="2" maxlength="80" value=""></textarea>' 
             .'<br />' 
             .'<button id="submit" type="submit" class="btn btn-success" style="display:block; margin:2px auto; padding:2px 5px;">Submit</button>' 
            .'</td>'; 
          } 
          else { echo '<td>'.$mydata4['fb_quality'].'</td>'; 
            echo '<td>'.$mydata4['fb_comment'].'</td>'; 
            } 
       echo '</tr>'; 
      } // end of if 
      } //end of foreach 
      Database::disconnect(); 
      ?> 
      </tbody> 
     </table> 
    </form> 
    </div> 
+0

問題是你有多個同名的元素!讓它成爲'name ='res_id []''! – Jeff

+0

更多信息:http://stackoverflow.com/questions/4688880/html-element-array-name-something-or-name-something – Jeff

回答

0

包括行計數器在foreach循環或使用一個唯一的ID的每一行從另一個來源

$i_rowcount++; 

名稱輸入字段這樣的,所以你必須在你的帖子值的行ID作爲數組索引鍵。

<input type="hidden" name="resid[$i_rowcount]" value="your value"> 

結果將是,你有當你點擊後,看起來像這樣

resid 
1 => value of first row 
2 => value of second row 
... 

,並在情況下,你要修復您的選擇字段的值的數組,這樣做同樣的way

$a_options = array(1 => 'Very Good', 
        2 => 'Good', 
        3 => 'Average'); 

echo '<select name="selected_quality[$i_rowcount]"style="color:blue;">'; 
foreach($a_options as $i_key => $s_value){ 
echo '<option'.($_POST['selected_quality'][$i_rowcount] == $i_key ? ' selected="selected"' : '').' value="'.$i_key.'">'.$s_value.'</option>'; 
} 
echo '</select>'; 
+0

我明白了這一點。這使得一個完美的聲音。我只需要一點幫助,試圖找出如何在動作頁面中獲取這個動態命名的輸入字段'name =「resid [$ i_rowcount]」'的值。我嘗試了我能想到的,但沒有運氣!謝謝...... @Bernhard – Ali

+0

如果您有任何其他具體問題,請告訴我!感謝您的評價! – Bernhard