简体   繁体   English

(PHP / JQuery?)选中复选框时获取表的行值

[英]( PHP /JQuery?) Get the table row value when the checkbox is checked

It's a table, each row consists a checkbox, when it's checked, would like to get the respective td values and echo out. 这是一张表,每一行都包含一个复选框,选中该复选框时,希望获取各自的td值并回显。 Here im using the if statement, but it doesn't seems to work. 我在这里使用了if语句,但似乎不起作用。

And iam using php here, is using jquery a way out, can jquery work with php code, so could i send those checked table row values back to serve? IAM在这里使用php,是使用jquery的一种出路,jquery可以与php代码一起使用,所以我可以将这些已检查的表行值发送回服务吗? Any thoughts? 有什么想法吗? Thank you. 谢谢。

  <form>
  <table>
  <tr>
  <?php

 $specific = []; 
 while($row = mysqli_fetch_array( $result ,MYSQL_ASSOC)) {?>

 <td><input type="checkbox" name="p[]" value="<?php echo $row['id']; ?>">
 </td>

  <td><input type="text"  name="patientid[]" 
  style="border: none" value="<?php echo $row['patientid'] ?>"></td>

  <td>
  <textarea name="msg" style="border: none" class='msg'>
  <?php echo  $row['message'];} ?> </textarea>
  </td>

 <td><input class="phone" type="text" value="<?php echo 
 $row['telMobile'] ?>"></td>

check whether table row(s) are checked 检查是否检查表行

  <?php if(!isset($_GET['p'])){

  $specific[] = [
                 "phone" => $row["telMobile"],
                 "message" =>$row["message"],

  ];}
  }  

  $result = json_encode($specific,JSON_UNESCAPED_UNICODE);
  echo $result; 

  echo "</tr>";
  echo "</table>";
  echo "</form>";?>

The desired result for $result is to show only the data of the table row(s) that are checked . $ result的期望结果是仅显示被checked的表行的数据。

 [{"phone":"123456","message":"test"},
 {"phone":"789456","message":"testing"}]

Change your HTML code as below: 更改您的HTML代码,如下所示:

<td><input type="text"  name="patientid[<?php echo $row['id']; ?>]" 
  style="border: none" value="<?php echo $row['patientid'] ?>"></td>

  <td>
  <textarea name="msg[<?php echo $row['id']; ?>]" style="border: none" class='msg'>
  <?php echo  $row['message'];} ?> </textarea>
  </td>

 <td><input name="phone[<?php echo $row['id']; ?>]" class="phone" type="text" value="<?php echo 
 $row['telMobile'] ?>"></td>

I have added <?php echo $row['id']; ?> 我添加了<?php echo $row['id']; ?> <?php echo $row['id']; ?> in the input control name. 输入控件名称中的<?php echo $row['id']; ?>

Change your PHP code as below: 如下更改您的PHP代码:

foreach($_GET["p"] as $id) {
   $specific[] = [
         "phone" => $_GET["phone"][$id],
         "message" => $_GET["msg"][$id],
       ];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM