0

I have below php table, it has row and column. Each row has checkbox, I am trying to retrieve email data from row that checkbox is selected. I can do this with Jquery, but since I can't save in text file. I am trying to do same thing with PHP. I try couple things, but none worked.

<?php
echo "<table border='1' bordercolor='red' id='event_table'>
<tr>
    <th> <input type='checkbox' id='chk_all' /> </th>
    <th>Id</td>
    <th>Email</th>
    <th>Name</th>
    <th>Car</th>
    <th>Phone</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
    echo "<tr>";
    echo "<td>  <input type='checkbox' id='chk[]'/> </td>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['email'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['car'] . "</td>";
    echo "<td>" . $row['phone'] , "</td>";
    echo "</tr>";
}
echo "</table>";
?>
8
  • 1
    input type should have a name Commented Apr 18, 2019 at 3:21
  • Your checkbox won't be sent with the data as it doesn't have a name, you also need to provide unique IDs. id='chk[]' should be name='chk[]'. What are you using to determine if a checkbox should be checked? Commented Apr 18, 2019 at 3:23
  • 1
    Just reread your description, you can't get the data with PHP unless you either submit the form, or do an AJAX request. JS / jQuery is your best option Commented Apr 18, 2019 at 3:26
  • @Second2None I tried if(!empty($_POST['chk[]'])) but didn't really help Commented Apr 18, 2019 at 3:26
  • @Second2None after submit the form if I can get the data thats good enough for me. All I want is the save the data in txt file Commented Apr 18, 2019 at 3:27

2 Answers 2

1

You can do this using Hidden fields and some indentity implementation on name= (and id= as well). Here are some inputs from my end but you can modify the code as per your requirements.

while($row = mysqli_fetch_array($result))
{
    echo "<tr>";
    echo "<td>  <input type='checkbox' name='chk_".$row['id']."' id='SHOULD_BE_UNIQUE_ON_THIS_PAGE'/> </td>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['email'] . "<input type='hidden' name='chk_".$id."_email' value='".$row['email']."' id='SHOULD_BE_UNIQUE_ON_THIS_PAGE' /></td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['car'] . "</td>";
    echo "<td>" . $row['phone'] . "</td>";
    echo "</tr>";
}
echo "</table>";

Here chk_{ID} is your main identifier while filtering data. When you submit the form, you should get to know whether checkbox is checked or not and if it is checked then you will come to know which hidden input field has your data. Hidden fields will not appear in HTML page.

Sign up to request clarification or add additional context in comments.

8 Comments

I dont quite understand what this return when form is submitted, does return with the list of emails selected?
yes, after submitting just do print_r($_POST); to see the values you posted
I get Array ( [search] => ) only
You need to put that code on <form> in order to get the submitted values
even if this print out array of emails, since its not saved in variable how can I save this in a file?
|
1

As PHP is a Hypertext Preprocessor language, you can't detect any client action on your page. So here is the best solution: you must detect changes you need with JavaScript/JQuery, and then send the result to a php file via Ajax and handle the result there (In your case save it in a file);

I hope it would be helpful for you. Let me know if you have any questions

Comments

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.