iT邦幫忙

0

PHP table當按下radio取該列的值

各位大大好,我有設定一個rs陣列顯示各列資料,並且使用rs[4]存放各列ID,想請問我要怎麼讓此id識別當我按下radio取得該列資料POST到我另一個PHP檔呢?
我有使用此方式→https://ithelp.ithome.com.tw/questions/10184648
但是還是取不到rs[4]的值

<table">
     <thead>
                  <tr>
                    <th scope="col">課程名稱</th>
                    <th scope="col">課程分類</th>
                    <th scope="col">教師</th>
                    <th scope="col">時間</th>
                  </tr>
     </thead>
     <tbody>
        <?php
            for($i=1;$i<=5;$i++){
            $rs=mysqli_fetch_row($data);
         ?>
         <tr>
              <th scope="row"><?php echo $rs[0]?></th>
                <td><?php echo $rs[1]?></td>
                <td><?php echo $rs[2]?></td>
                <td><?php echo $rs[3]?></td>
                <td>
                //以下是當使用者按下訂閱圖樣則會換成顯示已訂閱圖樣
                 <div class="" data-toggle="buttons">
                          <label class="btn btn-primary active" for="option1Y">
                          <?php
                            echo '<input type="radio" name="subscription" id="option1Y" value="nosubscription" onclick=location.href="testhaha.php?id=$rs[4]">';
                           ?>
                           已訂閱
                         </label>

                         <label class="btn btn-warning" for="option2N">
                          <?php
                           echo '<input type="radio" name="subscription" id="option2N" value="subscription" onclick=location.href="testhaha.php?id=$rs[4]">';
                          ?>
                           <i class="fas fa-chalkboard-teacher"></i>訂閱
                         </label>
                       </div>
                  <div id="result" style="width:60px;height:60px;border:3px #cccccc dashed;"></div>
                </td>   
          </tr>
                   <?php
                 }
                 ?>
       </tbody>
</table>

AJAX程式碼

  <script>  
   $(document).ready(function(){  
    $('input[type="radio"]').click(function(){  
     var subscription = $(this).val();  
     $.ajax({  
      url:"testhaha.php",  
      method:"POST",  
      data:{subscription:subscription},  
      success:function(data){  
       $('#result').html(data);  
     }  
   });  
   });  
  });  
</script>

testhaha.php

<?php
$id = $_GET["id"];
echo "我是ID" . $id;
?>

顯示結果

我是ID$rs[4]

懇請各位大大解答,感謝

淺水員 iT邦大師 6 級 ‧ 2019-02-16 12:53:55 檢舉
先從瀏覽器的開發者工具去看看自己產生的HTML是不是有錯誤吧。
覺得 onclick 那邊應該有問題...
p39212053 iT邦新手 4 級 ‧ 2019-02-16 13:06:05 檢舉
onclick確定可以運作,顯示結果就是onclick之後運行的
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

1
海綿寶寶
iT邦大神 1 級 ‧ 2019-02-16 14:01:05
最佳解答

如果趕時間的話

echo '<input type="radio" name="subscription" id="option2N" value="subscription" onclick=location.href="testhaha.php?id=$rs[4]">';

改成

echo '<input type="radio" name="subscription" id="option2N" value="subscription" onclick=location.href="testhaha.php?id=' . $rs[4] . '">';

試試看

p39212053 iT邦新手 4 級 ‧ 2019-02-16 14:12:23 檢舉

非常感謝海綿寶寶大大!!已解決T T

淺水員 iT邦大師 6 級 ‧ 2019-02-16 18:10:05 檢舉

可以去開發者工具列那邊看看這樣寫產生的HTML跟原本你寫的差在哪邊喔

get傳送會有點危險喔。要注意

0

我剛看完你的程式碼。發現你的問題在於你是用post的方式來做ajax傳送。
method:"POST",

可是你的php是用get取值??

這樣你當然取不到值啊。

我要發表回答

立即登入回答