0

It's a table, each row consists a checkbox, when it's checked, would like to get the respective td values and echo out. Here im using the if statement, but it doesn't seems to work.

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? 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.

 [{"phone":"123456","message":"test"},
 {"phone":"789456","message":"testing"}]
3
  • is there more code involved with this, is the last php snippet called via ajax call? Commented Oct 11, 2017 at 3:34
  • @flauntster I don't think he has used ajax and thats root of evil in this case Commented Oct 11, 2017 at 3:35
  • yes, haven't use ajax Commented Oct 11, 2017 at 3:36

1 Answer 1

1

Change your HTML code as below:

<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']; ?> in the input control name.

Change your PHP code as below:

foreach($_GET["p"] as $id) {
   $specific[] = [
         "phone" => $_GET["phone"][$id],
         "message" => $_GET["msg"][$id],
       ];
}
Sign up to request clarification or add additional context in comments.

6 Comments

The thing is the php code is within the form, so maybe cant use$_GET, as the form has submit yet
Do you want the solution for Javascript or PHP? In the PHP, you can only get the result if you submit the form.
i have tried to submit the form, but cant echo out the data
Did you copied text from my answer? I forgot to put ending bracket(];). $specific[] = [ "phone" => $_GET["phone"][$id], "message" => $_GET["msg"][$id], ];
I have created a gist with sample code gist.github.com/anonymous/b40136df15087bb8b0955a1ecbee33f3
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.