How to calculate sum of all rows with jQuery? [duplicate] - php

This question already has answers here:
jQuery sum rows adding all cells
(3 answers)
Closed 5 years ago.
Below is my table code with Add New button. Onclick Add New
button new row will create. In picture you can see this. User will
input debit and credit values at the end all debit values should be
calculate and sum should be display and same for the credit value. I
am using jQuery to create new row:
Jquery code:
<script type="text/javascript">
$("#add, .check ").click(function() {
$('#datatable-buttons tbody>tr:last')
.clone(true)
.insertAfter('#datatable-buttons tbody>tr:last').find('input').each(function(){
$(this).val('');
});
});
</script>
Html table code:
<table id="datatable-buttons" class="table table-striped jambo_table bulk_action">
<thead>
<tr class="headings">
<th>
<input type="checkbox" id="check-all" class="flat">
</th>
<th class="column-title">Account</th>
<th class="column-title">Debits</th>
<th class="column-title">Credits</th>
<th class="column-title">Description</th>
<th class="column-title">Name</th>
<th class="column-title no-link last"><span class="nobr">Action</span>
</th>
<th class="bulk-actions" colspan="7">
<a class="antoo" style="color:#fff; font-weight:500;">Bulk Actions ( <span class="action-cnt"> </span> ) <i class="fa fa-chevron-down"></i></a>
</th>
</tr>
</thead>
<tbody>
<tr class="even pointer">
<td class="a-center ">
<input type="checkbox" class="flat" name="table_records">
</td>
<td class=" "><select class="form-control" name="journalname">
<option>Choose option</option>
<?php
$sql = mysqli_query($conn,'SELECT * FROM `chartofaccounts`');
while ($row1 = mysqli_fetch_array($sql))
{?>
<option value="<?php echo $row1["name"];?>"><?php echo $row1["name"];?></option>
<?php }?>
</select></td>
<td class=""><input class="form-control" type="text" name="debit" ></td>
<td class=" "><input class="form-control" type="text" name="credit"></td>
<td class=" "><input class="form-control" type="text" name="descc"></td>
<td class=" "><input class="form-control" type="text" name="name"></td>
<td class=" last">View
</td>
</tr>
</tbody>
</table>
<button type="button" id="add">Add New</button>
Below image will give you clear understanding:

made you a simple example of how to calculate the sum from the input fields.
check snippet below or jsFiddle
add numbers to the 3 inputs, and then click on View , it will get the sum of the 3 input values.
var input = $("input"),
button = $("td.last a")
$(button).click(function () {
var sum = 0;
$(input).each(function() {
sum += Number($(this).val());
});
$(this).html(sum)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>
<input>
</td>
<td>
<input>
</td>
<td>
<input>
</td>
<td class=" last">View
</tr>
</table>

Related

How to add and remove table rows using checkboxes

The problem is here that I have successfully written the code to add and remove row on checked checkbox but it only works for the first element.
Like when I click on the first element it adds a row and on unchecking it remove that row second time when I check and uncheck the second checkbox it adds a row on checked and removes row on unchecked.
here is jquery code
<script type="text/javascript">
$(document).ready(
function () {
$('.finding').change(
function () {
if ($('.finding').is(':checked')) {
var finding = $(this).next('label').text();
$('#findings-table').append("<tr><td>"+finding+"</td></tr>");
}
else {
$('#findings-table').empty();
}
});
});
</script>
here is php code i'm getting checkboxes
if ($checkResult > 0) {
for ($i=0; $i < $checkResult ; $i++) {
$result = mysqli_fetch_assoc($query);
$findings = $result['name'];
$count = 1 + $i;
echo"<tr>
<th scope='row'>$count</th>
<td scope='col'>
<div class='input-group'>
<input class='finding form-check-input' type='checkbox' name='findings[]' value='$findings'>
<label class='input-label pt-1 px-3'>$findings</label>
</div>
</td>
</tr>";
}
}
and finally here is my html code where checkboxes appear
<div class='table-container' style="height: 21vh;">
<table class="table table-hover" style="line-height: 7px;">
<tbody>
<?php include 'getfindings.php'; ?>
</tbody>
</table>
</div>
I could not figure out what #findings-table is, therefor I just created a table.
See this:
$(function() {
$('.finding').on('change', function(e) {
var $this = $(e.currentTarget),
$findingTable = $('#findings-table'),
labelText = $this.parents('.input-group').find('.input-label').text();
if ($this.is(':checked')) {
$findingTable.append($('<tr/>').append($('<td/>').text(labelText)));
} else {
$findingTable.empty();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<table id="findings-table"></table>
<div class='table-container' style="height: 21vh;">
<table class="table table-hover" style="line-height: 7px;">
<tbody>
<tr>
<th scope='row'>1</th>
<td scope='col'>
<div class='input-group'>
<input class='finding form-check-input' type='checkbox' name='findings[]' value='a'>
<label class='input-label pt-1 px-3'>nice a</label>
</div>
</td>
</tr>
<tr>
<th scope='row'>2</th>
<td scope='col'>
<div class='input-group'>
<input class='finding form-check-input' type='checkbox' name='findings[]' value='b'>
<label class='input-label pt-1 px-3'>nice b</label>
</div>
</td>
</tr>
<tr>
<th scope='row'>3</th>
<td scope='col'>
<div class='input-group'>
<input class='finding form-check-input' type='checkbox' name='findings[]' value='c'>
<label class='input-label pt-1 px-3'>nice c</label>
</div>
</td>
</tr>
<tr>
<th scope='row'>4</th>
<td scope='col'>
<div class='input-group'>
<input class='finding form-check-input' type='checkbox' name='findings[]' value='d'>
<label class='input-label pt-1 px-3'>nice d</label>
</div>
</td>
</tr>
</tbody>
</table>
</div>

Select an option to retrieve it's row from database MySQL & PHP

I have a table that should retrieve all the row form the database based on a selection from the same row.
HTML & PHP Code
<div class="row">
<div class="table-responsive">
<table class="table table-bordered" id="tab_logic">
<thead>
<tr>
<th>Brand Name</th>
<th>Item Name</th>
<th>Model Number</th>
<th class="col-md-2">Item Description</th>
<th>Part Number</th>
<th>Unit</th>
<th class="col-md-1">QTY</th>
<th class="col-md-1">Unit Price (SR)</th>
<th class="col-md-1">Total Price (SR)</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<strong>Record</strong>
</td>
<td>
<strong>Record</strong>
</td>
<td>
<strong>Record</strong>
</td>
<td>
<div class="form-group">
<?php
$query = $con->query("SELECT products_itemDescription FROM pruc_products"); // Run your query --> For Item Desc.
echo '<select class="form-control" id="DescriptionS2forms" name="itemDescription">'; // Open your drop down box
echo '<option value="" selected="selected" disabled></option>'; //Empty Value for VALIDATION
// Loop through the query results, outputing the options one by one
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="'.$row['products_itemDescription'].'">'.$row['products_itemDescription'].'</option>';
}
echo '</select>';// Close your drop down box
?>
</div>
</td>
<td>
<strong>Record</strong>
</td>
<td>
<strong>Record</strong>
</td>
<td>
<div>
<input class="form-control" type="number" name="requestQTY" required>
</div>
</td>
<td>
<div>
<input class="form-control" type="number" name="requestUnit" step="0.01" min="0.01" max="1000000" required>
</div>
</td>
<td>
<strong>Record</strong>
</td>
<td class="col-md-1" id="deleteBtn">
<a class="btn btn-default deleteBtn">Delete</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
So I have this selection as defined it is fetched from the database so I need every "Record" to be fetched also but based on the selection I'll make from the selection tab.
PHP & SQL Query
<?php
$sql = $con->prepare("SELECT products_brandName FROM pruc_products WHERE products_brandName, products_itemDescription = (products_id)") ;
$sql->execute() ;
while( $row = $sql->fetch()):
echo '<strong>'.$row['products_brandName'].'</strong>';
endwhile ?>
any help for this?
products_barndName needs to equal something. Also, don't use a comma to separate your conditions in a WHERE clause. Try this:
WHERE products_brandName = 'x' AND products_itemDescription = (products_id)

get individual child value from parent tag using onclick method

I want to get individual table id value with the individual button click. and I got the table id value but it shows all table id value's like (1234... 10).
How to get the individual table value (like clicking the 3rd button -> get 3rd table id only).
I am using both PHP and HTML code together.
This is my code.
<table>
<tr>
<td><b>S.No</b></td>
<td><b>User Name</b></td>
<td><b>Post Title</b></td>
<td><b>Post Date</b></td>
</tr>
<?php
require_once('databaseconnection.php');
$query = "SELECT * FROM `userpost`";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<tr>
<td style="width: 5%;" id="td_id" value="<?php echo " {$row[ 'postid']} "; ?>">
<?php echo "{$row['postid']}"; ?>
</td>
<td style="width: 15%;" id="td_user">
<?php echo "{$row['postuser']}"; ?>
</td>
<td style="width: 65%;" id="td_title">
<?php echo "{$row['posttitle']}"; ?>
</td>
<td style="width: 10%;">
<?php echo "{$row['postdate']}"; ?>
</td>
<td style="width: 5%;"><input id="getpostid" type="submit" value="view" onclick="actiongetpostid();"></input>
</td>
</tr>
<?php
}
?>
</table>
<script type="text/javascript">
function actiongetpostid() {
var tdid = $('tr').children('#td_id').text();
alert(tdid);
}
</script>
HTML code:
This table will create dynamically.
<tr>
<td style="width: 5%;" id="td_id" value="1">1</td>
<td style="width: 15%;" id="td_user">rose</td>
<td style="width: 65%;" id="td_title">YII2 framwork for PHP</td>
<td style="width: 10%;">2016-02-12</td>
<td style="width: 5%;"><input id="getpostid" type="submit" value="view" onclick="actiongetpostid();"></input>
</td>
</tr>
<tr>
<td style="width: 5%;" id="td_id" value="2">2</td>
<td style="width: 15%;" id="td_user">rose</td>
<td style="width: 65%;" id="td_title">Angular JS</td>
<td style="width: 10%;">2016-02-12</td>
<td style="width: 5%;"><input id="getpostid" type="submit" value="view" onclick="actiongetpostid();"></input>
</td>
</tr>
<tr>
<td style="width: 5%;" id="td_id" value="3">3</td>
<td style="width: 15%;" id="td_user">murali</td>
<td style="width: 65%;" id="td_title">EXT JS</td>
<td style="width: 10%;">2016-02-12</td>
<td style="width: 5%;"><input id="getpostid" type="submit" value="view" onclick="actiongetpostid();"></input>
</td>
</tr>
I am using this code within "while" loop so how to solve this issue. Please help me.
Firstly, I'm not sure why you are using <input> tags if you are just wanting to display the data. Use <button> tags instead.
On this note I would change your button td to something like:
<td style="width: 5%;"><button class="getpostid">View</button>
Note the use of class="getpostid". The use of id would only apply to the first element with getpostid as IDs can only be used once.
Then using jQuery:
$(document).ready(function(){
$('.getpostid').click(function(){
var tdid = $(this).parents('tr').find('#td_id').text();
alert(tdid);
});
});
Note the use of parents() instead of parent(). The latter can only go up one level in the DOM tree. Whereas the step needed is 2 levels.
Here is a JSFiddle with a working example.

adding new rows in table using ajax

i'm trying to get next rows in my multiple table using ajax and php. my concept is that on changing my year and type selection, table has to re_appears.
if i change type as year third table hides and if semester third table appears. it works fine,. but if i change no of year table rows also have to added according to that.
for example if no of years 4 means then i have to get fou rows in each table.
i dono where i struck here?
my code:
<table>
<tr><td><label><span class="required">*</span>No of Year</label></td>
<td>
<select name="no_of_year" id="no_no_year" onchange="change_row()">
<option value="">--Year--</option>
<?php for($i=1;$i<=$year;$i++) { ?>
<option value="<?php echo $i; ?>" <?php if($_POST['no_of_year']==$i) { echo "selected"; } ?>><?php echo $i; ?></option>
<?php } ?>
</select> <span class="required"><?php echo $msg_year;?></span>
</td>
</tr>
<tr> <td><label><span class="required">*</span>Type</label> </td>
<td><select name="type" onchange="change_col(this.value);"><option value="">--Type--</option> <option value="2" <?php if($_POST['type']=='2') { echo "selected"; } ?>>Semester</option> <option value="1" <?php if($_POST['type']== '1') { echo "selected"; } ?>> Year </option> </select>
<span class="required"><?php echo $msg_type;?></span></td></tr>
<tr> <td><label><span class="required">*</span>Fees Per Year/Semester:</label> </td> </tr>
<tr>
<table class="fees_dets" border="1px solid" cellpadding="1" cellspacing="1">
<tr>
<td>
<table id="year_tab" cellpadding="1" cellspacing="1">
<tr style="height:37px;"> <th > Year </th> </tr>
<tr> <td align="center"> <input type="text" name="year1" value="1" /> </td> </tr>
</table>
</td>
<td>
<table id="sem1_tab" cellpadding="1" cellspacing="1">
<tr> <th colspan="2"> Semester-1 </th> </tr>
<tr> <th> Due date</th> <th > Fees</th> </tr>
<tr> <td > <input type="text" name="y1_fees1" /> </td><td> <input type="text" name="y1_due1" /> </td> </tr>
</table>
</td>
<td id="donshow">
<table id="sem2_tab" cellpadding="1" cellspacing="1">
<tr> <th colspan="2" > Semester-2</th> </tr>
<tr> <th> Due date</th> <th > Fees</th> </tr>
<tr> <td > <input type="text" name="y1_fees2" /> </td><td > <input type="text" name="y1_due2" /> </td> </tr>
</table>
</td>
</tr>
</table>
</tr>
</table>
script:
<script type="text/javascript">
function change_col(type)
{
if(type == 1 ) {
document.getElementById("donshow").style.display = 'none';
}
else if(type == 2 ) {
document.getElementById("donshow").style.display = 'block';
}
}
function change_row(year)
{
$.ajax({
type: 'POST',
url: 'ajax_redirect.php',
data:{
type : 'year_tab',
year : year
},
success: function(msg){
//alert(msg);
document.getElementByClassName('fees_dets').innerHTML = msg;
}
});
}
</script>
ajax_redirect.php
<?
$n= $_POST['year'];
if($_POST['type'] == 'year_tab')
{
for($i=2;$i<=$n;$i++)
{
?>
<table class="fees_dets" border="1px solid" cellpadding="1" cellspacing="1">
<tr>
<td>
<table id="year_tab" cellpadding="1" cellspacing="1">
<tr style="height:37px;"> <th > Year </th> </tr>
<tr> <td align="center"> <input type="text" name="year<?=$i?>" value="<?=$i?>" /> </td> </tr>
</table>
</td>
<td>
<table id="sem1_tab" cellpadding="1" cellspacing="1">
<tr> <th colspan="2"> Semester-1 </th> </tr>
<tr> <th> Due date</th> <th > Fees</th> </tr>
<tr> <td > <input type="text" name="y<?=$i?>_fees1" /> </td><td> <input type="text" name="y<?=$i?>_due1" /> </td> </tr>
</table>
</td>
<td id="donshow">
<table id="sem2_tab" cellpadding="1" cellspacing="1">
<tr> <th colspan="2" > Semester-2</th> </tr>
<tr> <th> Due date</th> <th > Fees</th> </tr>
<tr> <td > <input type="text" name="y<?=$i?>_fees2" /> </td><td > <input type="text" name="y<?=$i?>_due2" /> </td> </tr>
</table>
</td>
</tr>
</table>
<? }
}
?>
can anyone help?
You need to hit the ajax_redirect.php every time you change the 'no. of years', your ajax_redirect.php must execute the select query to associated table with changing where clause. Whatever HTML element you are using, must be replaced with new value(s) accordingly.
NOTE: Here I assume you are fetching data from DB thats why I have
asked you to use select query on asssociated table with changing where
clause.
UPDATE: Your Problem is with document.getElementByClassName('fees_dets') because there no such function as getElementByClassName... you should your table in a DIV with a attribute ID and use getElementById

jquery show button when checkbox is checked

I want to show the delete button below the table if any of the row checkboxes are checked. Otherwise, the button should stay hidden.
Table I want it to be implemented.
screenshot of table:
This is the html table code behind.
<table id="datatable_example" class="responsive table table-striped table-bordered" style="width:100%;margin-bottom:0; ">
<thead>
<tr>
<th style="width:0px; padding-right:0px;" class="jv no_sort"> <label class="checkbox ">
<input type="checkbox">
</label>
</th>
<th style="width:200px;" class=""> Skill </th>
<th style="width:300px;" class="to_hide_phone no_sort"> Description </th>
<th class=""> Rating </th>
<th style="width:200px;" class="ue no_sort"> Date </th>
<th class="ms no_sort "> Actions </th>
</tr>
</thead>
<tbody>
<?php echo $skills; ?>
</tbody>
</table>
This is the skills variable:
$skills="";
while($row=mysql_fetch_array($skills_list))
{
$showskillid=$row['skill_id'];
$showskillname=$row['skill_name'];
$showskilldescription=$row['skill_desc'];
$showskillrating=$row['skill_rating'];
$showskill_lastupdated=$row['last_updated'];
$skills .="<tr>
<td><label class=\"checkbox\">
<input type=\"checkbox\" class=\"chkboxes\" name=\"ids[]\">
</label></td>
<td class=\"to_hide_phone\"> $showskillname </td>
<td class=\"to_hide_phone\">$showskilldescription</td>
<td> $showskillrating </td>
<td> $showskill_lastupdated </td>
<td class=\"ms\"><div class=\"btn-group1\"> <i class=\"gicon-edit\"></i> <a class=\"btn btn-small\" rel=\"tooltip\" data-placement=\"top\" data-original-title=\"View\"><i class=\"gicon-eye-open\"></i></a> <a class=\"btn btn-small\" rel=\"tooltip\" data-placement=\"bottom\" data-original-title=\"Remove\"><i class=\"gicon-remove \"></i></a> </div></td>
</tr>";
}
The Jquery which I tried but I know I am newbie in jquery so need help to fix it.
<script type="text/javascript">
$(document).ready(function() {
$('.delbtn').hide();
});
$('.chkboxes').click(function(){
$('.delbtn').toggle();
});
</script>
Jquery I tried it somehow works like when I click one checkbox the button shows up and when I go and click the other checkbox the button disappears?
I know toggle should not be used here..
What instead should I use here to fix this issue, or if there is more better option to fix it I am open to it..
This will show the Delete button when at least one checkbox is checked.
$('.chkboxes').change(function(){
$('.delbtn').toggle($('.chkboxes:checked').length > 0);
});
But instead of hiding it, the more common approach is to enable and disable the button:
$('.delbtn').prop('disabled', true);
$('.chkboxes').change(function(){
$('.delbtn').prop('disabled', $('.chkboxes:checked').length == 0);
});

Categories