From php table retrieve data from selected checkbox - php

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>";
?>

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.

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

Related

Retrieve row id from table displayed in webpage

I have a MySQL table that is displayed in the webpage and every row has a submit button. When the submit button for a particular row is clicked, the first element i.e. row id should be retrieved. Should I use arrays to store the data or can I obtain the row id from the table I've echoed on screen. Here's the code:
echo "<table>";
while($row = mysqli_fetch_row($result))
{
echo "<tr>";
echo "<td align='center' name='bus_id'>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "<td>" . $row[5] . "</td>";
echo "<td>" . $row[6] . "</td>";
echo "<td>" . $row[7] . "</td>";
echo "<td>" . $row[7] . "</td>";
echo "<td><form name='f1' method='post' action='schedule_list.php'>
<input type='submit' name='book' value='Book'></form></td>";
echo "</tr>";
}
echo "</table";
Use a a button with type submit to submit the ID as follows (and still display a meaningful button title
echo "<td><form name='f1' method='post' action='schedule_list.php'>
<button type='submit' name='book' value='".$row[0]."'>Book</button></form></td>";

Get table value html

I'm trying to update a mySQL table after a button click..The button click is not the problem but I wonder how I can get the klant_pk which is unique to update a certain record in mySQL. As you see I print out the mySql table at first. So is there anyone who know how I can get the according klant_pk after I click on a button in the table..
Thanks
$result = mysqli_query($con, "SELECT * FROM bestelling");
echo "Bestellingen";
echo "<table border='1' align='center'>
<tr>
<th>Bestelling_pk</th>
<th>Klant_pk</th>
<th>Product</th>
<th>Commentaar</th>
<th>Tijd</th>
<th> Voortgang </th>
<th> Status </th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['bestelling_pk'] . "</td>";
echo "<td>" . $row['klant_pk'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['opmerking'] . "</td>";
echo "<td>" . $row['tijd'] . "</td>";
echo "<td> <input type='button' value='In Wacht' onclick='return change(this);' />";
echo "<td>" . $row['status'] . "</td>";
echo "</tr>";
}
echo "</table>";
while ($row = mysqli_fetch_array($result)) {
...
echo "<td id='klank_pk_".$row['klant_pk']."'>" . $row['klant_pk'] . "</td>";
...
echo "<td> <input type='button' value='In Wacht' onclick='change(getElementById('klank_pk_".$row['klant_pk']."').value);' />";
...
}

Parsing multiple checkbox values into php array

I have a form which displays stock levels and I want the user to be able to delete multiple products so have provided checkboxes. the code is below:
echo "<form method='get'>
<input type='submit' name='removestock' value= 'Remove'>
<table class='display' border='0'>
<tr>
<th>Select</th>
<th>Name</th>
<th>Description</th>
<th>Price (£)</th>
<th>Quantity</th>
<th>Size</th>
</tr>";
echo "<tr>";
require ('connection.php');
$query = mysql_query("SELECT * FROM items")or die(mysql_error());
while($results = mysql_fetch_array($query)){
echo "<td> <input type='checkbox' name='item' value='".$results['item_id']."'></td>";
echo "<td>" . $results['name'] . "</td>";
echo "<td>" . $results['description'] . "</td>";
echo "<td>" . $results['price'] . "</td>";
echo "<td>" . $results['quantity'] . "</td>";
echo "<td>" . $results['size_id'] . "</td>";
echo "</tr>";
}
echo "</table></form>";
And my parsing code is...
if(isset($_GET['removestock']) === true){
$errors = array();
$items = $_GET['item'];
echo $items;
}
But for some reason it only displays the last item_id selected. Your help will be much appreciated!
PS. I tried changing the checkbox name to name="items[]" and implemented a foreach loop to parse the data but it still did not work.
The name of the checkbox should be
<input type='checkbox' name='item[]' value='".$results['item_id']."'>
This wil give you an array which you can read with a foreach
Use name=item [] for the checkboxes. This will give you the array.
Then use var_export ($ items)
Change this line:
"<td> <input type='checkbox' name='item' value='".$results['item_id']."'></td>";
To read:
"<td> <input type='checkbox' name='item[" . $results['item_id'] . "]'></td>";
And you will get your desired array in the variable.
This always works for me:
$i =0;
while($results = mysql_fetch_array($query)){
echo "<td> <input type='checkbox' name='item[".$i."]' value='".$results['item_id']."'></td>";
echo "<td>" . $results['name'] . "</td>";
echo "<td>" . $results['description'] . "</td>";
echo "<td>" . $results['price'] . "</td>";
echo "<td>" . $results['quantity'] . "</td>";
echo "<td>" . $results['size_id'] . "</td>";
echo "</tr>";
$i++;
}
Once posted, the item[] element will be an array.

Dynamically generated radio button dont work

I am generating radio buttons dynamically by PHP but it is not working .I am not able to uncheck a radio button once checked and once I select the other radio button with same name other is not automaticaly unchecked .
echo "<form action="."".">";
echo "<table border='1'>
<tr>
<th>Reg Num</th>
<th>Select</th>
<th>Reject</th>
</tr>";
/* <th>Name</th>
<th>State</th>
<th>Constituency</th> */
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
// echo "<td>" . $row['voter_id'] . "</td>";
// echo "<td>" . $row['name'] . "</td>";
// echo "<td>" . $row['state'] . "</td>";
// echo "<td>" . $row['constituency'] . "</td>";
echo "<td>" . $row['numreg'] . "</td>";
echo "<td><input type="."radio"." name=".$row['voter_id']. "value="."1"."></td>";
echo "<td><input type="."radio"." name=".$row['voter_id'] ."value="."2"."></td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
try following
echo "<form action=''>";
echo "<table border='1'>
<tr>
<th>Reg Num</th>
<th>Select</th>
<th>Reject</th>
</tr>";
/* <th>Name</th>
<th>State</th>
<th>Constituency</th> */
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
// echo "<td> $row['voter_id']</td>";
// echo "<td>" . $row['name'] . "</td>";
// echo "<td>" . $row['state'] . "</td>";
// echo "<td>" . $row['constituency'] . "</td>";
echo "<td>" . $row['numreg'] . "</td>";
echo "<td><input type='radio' name='voter_id' value='$row[voter_id]'></td>";
echo "<td><input type='radio' name='voter_id' value='$row[voter_id]'></td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
For that the name of your radio button must be same.Change your code like the following inside the while loop where you are creating the radio buttons
echo '<td><input type="radio" name="voter_id" id="'.$row["voter_id"]. '"value="1"></td>';
echo '<td><input type="radio" name="voter_id" id="'.$row["voter_id"] .'"value="2"></td>';
We dont need double quotes for the type.Try like
echo "<td><input type='radio' name='".$row['voter_id']. "' value='1' "></td>";
Finally I did this and it worked
echo '<td><input type="radio" name="'.$row["voter_id"]. '"value="1"></td>';
echo '<td><input type="radio" name="'.$row["voter_id"] .'"value="2"></td>';

passing a variable between the 2 phps with POST method

I have a code that populates data from a mysql table into an html table. I also have a text box and a button at the end of each row. I want to send all the variables in the row including the text in the textbox to update.php. Could not do that somehow. Here is the code I am trying. Please help. I can send it through GET method. But I want to use POST.
<?php
require 'config.php';
mysql_connect ($dbhost, $dbusername, $dbuserpass);
mysql_select_db($dbname) or die('Cannot select database');
$query = "SELECT * FROM cust_info;";
$result = mysql_query($query) or die(mysql_error());
echo "</br>";
echo "<table border='1'>
<tr>
<th>Pop Code</th>
<th>Open_Date</th>
<th>Maturity_Date</th>
<th>Amount</th>
<th>Balance</th>
<th>Collection Amount</th>
</tr>";
while($row1 = mysql_fetch_array($result))
{
echo "<div class=\"addform\"><form method='post' action=\"update.php?upd=".$row1['pop_code']."\">\n";
echo "<tr>";
echo "<td>" . $row1['pop_code'] . "</td>";
echo "<td>" . $row1['open_date'] . "</td>";
echo "<td>" . $row1['mat_date'] . "</td>";
echo "<td>" . $row1['depoamt'] . "</td>";
echo "<td>" . $row1['balance'] . "</td>";
echo "<td>" . " <input type=\"text\" name=\"amount\"/>\n" . "</td>";
echo "<td>" . " <input type=\"image\" src=\"images/update.png\" alt=\"Update Row\" class=\"update\" title=\"Update Row\">\n" . "</td>";
echo "</tr>";
echo "</form></div>";
}
echo "</table>";
?>
Change your table code to something like this:
echo "<td><input type='hidden' name='pop_code' value='".$row1['pop_code']."'>" . $row1['pop_code'] . "</td>";
It looks like your code isn't actually sending anything, just displaying it on the page. This will display it as well as sending a hidden field when the form is submitted.
Edit: Another way to do it would be to have a single form on your page outside your loop and have the button run a javascript function that copies the data to the form and submits the form. Probably easier the way you have it at the moment, but a javascript like that would be able to pick up other information off the user/page easily and send it via a single form to the next page.
You can make the fields hidden:
echo "<td><input type='hidden' name='pop_code' value='" . $row1['pop_code'] . "' />" . $row1['pop_code'] . "</td>";
echo "<td><input type='hidden' name='open_date' value='" . $row1['open_date'] . "' />" . $row1['open_date'] . "</td>";
your HTML is broken, <table> only allows <thead>, <tbody>, <tfoot> and <tr> as direct children, try fix this first because this will cause your Browser to close Tags automatically

Categories