Combine checkbox sum from Javascript into my PHP database table - javascript

This is my code for my the current database populated table. I have found a previous Javascript code to help me sum values from multiple checkboxes.
The values I want summed are only from my Asset_Cost
<body>
<table cellpadding="0" cellspacing="0" border="0" id="table" class="sortable">
<thead>
<tr>
<th><h3>Asset ID</h3></th>
<th><h3>Vendor</h3></th>
<th><h3>Hardware </h3></th>
<th><h3>Cost</h3></th>
<th><h3>Purchase Date</h3></th>
<th><h3>Select Cost</h3></th>
</tr>
</thead>
<tbody>
<?php
$result = mysql_query("SELECT * FROM Asset");
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Asset_ID']."</td>";
echo "<td>" . $row['Vendor_Name'] . "</td>";
echo "<td>" . $row['Hardware_ID'] . "</td>";
echo "<td>" . $row['Asset_Cost'] . "</td>";
echo "<td>" . $row['DateOfPurchase'] . "</td>";
echo "</tr>";
}
echo "</table>";
I want to to integrate the checkbox sum from this code:
<?php
include('connect1.php');
$result = $db->prepare("SELECT * FROM Asset");
$result->bindParam(':userid', $res);
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<INPUT TYPE="checkbox" NAME="items[]" value="<?php echo $row['Asset_Cost'] ?>" id="sum_m_<?php echo $i ?>" onclick="UpdateCost()">
<td>
<?php echo $row['Asset_ID'] ?>
<?php echo $row['Vendor_Name'] ?>
<?php echo $row['Hardware_ID'] ?>
<?php echo $row['Asset_Cost'] ?>
</td>
<br>
<?php
}
?>
<br>
Total Cost : <input type="text" name="sen" id="totalcost" value="">
Here is the javascript
<script type="text/javascript">
function UpdateCost() {
var sum = 0;
var gn, elem;
for (i=0; i<<?php echo $calculated ?>; i++) {
gn = 'sum_m_'+i;
elem = document.getElementById(gn);
if (elem.checked == true) { sum += Number(elem.value); }
}
document.getElementById('totalcost' ).value = sum.toFixed(0);
}
window.onload=UpdateCost
</script>

<body>
<table cellpadding="0" cellspacing="0" border="0" id="table" class="sortable">
<thead>
<tr>
<th><h3>Select</h3></th>
<th><h3>Asset ID</h3></th>
<th><h3>Vendor</h3></th>
<th><h3>Hardware </h3></th>
<th><h3>Cost</h3></th>
<th><h3>Purchase Date</h3></th>
</tr>
</thead>
<tbody>
<?php
$result = mysql_query("SELECT * FROM Asset");
$calculated = mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><INPUT TYPE='checkbox' NAME='items[]' value='". $row['Asset_Cost'] ."' id='sum_m_$i' onclick='UpdateCost()'> </td>";
echo "<td>" . $row['Asset_ID']."</td>";
echo "<td>" . $row['Vendor_Name'] . "</td>";
echo "<td>" . $row['Hardware_ID'] . "</td>";
echo "<td>" . $row['Asset_Cost'] . "</td>";
echo "<td>" . $row['DateOfPurchase'] . "</td>";
echo "</tr>\n";
}
?>
<br>
Total Cost : <input type="text" name="sen" id="totalcost" value="">
<script type="text/javascript">
function UpdateCost() {
var sum = 0;
var gn, elem;
for (i=0; i<<?php echo $calculated ?>; i++) {
gn = 'sum_m_'+i;
elem = document.getElementById(gn);
if (elem.checked == true) { sum += Number(elem.value); }
}
document.getElementById('totalcost' ).value = sum.toFixed(0);
}
window.onload=UpdateCost();
</script>
</body>
Mostly things are as it is in your code. Only gave $calculated value equal to the number of rows in your mysql result. Hope this helps..

Related

How to send php Select box value?

The following code will output as shown in the attached picture.
Select the value of the Select box in test16 and press the "test19" button, the value of the Select box you want to modify.I want to make the value delivered to php.
I want to know how to deliver the value of the select box in php and how to receive the value delivered.
<?php
if($result = mysqli_query($link, $sql)){
$test = 'test';
if(mysqli_num_rows($result) > 0){
echo "<table id='datatable1' class = table style = 'width: 100%; background-color:#dee2e6'>";
echo "<thead >";
echo "<tr>";
echo "<th>No</th>";
echo "<th>test1</th>";
echo "<th>test2</th>";
echo "<th>test3</th>";
echo "<th>test4</th>";
echo "<th>test5</th>";
echo "<th>test6</th>";
echo "<th>test7</th>";
echo "<th>test8</th>";
echo "<th>test9</th>";
echo "<th>test10</th>";
echo "<th>test11</th>";
echo "<th>test12</th>";
echo "<th>test13</th>";
echo "<th>test14</th>";
echo "<th>test15</th>";
echo "<th>test16</th>";
echo "<th>test17</th>";
echo "<th>test18</th>";
echo "<th>test19</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['chart_num'] . "</td>";
echo "<td>" . $row['chart_name'] . "</td>";
echo "<td>" . $row['visit'] . "</td>";
echo "<td>" . number_format($row['total_medical_bills']) . "</td>";
echo "<td>" . number_format($row['total_amount']) . "</td>";
echo "<td>" . number_format($row['amount_asked']) . "</td>";
echo "<td style = 'font-weight: bold'>" . number_format($row['medical_bills_payment']) . "</td>";
echo "<td style = 'font-weight: bold'>" . number_format($row['personal_liability_amount']) . "</td>";
echo "<td style = 'font-weight: bold'>" . number_format($row['non_payment']) . "</td>";
echo "<td>" . $row['insurance_division'] . "</td>";
echo "<td>" . $row['division'] . "</td>";
echo "<td>" . number_format($row['cash_amount_received']) . "</td>";
echo "<td>" . number_format($row['card_amount_received']) . "</td>";
echo "<td style = 'font-weight: bold'>" . number_format($row['treatment_fees_difference']) . "</td>";
echo "<td>" . $row['treatment_fees_check_division'] . "</td>";
echo "<td><select name='". $test ."'>";
echo "<option value='". $test ."'>First</option>";
echo "<option value='". $test ."'>Second</option>";
echo "<option value='". $test ."'>Third</option>";
echo "</select></td>";
echo "<td>" . $row['treatment_fees_check'] . "</td>";
echo "<td>" . $row['treatment_fees_check_modify'] . "</td>";
echo "<td>";
if($row['medical_bills_payment'] == $row['personal_liability_amount'] + $row['non_payment']){
echo "수정불가";
}
else{
echo "<a href='treatment_fees_check_update.php?id=". $row['id'] ." title='수정' data-toggle='tooltip'><span class='icon ion-edit'></span></a>";
}
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
mysqli_free_result($result);
}
}
else{
echo "ERROR: Could not able to execute $sql2. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
Since you can have a lot of select boxes, you will need give them an ID so we can reference the respective field. Let's add/change your code as below:
$rowCount=0;
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
...
echo "<td><select id='MySelect".$rowCount."' name='". $test ."'>";
echo "<option value='". $test ."'>First</option>";
echo "<option value='". $test ."'>Second</option>";
echo "<option value='". $test ."'>Third</option>";
echo "</select></td>";
...
echo "<td>";
if($row['medical_bills_payment'] == $row['personal_liability_amount'] + $row['non_payment']){
echo "수정불가";
}
else{
echo '<span class="icon ion-edit"></span>';
}
echo "</td>";
$rowCount++;
EDIT: I missed to add $rowCount++; before close while block. Sorry.
At the end of your document, before close body tag you add:
<script>
function GetMySelectValue(select,objId){
document.location.href= "yoursite.com/treatment_fees_check_update.php?id="+objId+"&myselect="+document.getElementById(select).value;
}
</script>
In treatment_fees_check_update.php you get id and select value:
$rowid=$_GET['id'];
$myselect=$_GET['myselect'];
Hope it can help you.
The principles for what you need is to asign a id to each one of the options in the dropdown, so you can send the id of tha specific value, compare with that same id in the database if exists then you will have the column with the value that you need to modify in the database, but first bring and add the proper id of each one of the options in the select.

JQuery: How to pass PHP data from div to div

As for my HTML code is
<div class="right">
<h2>Product Detail</h2>
<div id="righttop"></div>
<button id="Add">Add</button>
</div>
<div class="rightbot">
<h2>Shopping Car</h2>
<div id="rightbot"></div>
</div>
And the PHP is
mysqli_select_db($con,"products");
$sql="SELECT product_id, product_name, unit_price, unit_quantity, in_stock FROM products WHERE product_id = '".$productid."'";
$result = mysqli_query($con,$sql);
echo "<table>";
echo "<th>Product Name</th>";
echo "<th>Unit Price</th>";
echo "<th>Unit Quantity</th>";
echo "<th>In Stock</th>";
while($row = mysqli_fetch_array($result)) {
echo "<tr id='detail'>";
echo "<td>" . $row['product_name'] . "</td>";
echo "<td>" . $row['unit_price'] . "</td>";
echo "<td>" . $row['unit_quantity'] . "</td>";
echo "<td>" . $row['in_stock'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo $_POST['detail'];
mysqli_close($con);
?>
div#righttop is changed by another button.
How can I pass the current content from div#righttop to div#rightbot?
If you meant to transfer the contents of righttop to rightbot, here is a way. On click of the button, before changing anything in righttop, do this:
$('#rightbot').html($('#righttop').html());

I want to display 1 record instead of displaying the whole Database, PHP

dbconnect.php
<form method="post" action="a.php">
<select name="taskOption" id="cust-id" onchange="showUser(this.value)">
<?php
include 'orderSelect.php';
echo '<option>View Order</option>';
while($row = mysqli_fetch_array($result)):;?>
<option value='<?php echo $row[0]; ?>'><?php echo $row[1]; echo " ";
echo $row[2]; ?></option>
<?php endwhile; ?>
</select>
</form>
a.php
<?php
include 'connect.php';
$q = intval($_GET['q']);
$sql = "SELECT id, firstname, lastname,productOne, quantity, price
FROM orderlist";
$result = mysqli_query($conn, $sql);
echo "<table >
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Product</th>
<th>Quantity</th>
<th>Price</th>
<th>Total</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
$tquan = $row['quantity'];
$tprice = $row['price'];
$total = $tquan * $tprice;
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['productOne'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $total . "</td>";
echo "</tr>";
}
echo "</table>";
?>
I got it working. IT display the record/ users that I want to see when something is selected from the drop down list, but the problem is it's displaying the entire database data. I only want to get that specific that of the selected person. Any Ideas how to solve this problem? I would appreciate it.
Image Example
I only wanted the first row to be selected when I select from the dropdown list
Your SELECT is of the whole database you need to select something exact.
For example:
$sql = "SELECT 'id' FROM Users WHERE username='$name'";
That is an example of using the WHERE clause.
In addition to what the above said.
You mentioned you only wanted 1 record.
Add " LIMIT 1" to the end of your query
OR
Change the 'while' command to an 'if' command and it will only run once.

How to add column to a table using javascript and take the column name from a textbox?

I wrote this code to add column to a table using javascript and take the column name from a html textbox. But this is not working.
JavaScript :-
<script type="text/javascript">
$(document).ready(function () {
$('#btnAdd').click(function () {
var count = 3,
first_row = $('#Row2');
while (count-- > 0) first_row.clone().appendTo('#blacklistgrid');
});`enter code here`
var myform = $('#myform'),
iter = 4;
$('#btnAddCol').click(function () {
var col_name = document.getElementID("txt_col_name").value;
myform.find('tr').each(function(){
var trow = $(this);
if(trow.index() === 0){
trow.append('<td>'+colname+'</td>');
}else{
trow.append('<td><input type="text" name="al'+iter+'"/></td>');
}
});
iter += 1;
});
});
</script>
HTML :-
<form name="myform" id="myform">
<table id="blacklistgrid">
<tr id="heading">
<td>Employee ID</td>
<td>Employee Name</td>
<td>Basic Salary</td>
<td>Mobile Allowances</td>
<td>Travel Allowences</td>
<td>Internet Allowences</td>
<td>Sales Commission</td>
<td>Bonus</td>
<td>Net Salary</td>
</tr>
<?php
$sql1="SELECT user_id, user_fname, user_lname
FROM ".$prefix."users_info
WHERE user_st='1'";
$employee_info = $wpdb->get_results($sql1);
//var_dump($employee_info);
foreach ($employee_info as $employee_info_row)
{
$user_id = $employee_info_row->user_id;
$user_fname = $employee_info_row->user_fname;
$user_lname = $employee_info_row->user_lname;
echo "<tr id=\"row\">";
echo "<td>";
echo "$user_id";
echo "</td>";
echo "<td>";
echo "$user_fname $user_lname";
echo "</td>";
echo "<td>";
echo "<input type=\"text\" name=\"bs\" />";
echo "</td>";
echo "<td>";
echo "<input type=\"text\" name=\"a1\" />";
echo "</td>";
echo "<td>";
echo "<input type=\"text\" name=\"a2\" />";
echo "</td>";
echo "<td>";
echo "<input type=\"text\" name=\"a3\" />";
echo "</td>";
echo "<td>";
echo "<input type=\"text\" name=\"sc\" />";
echo "</td>";
echo "<td>";
echo "<input type=\"text\" name=\"bns\" />";
echo "</td>";
echo "<td>";
echo "";
echo "</td>";
echo "</tr>";
}
?>
</table>
<input type="text" id="txt_col_name" name="add_col" />
<button type="button" id="btnAddCol">Add new column</button>
</br>
</br>
<input type="submit"></input>
</form>
I also want to add the new column between 'Internet Allowences' and 'Sales Commission'. Can anyone pls tell me how to do that. Thank You
Hope this help
jsfiddle.net/stormspirit/2zh64/1/

sending checkboxes value to database in php

I have a table in my page that first two columns of it come from a table in database, and third column is for checkbox, if user know the meaning of the words that are displayed in table, check them
I want to update my table in database in this way: for checked checkbox insert 1 to checking column in table and otherwise insert 0
how can I get checkbox value and insert it into right row in database
I have this code until now:
<?php
$con = mysql_connect("localhost", "root", "")
or die(mysql_error());
if (!$con) {
die('Could not connect to MySQL: ' . mysql_error());
}
mysql_select_db("project", $con)
or die(mysql_error());
$result = mysql_query("select * from words");
echo "<table border='1'>
<tr>
<th>word</th>
<th>meaning</th>
<th>checking</th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['word'] . "</td>";
echo "<td>". "<div class='hiding' style='display:none'>".$row['meaning']."</div>"."</td>";
echo "<td>";
echo "<input name=\"fahimeh\" type=\"checkbox\" value=\"\"> ";
echo "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
<button onclick="ShowMeanings()">ShowMeanings</button>
<button onclick="feedback()">sendfeedback</button>
and it is my javascript code:
<script type="text/javascript">
function ShowMeanings(){
var hidings = document.getElementsByClassName('hiding');
for (var i=0; i<hidings.length; i++){
hidings[i].style.display = 'block';
}
}
</script>
I dont know how can I write feedback(), and how can I get checkbox value and insert it into right row in database
This should work :)
<?php
$con = mysql_connect("localhost", "root", "")
or die(mysql_error());
if (!$con) {
die('Could not connect to MySQL: ' . mysql_error());
}
mysql_select_db("project", $con)
or die(mysql_error());
$result = mysql_query("SELECT * FROM words");
echo "<table border='1'>
<tr>
<th>word</th>
<th>meaning</th>
<th>checking</th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['word'] . "</td>";
echo "<td>". "<div class='hiding' style='display:none'>".$row['meaning']."</div>"."</td>";
echo "<td>";
echo "<input name=\"fahimeh\" type=\"checkbox\" name='" . $row['id'] . "' value=\"\"> ";
echo "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
<button onclick="ShowMeanings()">ShowMeanings</button>
<button onclick="feedback()">sendfeedback</button>

Categories