0

In the following code, the problem is that the value of hidden input field (named "res_id") will be fixed to the first row of the table. Knowing that I am running a foreach loop here to generate the value of res_id fields, I am expecting to have unique values as per every single time the foreach loop starts over. Please note that the table data field or <td> renders a correct value. In other words, if I try to echo the POST value of res_id in the action php page, it will remain the same value! hope I am clear. Your help is very much appreciated.

    <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>
Ali
  • 216
  • 2
  • 11
  • problem is you have multiple elements with the same name! Make it `name='res_id[]'`! – Jeff Jan 21 '17 at 17:05
  • more info: http://stackoverflow.com/questions/4688880/html-element-array-name-something-or-name-something – Jeff Jan 21 '17 at 17:05

1 Answers1

0

include a row counter in your foreach loop or use an unique id for every row from another source

$i_rowcount++;

name the input field like this, so you have the row id in your post values as array-index-key.

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

the result will be that you have an array when you click post that looks like this

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

and in case you want to fix the values of your select-field, do it the same 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>';
Bernhard
  • 1,852
  • 11
  • 19
  • I see the point here. That makes a perfect sound. I just need a little help trying to figure out how to fetch the value of this dynamically named input field 'name="resid[$i_rowcount]"' in the action page. I tried what I could think of, but with no luck! Thanks... @Bernhard – Ali Jan 22 '17 at 08:21
  • if you have any other specific questions, let me know! thanks for the rating! – Bernhard Jan 23 '17 at 21:19