( PHP /JQuery?) Get the table row value when the checkbox is checked - php

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"}]

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],
];
}

Related

Form send data to php with multiple inputs with the same name

I'm trying make a form where me or user can insert data. Some parts are coming form data base like: Work name and work price. In input fields can insert work amount. In each input row have checkbox, if checkbox ar checked, that row will be seen in php.
<table class="table">
<thead>
<tr>
<th scope="col">*</th>
<th scope="col">Work name</th>
<th scope="col">Quantity</th>
<th scope="col">Price for unit</th>
<th scope="col">Total price</th>
</tr>
</thead>
<form method="POST" action="process/pdf.process.php">
<tbody>
<?php
$works = ORM::for_table('work')->find_many();
foreach($works as $work){
?>
<tr id="<?php echo $work->id; ?>">
<td><input type="checkbox" name="useit[]" value="<?php echo $work->id; ?>"/></td>
<td><?php echo $work->name; ?></td>
<td><input type="text" placeholder="" class="amount"/> <?php echo $work->unit; ?></td>
<td class="work_price" data="<?php echo $work->price; ?>"><?php echo $work->price.'€/'.$work->unit; ?></td>
<td class="total_price">0€</td>
<input type="" name="work_id[]" value="<?php echo $work->id; ?>" />
<input type="hidden" name="work_name[]" value="<?php echo $work->name; ?>" />
<input type="hidden" name="amount[]" class="<?php echo $work->id; ?>_copy_amount" value="" />
<input type="hidden" name="unit[]" value="<?php echo $work->unit; ?>" />
<input type="hidden" name="unit_price[]" value="<?php echo $work->price; ?>€" />
<input type="hidden" name="unit_total[]" class="<?php echo $work->id; ?>_copy_total" value="" />
</tr>
<?php
}
?>
</tbody>
<input type="submit" name="do_pdf" value="Pga jkāuztaisa ar jquery" />
</form>
</table>
Now, there is php, but how can i show only checked rows in while loop?
<?php
$data = array();
$work_id = array();
$work_names = $_POST['work_name'];
$amounts = $_POST['amount'];
$units = $_POST['unit'];
$units_prices = $_POST['unit_price'];
$units_total = $_POST['unit_total'];
if(isset($_POST['useit'])){
$work_id = $_POST['useit'];
}
$data = array($work_id, $work_names, $amounts, $units, $units_prices, $units_total);
echo '<pre>';
echo htmlspecialchars(print_r($data, true));
echo '</pre>';
?>
There are different possibilities how to do that. One Suggestion (i limit it to the parts which are relevant)
form (note that i gave work_name the id as an index, use_it not (but it could have)
<td><input type="checkbox" name="useit[]" value="<?php echo $work->id; ?>"/></td>
<input type="hidden" name="work_name[<?php echo $work->id?>]" value="<?php echo $work->name; ?>" />
The form only submits the checked checkboxes values in an array, all other are omited. Therefore we could loop over the checked checkbox values like this
foreach($_POST['useit'] as $work_id){
$work_name = $work_names[$work_id];
//do something with the checked rows only (all others are not looped over)
}
This is only possible, due to the given id as an array key in the form work_name[<?php echo $work->id?>].
A general sidenote for better (and more secure) code: please note that your data could be modified by the user, and send back with wrong data (or worse). So please make sure to sanitize your input, or probably better in this case only submit the id in question and the new data and pickup the rest directly from your database. So you can make sure the hidden data has not been modified on the client side and send back wrong.

How to delete specified row in a database table using post method in php

//Deleting is working. However, I can't delete the specified row in the table. It always deletes the last row. I hope you could help me. Thank you! This is my code for displaying data from database:
<form action="deleteCart.php" method = "post" role="form">
<?php
while ($row = mysqli_fetch_array($result2)) {
?>
<tr style="text-align: center;">
<td> <img src="images/<?php echo $row["ImageProduct1"]; ?>"/>
<td><?php echo $row['NameProduct1']; ?> </td>
<td>#<?php echo $row['OrderID']; ?></td>
<td><?php echo $row['OrderQuantity']; ?></td>
<td><input type="submit" name="cancelOrder" value = "Cancel" ></td>
<td><input type="hidden" name="hiddenID" value="<?php echo $row['OrderID']; ?>"></td>
</tr>
<?php
}
?>
</form>
//This is my code for deleting:
if(isset($_POST['cancelOrder'])){
orderID = $_POST['hiddenID'];
mysqli_query($con, "DELETE FROM OrderTable WHERE OrderID=$_POST[hiddenID];");
header('location: deleteCart.php');
}
Delete only the last record because you submitting form whole table record. you should try this code. it will work fine.
this will submit separate record.
<?php
while ($row = mysqli_fetch_array($result2)) {
?>
<form action="deleteCart.php" method = "post" role="form">
<tr style="text-align: center;">
<td><img src="images/<?php echo $row["ImageProduct1"]; ?>"/>
<td><?php echo $row['NameProduct1']; ?> </td>
<td>#<?php echo $row['OrderID']; ?></td>
<td><?php echo $row['OrderQuantity']; ?></td>
<td>
<input type="hidden" name="hiddenID" value="<?php echo $row['OrderID']; ?>">
<input type="submit" name="cancelOrder" value = "Cancel" >
</td>
</tr>
</form>
<?php
}
?>

Process multiple arrays from form

I am trying to process multiple arrays from a form, but I constantly face the issue where only the last field is submitted.
The form is echoing results (IP addresses and their rDNS settings) from an simple xml output and publishing these in an editable form and table.
<?php include_once("API.php");
$ips=array('xx.xx.xx.xx','xx.xx.xx.xx','xx.xx.xx.xx');
foreach($ips as $ip){
$API->GetRdns($ip);
$xml = simplexml_load_string($API);
}
foreach ($xml->RdnsDetails as $RdnsDetails) :?>
<tr>
<td><?php echo $ip; ?></td>
<td><input type="text" name="hostname" value="<?php echo $RdnsDetails->hostname; ?>"></td>
<td><input type="text" name="extra" value="<?php echo $RdnsDetails->extra; ?>"></td>
</tr>
<?php endforeach; ?>
When the submit button is clicked, the changes have to be submitted.
<button type="submit" name="submit">Submit</button>
<?php if (isset($_POST['submit'])){
foreach ($ips as $ip) {
$API->Sent($ip,$_POST[hostname],$_POST[extra])
}
?>
How do I get the values 'hostname' and 'extra' submitted for each of the IP's ($ip)?
You should try creating an array instead of a single value. Try this:
<tr>
<td><?php echo $ip; ?></td>
<td><input type="text" name="ip[<?php echo $ip; ?>]['hostname']" value="<?php echo $RdnsDetails->hostname; ?>"></td>
<td><input type="text" name="ip[<?php echo $ip; ?>]['extra']" value="<?php echo $RdnsDetails->extra; ?>"></td>
</tr>
You can catch this with:
<?php if (isset($_POST['submit'])){
foreach ($_POST['ip'] as $ip => $info) {
$API->Sent($value,$info['hostname'],$info['extra']);
}
?>
You should bind your submit button into a <form> to get the values
<form>
<?php foreach ($xml->RdnsDetails as $RdnsDetails) :?>
<tr>
<td><?php echo $ip; ?></td>
<td><input type="text" name="hostname" value="<?php echo $RdnsDetails->hostname; ?>"></td>
<td><input type="text" name="extra" value="<?php echo $RdnsDetails->extra; ?>"></td>
</tr>
<?php endforeach; ?>
<button type="submit" name="submit">Opslaan</button>
</form>
another problem is you missed '' here:
$API->Sent($value,$_POST['hostname'],$_POST['extra'])
a form by default will only send one value for each "name" entity in the form. Can you try adding the brackets to the [name] property of the inputs so it sends the values as an array ?
I think i understand a little more now, you need the association between the IP and the hostname/extra values. How about if you add the ip as a hidden form field
<?php foreach ($xml->RdnsDetails as $RdnsDetails) :?>
<tr>
<td><?php echo $ip; ?></td>
<td><input type="hidden" name="ips[]" value="<?php echo $ip; ?>"></td>
<td><input type="text" name="hostname[]" value="<?php echo $RdnsDetails->hostname; ?>"></td>
<td><input type="text" name="extra[]" value="<?php echo $RdnsDetails->extra; ?>"></td>
</tr>
<?php endforeach; ?>
then you can process in the backend something like this:
<?php
$ips = $_POST['ips'];
$hostnames = $_POST['hostname'];
$extras = $_POST['extras'];
for($i=0;$i<count($ips);$i++)
{
echo "IP: " . $ips[i] . ", Hostname: " . $hostnames[i] . ", Extra: " . $extras[$i];
}

saving data from an array without submitting any form php

I'm tryng to save an array so I have the following code:
<?php
$sql = "SELECT * FROM scenarii where code_s='".mysql_real_escape_string($_POST['code_s'])."'";
$qry = mysql_query($sql) or die(__LINE__.mysql_error().$sql);
$i = -1; // index des enregistrements
?>
<table cellpadding="5" cellspacing="5">
<tr>
<td><strong>CODE SCENARIO</strong></td>
<td><strong>LIBELLE</strong></td>
<td><strong>ACTION</strong></td>
<td><strong>DESCRIPTION</strong></td>
<td><strong>DATE</strong></td>
</tr>
<form action="<?php echo (isset($_POST['go'])) ? 'go.php' : '#'; ?>" method="post">
<input type="hidden" name="liasse" value="<?php echo $_POST['liasse']; ?>"/>
<input type="hidden" name="n_doss" value="<?php echo $_POST['n_doss']; ?>"/>
<input type="hidden" name="qualite" value="<?php echo $_POST['qualite']; ?>"/>
<?php while($row = mysql_fetch_assoc($qry)): ?>
<tr>
<td><input name="data[<?php echo ++$i; ?>][code_s]" type="text" value="<?php echo $row['code_s'];?>" size="10"></td>
<td><input name="data[<?php echo $i; ?>][titre]" type="text" value="<?php echo $row['titre']; ?>" size="45"></td>
<td><input name="data[<?php echo $i; ?>][action]" type="text" value="<?php echo $row['action']; ?>" size="15"></td>
<td><input name="data[<?php echo $i; ?>][libelle]" type="text" value="<?php echo $row['libelle']; ?>" size="55"></td>
<td><input type="text" name="data[<?php echo $i; ?>][date]" value="<?php echo $get_date($row['jour']) ; ?>" size="12"></td>
</tr>
<?php endwhile; ?>
And in order to save this I have this code:
if (isset($_POST['liasse'])) {
$value = $_POST['data'] ;
foreach($value as $key => $array)
{
$sql = 'INSERT INTO agenda SET
liasse = "'.mysql_real_escape_string($_POST['liasse']).'",
code_s = "'.mysql_real_escape_string($array['code_s']).'",
date_action = "'.date('Y-m-d',strtotime($array['date'])).'",
libelle = "'.mysql_real_escape_string($array['titre']).'",
action = "'.mysql_real_escape_string($array['action']).'",
description = "'.mysql_real_escape_string($array['libelle']).'",
n_doss = "'.mysql_real_escape_string($_POST['n_doss']).'",
qualite = "'.mysql_real_escape_string($_POST['qualite']).'"
';
mysql_query($sql) or die(__LINE__.mysql_error().$sql);
}
But I'm really lost,
In fact I use a form for that, now I would like to submit all of this data but without any form, directly when I have the first while I would like to save it.
The thing is that I'm lost because I can not call any var like that data[][code_s].
So I do not know how to save this. I would like to save it in background, and not to display that something has been saved.
Receive all my Utmost Respect
kind regards,
SP.
Wrap the code od the lower code block into a function and hand over the value array as argument:
function storeValues ($data) {
foreach($data as $key => $val)
{
$catalog=sprintf("%s='%s'",$key,$val);
$sql = sprintf('INSERT INTO agenda SET %s', implode(',',$catalog));
mysql_query($sql) or die(__LINE__.mysql_error().$sql);
} // foreach
} // function storeValues
You call this function when you want to save the values. So after retrieving them from the database. You call it for each row you retrieve and hand over the values like that:
storeValues ($row);
This will store one row of values at a time. Obviously this can be optimized to use a multiple insert. But let's take one step after another...

using PHP for each to get dynamic values from table

It's been a while since I looked at php and I've got a brain block. I'm trying to get the values from a table using foreach so that I can store the values in a session and also display the number of items that have been ordered.
eg 2 of item number 4 etc
here's the table / form
<form id="products" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
//every item from the products table is queried because all fields need to be displayed
$sSQL = "SELECT * FROM products";
$rsResult = mysql_query($sSQL);
?>
<table id="products_table">
<tr style="font-weight:bold">
<td style="text-align:center">ID</td>
<td>Ref No.</td>
<td>Product Name</td>
<td>Description</td>
<td style="text-align:right">Price</td>
<td colspan='2' style='text-align:center'>Add To Order</td>
</tr>
<!--for each record in the table that matches the query a row is created in the table and the data in the relevant field is displayed-->
<?php while ($row = mysql_fetch_array($rsResult)){ ?>
<tr>
<input type="hidden" name="productID[<? echo $row['productID']; ?>]" value="<? echo $row['productID']; ?>" />
<td style="text-align:center"><? echo $row['productID']; ?></td>
<td><? echo $row['productReference']; ?></td>
<td><? echo $row['productName']; ?></td>
<td><? echo $row['productDescription']; ?></td>
<td style="text-align:right"><? echo '£'. $row['productPrice']; ?></td>
<td style="text-align:center"><span>Qty</span><input type='text' name="qty[<? echo ($_POST['qty']); ?>]" value="" ></td>
<? } ?>
</table>
Heres the php
<?php
foreach($_POST as $key => $value){
echo $key . ' ' .$value.'<br />';
print_r($key);
}
?>
I know that the php is no where complete for filling sessions etc, I just can't work out how to get the values out of the form. This php was my attempt to try and at least get some kind of value out of it
Any help is greatly appreciated
in your code , there is no need of
<input type="hidden" name="productID[<? echo $row['productID']; ?>
field .
also change the line
<td style="text-align:center"><span>Qty</span><input type='text' name="qty[<? echo ($_POST['qty']); ?>]" value="" ></td>
to
<td style="text-align:center"><span>Qty</span><input type='text' name="qty[<? echo ($row['productID']); ?>]" value="" ></td>
so that after the submission of the form , you can access the posted values with :
$sSQL = "SELECT * FROM products";
$rsResult = mysql_query($sSQL);
while ($row = mysql_fetch_array($rsResult))
{
echo $_POST['qty'.$row['productID']];
}
why don't you just display data in inputs with proper names?

Categories