dqk94069 2017-01-21 16:46
浏览 44
已采纳

在表单内的表中发布php数据

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>
  • 写回答

1条回答 默认 最新

  • doushouhe7072 2017-01-21 17:07
    关注

    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>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败