[英]Using ajax to send rowID and append selected rows to new table
Currently trying to take a row with a specific ID number and, if the row is checked, allow the user to click a button and append these specific rows to a new table in order to do comparisons, etc. The rows are all generated in a table using php and my code is only about half complete but I'm not sure where to go from here. 当前尝试使用具有特定ID号的行,如果选中了该行,则允许用户单击按钮并将这些特定行附加到新表中以进行比较,等等。所有这些行都在使用php的表格,我的代码仅完成了一半,但我不确定从这里开始。
Here is my current jquery: 这是我当前的jQuery:
$(function(){
var rows = [];
$('compareRowButton').click(function(){
$('.compareRow:checked').each(function(){
var ele_id = $(this).attr('id');
$.ajax({
type : 'post',
url : 'compare.php', //query
data : 'post_id='+ ele_id, // passing id via ajax
success : function(data){
rows.push(data);
}
});
});
$('.displayTable').click(function(){
// append to table or divs
});
});
});
});
and here is what I have in compare.php: 这是我在compare.php中所拥有的:
<?php
include_once('functions.php');
include_once('link_costreport_2013.php');
sec_session_start();
if(isset($_POST['post_id'])){
$id = $_POST['post_id'];
}
$query = $link->prepare("SELECT *
FROM `s10`
WHERE `id` = :id");
$query->bindParam(':id', $id, PDO::PARAM_INT);
$query->execute();
$results = $query->fetch();
?>
I'll admit I had some help with the jquery/ajax so my understanding of how it works is lacking a little, but a few things I don't understand fundamentally are: 我承认我在jquery / ajax方面有一些帮助,所以我对它的工作原理缺乏一些了解,但是我根本不了解的一些内容是:
<td></td>
elements under the right headers and such? <td></td>
元素中? The ID is obviously just one part of the entire query but there are about 6-7 columns worth of data for each ID. At the end I'm just trying to display the new table with the selected rows and hide the old one. 最后,我只是想显示具有选定行的新表,并隐藏旧表。
If I can provide any additional information I'd be glad to, wasn't sure what else to include. 如果我能提供我愿意提供的其他信息,则不确定是否还包括其他内容。
Thanks in advance 提前致谢
::EDIT:: ::编辑::
while ($results = $query->fetch()) {
$id = $results['id'];
$rowID = $results['id'];
if($results['301_cost_of_uncomp_care'] != 0){
$charityPortion = ($results['233_net_charity_care'] / $results['301_cost_of_uncomp_care']);
$baddebtPortion = ($results['291_cost_of_non_mcr_bad_debts'] / $results['301_cost_of_uncomp_care']);
} else {
$charityPortion = 0;
$baddebtPortion = 0;
}
echo "<tr id='$rowID'>";
echo "<td></td>";
echo "<td><input type='checkbox' id='$id' value='$id' class='compareCheck' name='post_id[]'></input></td>";
echo "<td>".$results['provider_num'];
echo "</td>";
echo "<td><a id='$id' data-toggle='modal' href='#provmodal' class='push'>".$results['provider_name']."</a>";
echo "</td>";
echo "<td $style>\$".number_format($results['233_net_charity_care']);
echo "</td>";
echo "<td $style>\$".number_format($results['291_cost_of_non_mcr_bad_debts']);
echo "</td>";
echo "<td $style>\$".number_format($results['301_cost_of_uncomp_care']);
echo "</td>";
echo "<td $style>".sprintf("%.1f%%", $charityPortion * 100);
echo "</td>";
echo "<td $style>".sprintf("%.1f%%", $baddebtPortion * 100);
echo "</td>";
echo "</tr>";
}
?>
This is how the final fields are calculated. 这就是最终字段的计算方式。 Is it possible to call modify the formatting like so in the script?
是否可以像在脚本中那样调用修改格式?
Use json_encode()
on the PHP side to encode the fetched results: 在PHP端使用
json_encode()
对获取的结果进行编码:
$results = $query->fetch(PDO::FETCH_NUM);
echo json_encode($results);
Then to display the results, turn the JSON array into a row of the table. 然后,要显示结果,请将JSON数组变成表的一行。
$.ajax({
type : 'post',
url : 'compare.php', //query
data : 'post_id='+ ele_id, // passing id via ajax
dataType: "json",
success : function(data){
var row = "<tr>";
$.each(data, function(i, val) {
row += "<td>" + val + "</td>";
});
row += "</tr>";
$("#tableID").append(row);
}
});
If you only want to display selected columns, have PHP return an associative array: 如果只想显示选定的列,请让PHP返回一个关联数组:
$results = $query->fetch(PDO::FETCH_ASSOC);
echo json_encode($results);
and extract the columns you want: 并提取所需的列:
$.ajax({
type : 'post',
url : 'compare.php', //query
data : 'post_id='+ ele_id, // passing id via ajax
dataType: "json",
success : function(data){
var row = "<tr>";
row += "<td>" + data.id + "</td>";
row += "<td>" + data.provider_num + "</td>";
row += "<td>" + data['291_cost_of_non_mcr_bad_debts'] + "</td>";
row += "</tr>";
$("#tableID").append(row);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.