Collect the values of checkboxes with jQuery - php

I have a website to predict football results. Each soccer match has three checkboxes.
1-First team win(1checkbox)
2-Second team win(1checkbox)
3-Equal(1checkbox)
There are 10 soccer matches on one page. So, on a page, I have 30 checkboxes. The user can select all three checkboxes for each football match.
I need jQuery code to work like this website:
https://web.archive.org/web/20100529040418/http://www.bwin90.com/
if you select two or three checkboxes in soccer match, the numbers are exponentially rising.
My html and php codes:
<form method="POST">
<div class="form-group mt-4">
<div class="w-100 p-3">
<table class="table">
<thead class="thead-dark">
<tr>
<th class="text-center" scope="col"></th>
<th class="text-center" scope="col">team1</th>
<th class="text-center" scope="col">equal</th>
<th class="text-center" scope="col">team2</th>
<th class="text-center" scope="col"></th>
</tr>
</thead>
<tbody>
<form>
<?php
$get_teams1="SELECT * FROM teams WHERE show_match=1 ORDER BY team_id DESC";
$get_teams2 = mysqli_query($conn, $get_teams1);
while($get_teams3=mysqli_fetch_assoc($get_teams2))
{
echo'
<tr>
<td class="text-right"><input type="checkbox" class="qty1 form-check-input" value="20" id="exampleCheck1"></td><td class="text-center">'.$get_teams3["mizban"].'</td>
<td class="text-center"><input type="checkbox" class="qty1 form-check-input" id="exampleCheck1"></td>
<td class="text-center">'.$get_teams3["mihman"].'</td><td class="text-right"><input type="checkbox" class="qty1 form-check-input" id="exampleCheck1"></td>
</tr>
';
}
?>
<input type="text" class="total" value="" />
</form>
</tbody>
</table>
<button type="button" class="btn btn-dark mt-2 w-100" name="register3" />
<h5>ثبت فرم</h5>
</button>
</div>
</div>
</form>
My jquery codes:
<script>
$(document).ready(function(){
$(document).on("change", ".qty1", function() {
var sum = 0;
$(".qty1").each(function(){
sum += +$('.qty1').val();
});
$(".total").val(sum);
});
});
</script>
At the end, I want to display the cost amount instantly by selecting checkboxes.

you May try something like this
Html Code
<table class="table table-bordered" id="filltable">
<tr>
<td>demo <input type="checkbox" id="chk1" value="10" onchange="add(1,10)"/></td>
<td>demo1 <input type="checkbox" id="chk2" value="10" onchange="add(2,10)" /></td>
<td>demo2 <input type="checkbox" id="chk3" value="10" onchange="add(3,10)"/></td>
<td>demo3 <input type="checkbox" id="chk4" value="10" onchange="add(4,10)"/></td>
</tr>
</table>
and Jquery Code:
var sum = 0;
function add(id,value) {
if ($('#chk' + id).prop('checked') == true) {
sum= sum + value
}
else {
sum= sum - value
}
alert(sum);
}

Related

PHP/MYSQL: Saving array data in table rows to mysql database using checkbox

I have an order table in my project. I am pulling the products from the database to this table using foreach. I show the quantity and unit price calculation information on the table instantly with jquery. I want to save the data in this table from the row selected with the checkbox to the database. I tried a few things with foreach but it looped so it created a new record for each row in the database. I want to print arrays with implode using commas between them. For example, the data in the rows entered in the quantity field in the table should be entered in the quantity field in the database as 1,2,3,4. If I have to explain briefly, I want to make an insert in one go.
Table:
Table & Form Code:
<form action="" method="POST">
<table class="table table-sm mb-3 text-center align-middle">
<thead>
<tr>
<th>Choose</th>
<th>Product Name</th>
<th width="137px">Quantity</th>
<th>Unit Price</th>
<th>Total Price</th>
</tr>
</thead>
<tbody>
<?php foreach($ProductTbl as $product){ ?>
<tr>
<th scope="row">
<div class="form-check form-check-success">
<input class="form-check-input" name="check[]" type="checkbox" value="
<?= $product-> productid">
</div>
</th>
<td>
<?= $product->productname ?>
</td>
<td>
<input class=" w-25 text-center quantity" type="text" name="quantity[]" value="0">
</td>
<td>
<input class="w-25 text-center unitprice" type="text" name="unitprice[]" value="
<?= $product->unitprice ?>" disabled="disabled"> €
</td>
<td>
<input class="w-25 text-center totalprice" type="text" name="totalprice[]" value="" disabled="disabled"> €
</td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="text-center">
<button class="btn btn-success col-md-2" type="submit" name="add">Offer Preview</button>
<button class="btn btn-warning col-md-1" type="reset">Reset</button>
<a href="#">
<button class="btn btn-danger col-md-1" type="button">Cancel</button>
</a>
</div>
</form>
//The code structure I tried
if(isset($_POST['add'])){
$check = $_POST['check'];
$quantity = implode(',', $_POST['quantity']);
$totalprice = implode(',', $_POST['totalprice']);
if (!empty($check)) {
foreach ($check as $chk => $value){
// I printed it for testing.
echo $value.' - product id in the checked checkbox<br>';
print_r($quantity);
print_r($totalprice);
}
}
}
How does this code work the way I want?

Conditional Form Fields in Dynamic Data Table

I have table with a repeating recordset area. I am using Data Tables for the table design. For each record, there is a form with a drop-down menu. Based on the drop down menu choice, I need it to possibly display a text area to add more info. I have this working for the first record but, it doesn't show for the remaining records. Any advice would be helpful. The text area should only show if thy choose Not Met or Partially Met from the drop down menu. When they hit the save button, it will get inserted into the database.
Here is my code:
<table id="example" class="table table-responsive table-striped table-bordered" width="100%" border="0" cellspacing="0">
<thead>
<tr>
<th align="left" valign="top">Select a Status</th>
<th align="left" valign="top">Clause #</th>
<th align="left" valign="top">Clause</th>
</tr>
</thead><tbody>
<?php
$wa_startindex = 0;
while(!$pod_certify->atEnd()) {
$wa_startindex = $pod_certify->Index;
?>
<tr>
<td width="15%" valign="top"><form name="form2" method="post"><?php if ($pod_certify->getColumnVal("thegeps_clauseID") != $pod_certify->getColumnVal("thegeps_clauseID2")) {?><input type="hidden" name="geps_clauseID" id="geps_clauseID" value="<?php echo($pod_certify->getColumnVal("thegeps_clauseID")); ?>" required><input type="hidden" name="userID" id="userID" value="<?php echo($logged_user->getColumnVal("userID")); ?>" required><input type="hidden" name="dataID" id="dataID" value="<?php echo $_GET['dataID']; ?>" required>
<select name="status2" id="status2" class="form-control" required>
<option></option>
<option value="Met">Met</option>
<option value="Partially Met">Partially Met</option>
<option value="Not Met">Not Met</option>
</select>
<script>
$('#status2').on('change',function(){
if( $(this).val()=='Partially Met'){
$("#otherType4").show()
}
else{
$("#otherType4").hide()
}
});
</script>
<script>
$('#status2').on('change',function(){
if( $(this).val()=='Not Met'){
$("#otherType5").show()
}
else{
$("#otherType5").hide()
}
});
</script> <div style="display:none;" id="otherType5"><br><label><strong>Please Explain:</strong></label><br><textarea name="explanation" id="explanation" class="form-control"></textarea></div><div style="display:none;" id="otherType4"><br><label><strong>Please Explain:</strong></label><br><textarea name="explanation" id="explanation" class="form-control"></textarea></div><button type="submit" name="submit" id="submit" class="btn btn-warning btn-sm mb-2">Save</button><?php }?><?php if ($pod_certify->getColumnVal("thegeps_clauseID") == $pod_certify->getColumnVal("thegeps_clauseID2")) {?><input id="certifyID" name="certifyID" type="hidden" value="<?php echo($pod_certify->getColumnVal("certifyID")); ?>"><button type="submit" name="submit2" id="submit2" class="btn btn-danger btn-sm mb-2">Delete <?php echo($pod_certify->getColumnVal("status")); ?> Certification</button><?php }?></form></td>
<td valign="top"><?php echo($pod_certify->getColumnVal("article_number")); ?></td>
<td valign="top"><?php echo($pod_certify->getColumnVal("geps_clause")); ?></td>
</tr>
<?php
$pod_certify->moveNext();
}
$pod_certify->moveFirst(); //return RS to first record
unset($wa_startindex);
unset($wa_repeatcount);
?></tbody>
</table>

Send multiple data using checkbox via ajax

I have created a form which contains a table, the purpose of this is table is to select the desired fees to manage
<form method="POST" action="manage_fees.php" id="manage_form">
<table class="table table-striped table-bordered table-hover" id="table">
<thead>
<tr class="p-4">
<th scope="col">Select</th>
<th scope="col">School fees</th>
<th scope="col">Amount</th>
<th scope="col">type</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input check_amount" name="local_fees">
<label class="custom-control-label" for="check_amount"></label>
</div>
</td>
<td name="selected_fees">Local fees</td>
<td name="amount">200</td>
<td>University fees</td>
</tr>
</tbody>
</table>
<div class="modal-footer">
<button type="submit" class="btn btn-primary" name="submit">Create</button>
</div>
</form>
Now I want to record it all data("fee name and total payment") that has checked in checkbox table and save in database using ajax.
Once the fees is selected it will display on the on input just like this
<div class="form-group">
label for="fs">Fees selected</label>
<input type="text" class="form-control" id="fs" name="fs" required disabled>
</div>
<div class="form-group">
<label for="tp">Total payment</label>
<input type="number" class="form-control" id="tp" name="tp" required disabled>
</div>
Now, the problem is that im having trouble saving the data that has been checked in table seems there is no data that has been pass in the input.
Sumbit.php
if(isset($_POST['submit'])){
$fs = $_POST['fs'];
$tp = $_POST['tp'];
$result = $connect->query("INSERT INTO manage_fees (fs,tp) VALUES ('$fs','$tp')") or die($connect->error());
}
Instead of this way how will send the data using ajax?

How to insert duplicated rows of different data into Database in PHP & HTML

This is example html code i used. Basically, when I click 'Add New', the new row appear and I typed in the data. But the problem is only the data of last row (if i add 3 rows, it is only inserting 3rd row's data) is inserting into database
currently i used the simple sql query method to insert into database and I don't know how to modify the sql query or codes to make it store all the duplicated rows' data into databse.
If you can help me with this, it'd be most appreciated. Thanks !!
http://jsfiddle.net/2HGdv/13/
Here is how its work on jsfiddle.
one point is I don't use that <? $i ?> beside my variables in my actual html codes because when I add it, I got errors.
<br>
<table id="maintable" width="50%" cellpadding="0" cellspacing="0" class="pdzn_tbl1" border="#729111 1px solid">
<tr>
<th align="center">Roll No</th>
<th align="center">First Name</th>
<th align="center">Last Name</th>
</tr>
<tr id="rows">
<div style="padding-left: 5px">
<td style="padding:5px;">
<input type="text" name="rollno<? $i ?>" />
</td>
<td style="padding:5px;">
<input type="text" name="firstname<? $i ?>" />
</td>
<td style="padding:5px;">
<input type="text" name="lastname<? $i ?>" />
</td>
</div>
</tr>
</table>
<br><div id="add_new">ADD NEW
</div>
This is the script for duplicating the rows.
$("#add_new").click(function () {
$("#maintable").each(function () {
var tds = '<tr>';
jQuery.each($('tr:last td', this), function () {
tds += '<td>' + $(this).html() + '</td>';
});
tds += '</tr>';
if ($('tbody', this).length > 0) {
$('tbody', this).append(tds);
} else {
$(this).append(tds);
}
});
});
You overwrite your data because input attribute name remains the same. You need to make an HTML array that holds each row. Then in your js you will append one row with index the total .rows like var total_rows = $(".rows").length;
<form method="post" action="stack.php">
<table id="maintable" width="50%" cellpadding="0" cellspacing="0" class="pdzn_tbl1" border="#729111 1px solid">
<tr>
<th align="center">Roll No</th>
<th align="center">First Name</th>
<th align="center">Last Name</th>
</tr>
<tr class="rows">
<td style="padding:5px;">
<input type="text" name="item[0][rollno]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[0][firstname]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[0][lastname]" />
</td>
</tr>
<tr class="rows">
<td style="padding:5px;">
<input type="text" name="item[1][rollno]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[1][firstname]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[1][lastname]" />
</td>
</tr>
</table>
<div id="add_new">ADD NEW</div>
<input type="submit" value="Save all items" name="saveButton">
</form>
<?php
if (isset($_POST['item']) && is_array($_POST['item'])) {
$items = $_POST['item'];
$i = 0;
foreach($items as $key => $value) {
echo 'Item '.$i++.': '.$value['rollno'].' '.$value['firstname'].' '.$value['lastname'].'<br />';
}
}
?>
So your next row will be the following:
<tr class="rows">
<td style="padding:5px;">
<input type="text" name="item[2][rollno]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[2][firstname]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[3][lastname]" />
</td>
</tr>

Validate dynamically created text inputs in jQuery

What I presently have is this code which displays all the invoices under a specific catalogue. On change of the catalogue dropdown, the table should be populated based on the selected dropdown value. The table displays the invoices pertaining to the selected catalogue with 3 textboxes for each recordset. And what I need is to validate those textboxes. Lets say there are 3 invoices/records for a specific catalogue, then there should be 9 textboxes.
The View Page contains:-
<script type="text/javascript">
$(document).ready(function(){
$('#catalogue').change(function(){
$.post('../controller/valuation.php', {catalogue:valuation_form.catalogue.value},
function(result){
$('#valuationResult').html(result).show();
})
});
});
</script>
<form action="" id="valuation_form" name="valuation_form">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="16%">Catalogue Code</td>
<td width="15%">
<select name="catalogue" id="catalogue" style="width:75px;">
<option></option>
<?php
require_once '../model/catalogue.php';
#$result=Catalogue::getAllCatalogues();
while($value=mysql_fetch_assoc($result)){
?>
<option value="<?php echo $value['catalogue_id']; ?>">
<?php echo $value['catalogue_code'] ?>
</option>
<?php } ?>
</select>
</td>
</tr>
</table>
</form>
<div class="atable">
<form action="../controller/valuation.php" method="POST" enctype="multipart/form-data">
<div id="valuationResult"></div>
</form>
</div>
The Controller Page contains:-
function getValuationDetails(){
require_once '../model/sales.php';
$catalogue=$_POST['catalogue'];
$obj=new Sales();
$result=$obj->getValuationEntryLotsForCatalogue($catalogue);
#$num=mysql_num_rows(#$result);
if($num>0){
$table='<table id="tabled" class="tftable" border="1">
<thead>
<tr>
<th style="display:none" width="0%">INVOICE ID</th>
<th width="6%">LOT #</th>
<th width="15%">SELLING MARK</th>
<th width="9%">INVOICE #</th>
<th width="8%">PACKAGES</th>
<th width="8%">GRADE</th>
<th width="13%">NET WEIGHT(Kg)</th>
<th style="text-align:center">DESCRIPTION</th>
<th width="11%" style="text-align:center;">VALUATION </th>
</tr>
</thead>';
while($value=mysql_fetch_assoc($result)){
$table.='<tr>
<td style="display:none">
<input type="text" id="invoice" name="invoice[]" value="'.$value['invoice_id'].'"/>
</td>
<td>'.$value['lot_no'].'</td>
<td>'.$value['selling_mark'].'</td>
<td>'.$value['ref_invoice_no'].'</td>
<td>'.$value['no_of_packages'].'</td>
<td>'.$value['grade_desc'].'</td>
<td style="text-align:right;">'.$value['total_net_qty'].' </td>
<td>
<textarea style="width:270px;max-width:270px;" class="description" name="description[]"></textarea>
</td>
<td>
<input type="text" class="value1" name="value1[]" size="2">
<input type="text" class="value2" name="value2[]" size="2">
</td>
</tr>';
}
$table.='<tr><td colspan="9" style="text-align:right">
<input type="hidden" name="action" value="valuation_entry"/>
<input type="submit" name="save" id="save" value="Save"/>
<input type="reset" value="Clear"/>
</td></tr>';
echo $table;
}
else{
echo "";
}
}
The three textboxes/textareas are as follows:-
1. Description- textarea that allows only letters.
2. Value1- textbox that allows only numbers.
3. Value1- textbox that allows only numbers.
*All the 3 cannot be empty.
I have tried all sorts of options to try this but unfortunately I cannot figure out what and where I am missing.
Any help is very much appreciated!!!
Use class "required" to mark the fields that cannot be empty.
Then on Jquery you should loop through these elements:
var error = false;
$('.required').each(function(i){
if(!$(this).val()){
$(this).css('border', '2px solid red');
error = true;
}else{
$(this).css('border','1px solid #BBBBBB');
}
});
if(error) {
alert('All red fields are required!!!!!');
}

Categories