Calculate my dynamic and my static input together - php

I have a function static who can calculate all my static input:
$('document').ready(function() {
$('#input5,#tempsam ').each(function () {
$(this).on('change',recalculate);
});
});
function recalculate () {
if(isNaN(this.value))
alert("Please enter a number");
else {
var b = 0;
if (($('#input5').val() !== b) && ($('#input2').val() !== b) && ($('#tempsam').val() !== b )) {
var a = 40;
var value1 = $('#input1').val() == "" ? 0 : parseFloat($('#input1').val());
var value7 = $('#tempsam').val() == "" ? 0 : parseFloat($('#tempsam').val());
var total = value5 + value2 + value7
$('#result').val(total);
}
}
}
How I calculate my dynamic input and my static one?
Here is how I calculate my static one each day:
//jeudi calcul
function checkfieldjeudi()
{
var dynamicjeudi = document.getElementsByClassName("dynamicjeudi");
var itemCount5 = dynamicjeudi.length;
var total = 0;
for(var i = 0; i < itemCount5; i++) {
total = total + parseFloat(dynamicjeudi[i].value);
total = total + parseFloat(document.getElementById('input5').value);
}
document.getElementById('totaljeudi').value = total;
}
checkfieldjeudi();
How I make dynamic jeudi
$('#calculTempsdivjeu').append(
( '<input id="calculTempsdivjeu' + counterjeudi + '" name="calculTempsdivjeu[]' + '" type="number" onchange="checkfieldjeudi();" size="10" min="0" max="24" value="0" class="dynamicjeudi" onblur="autre();" onfocus="enter();"/>')
) //désactive champs précédent.
Here my static jeudi
<div id="calculTempsdivjeu">
<input step="any" type="number" id="totaljeudi" class="totaljeudi" onchange="checkfieldjeudi();" name="totaljeudi" size="10" min="0" max="24" value="0" onblur="autre();" onfocus="enter();"/>
<input step="any" type="number" onblur="autre();" onfocus="enter();" onchange="checkfieldjeudi();" id="input5" class="temps" name="tempsje" max="24" min="0" value="0" />
</div>

Related

$data[] = $row array give undefined in php

I used an array to print the data in several dynamically generated drop-down lists, but the result is very disappointing and does not bring the expected values from the database in the drop-down lists that I generate when needed, noting that the first drop-down list, which is outside the dynamic generation context, works well and brings the expected data, but the problem is Any new list generated dynamically, the values will only appear with the word "undefined" Any solution will be greatly appreciated...
here problem as GIF amage
php code:
`
<td class="td"><select class="form-control select_acount" name="account[]"required>
<option value="">--الحساب--</option>
<?php
$query = "select * from sah7atmain_acouts WHERE company like '".$multi."'";
$result = mysqli_query($con,$query);
$data = array();
if($result)
{
while($row = mysqli_fetch_assoc($result)){
$data[] = $row;
?>
<?php echo '<option value="'.$row['acount_mounte'].'">'.$row['acount_name'].'</option>'?>;
<?php
}
} else{
}
?>
</select>
</td>
jquery dynamic genaration code:
var data = '<?php echo $data; ?>';
var product_dd = "";
product_dd += '<select class="form-control select_acount" name="account[]"required>';
product_dd += '<option value="">--الحساب--</option>';
if(data.length > 0){
for(let i = 0; i < data.length; i ++) {
product_dd += `<option value="${data[i]['acount_mounte']}">${data[i]['acount_name']}</option>`;
}
}
product_dd += "</select>";
var i = 0;
$("#add-btn").click(function() {
++i;
$("#dynamicAddRemove").append('<tr><td class="td">'+product_dd+'</td> <td class="td"><input type="number" name="debtor[' + i + ']" id="fname"class="form-control debtor arabicNumbers" onkeydown="return event.keyCode !== 69" required></td> <td class="td"><input type="number" name="creditor[' + i + ']" id="james" class="form-control creditor arabicNumbers" onkeydown="return event.keyCode !== 69" required></td> <td class="td"><input type="text" name="description[' + i + ']" class="form-control" required></td> <td> <input type="text" name="mount[' + i + ']" class="form-control mount" required> </td><button type="button" name="add" class="btn btn-danger remove-tr"><i class="fas fa-trash-alt"></i></button></td></tr>');
});
$(document).on('click', '.remove-tr', function() {
$(this).parents('tr').remove();
});
`

Alternative way in (.on(input, function))

I am having a problem with this. My code is working fine but I use it to update then now I have a problem because value is already set.
In this code :
$('#principal').on('input', function() {
calculate();
});
$('#interest').on('input', function() {
calculate();
});
it works when you input a value on a field. But what if I have this code :
<input class="form-control number" name="principal" id="principal" type="text" value="<?php print $fetch['principal']; ?>" required/>
<input class="form-control number" name="interest" id="interest" type="text" value="<?php print $fetch['interest']; ?>" required/>
Value is already set. What code should I use in JQUERY to execute the code without type or editing value from input field.
Sample Output: Edit the value of Principal (What I want is to run compute without clicking or editing something)
$('#principal, #interest').on('input', function() {
calculate();
});
function calculate(){
var principal = $('#principal').val();
principal = principal.replace(/,/g, "");
principal = parseInt(principal);
var interest = $('#interest').val();
interest = interest.replace(/,/g, "");
interest = parseInt(interest);
var total = "";
var total_interest = "";
if(isNaN(interest) || isNaN(principal)){
total_interest = " ";
total = " ";
}
else{
total_interest = (interest / 100) * principal;
total = (interest / 100) * principal + principal;
}
var num = total;
num = addCommas(num);
$('#total').val(num);
var num_interest = total_interest;
num_interest = addCommas(num_interest);
$('#total_interest').val(num_interest);
function addCommas(nStr) {
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
}
$('.number').keyup(function(event){
// skip for arrow keys
if(event.which >= 37 && event.which <= 40) return;
// format number
$(this).val(function(index, value) {
return value
.replace(/\D/g, "")
.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Principal:
<input class="form-control number" name="principal" id="principal" type="text" value="9000" required/>
<br><br>
Interest:
<input class="form-control number" name="interest" id="interest" type="text" value="10" required/>
<br><br>
Total Interest:
<input class="form-control number" name="total_interest" id="total_interest" type="text" readonly/>
<br><br>
Total Payment
<input class="form-control number" name="total" id="total" type="text" readonly/>
Simply do this:
calculate();
...in code at the top level of your script. Make sure your script is at the end of body, just before the closing </body> tag.
Example:
function calculate() {
console.log(
"calculating with " +
$("#principal").val() +
" and " +
$("#interest").val()
);
}
$('#principal').on('input', function() {
calculate();
});
$('#interest').on('input', function() {
calculate();
});
calculate();
<input class="form-control number" name="principal" id="principal" type="text" value="42" required/>
<input class="form-control number" name="interest" id="interest" type="text" value="3.7" required/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Side note: You can simplify and consolidate your on calls:
$('#principal, #interest').on('input', calculate);
function calculate() {
console.log(
"calculating with " +
$("#principal").val() +
" and " +
$("#interest").val()
);
}
$('#principal, #interest').on('input', calculate);
calculate();
<input class="form-control number" name="principal" id="principal" type="text" value="42" required/>
<input class="form-control number" name="interest" id="interest" type="text" value="3.7" required/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Here's the snippet you added to your answer, with
calculate();
...added to the end:
$('#principal, #interest').on('input', function() {
calculate();
});
function calculate(){
var principal = $('#principal').val();
principal = principal.replace(/,/g, "");
principal = parseInt(principal);
var interest = $('#interest').val();
interest = interest.replace(/,/g, "");
interest = parseInt(interest);
var total = "";
var total_interest = "";
if(isNaN(interest) || isNaN(principal)){
total_interest = " ";
total = " ";
}
else{
total_interest = (interest / 100) * principal;
total = (interest / 100) * principal + principal;
}
var num = total;
num = addCommas(num);
$('#total').val(num);
var num_interest = total_interest;
num_interest = addCommas(num_interest);
$('#total_interest').val(num_interest);
function addCommas(nStr) {
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
}
$('.number').keyup(function(event){
// skip for arrow keys
if(event.which >= 37 && event.which <= 40) return;
// format number
$(this).val(function(index, value) {
return value
.replace(/\D/g, "")
.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
});
});
calculate();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Principal:
<input class="form-control number" name="principal" id="principal" type="text" value="9000" required/>
<br><br>
Interest:
<input class="form-control number" name="interest" id="interest" type="text" value="10" required/>
<br><br>
Total Interest:
<input class="form-control number" name="total_interest" id="total_interest" type="text" readonly/>
<br><br>
Total Payment
<input class="form-control number" name="total" id="total" type="text" readonly/>
You can calculate the result from the server
so from PHP side do the calculate() method and just print it into the result field. as you did with the values, or after your code loaded from js.
or you can use jQuery trigger
$(document).ready(function () {
$('#principal').trigger('input');
$('#interest').trigger('input');
}

why doesnt my form value's added to mysql / and how to get 2 decimals at calculation

1.can someone find why the form doesnt add/submit the values to database ?
i have tried adding values by the form and testing but its not getting in the database
2.and on calculation i get soms values with more than 2 decimals how to fix that because at the script/code i have decimals on 2 maybe someone ?
screenshot of decimals http://prntscr.com/5fbedf
<html>
<head>
</head><body>
<?php require "config.php" ; ?>
<?php require "admin_menu.php" ;?>
<?php
if(isset($_POST['verzenden']))
{
$id = addslashes($_POST['id']);
$flesnummer = addslashes($_POST['flesnummer']);
$begin_gewicht = addslashes($_POST['begin_gewicht']);
$verbruikt = addslashes($_POST['verbruikt']);
$eind_gewicht = addslashes($_POST['eind_gewicht']);
$verschil = addslashes($_POST['verschil']);
$tara_gewicht = addslashes($_POST['tara_gewicht']);
$over = addslashes($_POST['over']);
mysql_query("INSERT INTO Flesnummer (id, flesnummer, begin_gewicht, verbruikt, eind_gewicht, verschil, tara_gewicht, over) VALUES ('".$id."', '".$flesnummer."', '".$begin_gewicht."', '".$verbruikt."', '".$eind_gewicht."', '".$verschil."', '".$tara_gewicht."', '".$over."')");
echo '<br><br><br>Fles succesvol toegevoegd';
}
else
{
?>
<script>
function totaalIt() {
var x = document.getElementById("begin_gewicht").value;
var y = document.getElementById("tara_gewicht").value;
var z = document.getElementById("verbruikt").value;
var w = document.getElementById("eind_gewicht").value;
if ((isNumeric(x, true)) && (isNumeric(y, true)) && (isNumeric(z, true))&& (isNumeric(w, true))) {
x = parseFloat(x);
y = parseFloat(y);
z = parseFloat(z);
w = parseFloat(w);
var wverschil = x - y - z + y - w
var xyzover = x - y - z
var xyztotaal = x - y - z + y;
document.getElementById("verschil").value = wverschil;
document.getElementById("over").value = xyzover;
document.getElementById("totaal").value = xyztotaal;
if (confirm ("Formule: Begin gewicht - tara gewicht = vulling - verbruikt = vulling over + tara gewicht = totaal - eind gewicht = verschil(verlies) ?")) {
document.getElementById("form1").submit();
}
} else {
alert("Numeric Values must be entered.");
}
}
function isNumeric(sText, decimalAllowed) {
if (sText.length == 0) return false;
var validChars = "";
if (decimalAllowed) {
validChars = "0123456789.";
} else {
validChars = "0123456789";
}
var isNumber = true;
var charA;
var decimalCount = 0;
for (i = 0; i < sText.length && isNumber == true && decimalCount < 2; i++) {
charA = sText.charAt(i);
if (charA == ".") {
decimalCount += 1;
}
if (validChars.indexOf(charA) == -1) {
isNumber = false;
}
}
return isNumber;
}
</script>
<form name="form1" id="form1" action=" <?=$_SERVER['PHP_SELF']?> " method="POST">
<table>
<input type="hidden" name="id" >
<tr><td>Flesnummer:</td><td><input name="flesnummer" id="flesnummer" type="text" size"4">
<tr><td>Begin gewicht:</td><td><input name="begin_gewicht" type="text" id="begin_gewicht" size="4" />
Kg</td></tr>
<tr><td>Tara gewicht:</td><td><input name="tara_gewicht" type="text" id="tara_gewicht" size="4" />
Kg</td></tr>
<tr><td>Verbruik:</td><td><input name="verbruikt" type="text" id="verbruikt" size="4" />
Kg</td></tr>
<tr><td>Vulling over:</td><td><input name="over" type="text" id="over" size="4" readonly="true" />
Kg (automatisch berekend)</td></tr>
<tr><td>Eind gewicht:</td><td><input name="eind_gewicht" type="text" id="eind_gewicht" size="4" />
Kg</td></tr>
<tr><td>Eind gewicht: <img title="Eindgewicht volgens berekening" src="img/question.gif" width="15" height="15" onMouseDown="Eindgewicht volgens berekening" alt="Eindgewicht volgens berekening." /></td><td><input name="totaal" type="text" id="totaal" size="4" readonly="true" />
Kg (automatisch berekend)</td></tr>
<tr><td>Verschil(verlies):</td><td><input name="verschil" type="text" id="verschil" size="4" readonly="true" />
Kg (automatisch berekend)</td></tr>
<tr><td><input type="button" name="verzenden" id="verzenden" value="Verzenden" onClick="totaalIt()" /></td></tr>
</table></form>
<?
}
?>
On your form, try turning your button input to submit:
<input type="button" name="verzenden" id="verzenden" value="Verzenden" onClick="totaalIt()" />
TO:
<input type="submit" name="verzenden" id="verzenden" value="Verzenden" onClick="totaalIt()" />
Your button is not registering the name so it's not getting to the $_POST['verzenden'] in your check:
// There is no $_POST['verzenden'] with the button.
if(isset($_POST['verzenden'])) {

automatic submit form js

I'm trying to auto submit a form,(below it's only the form, its wrapped with a while{} for each DB entry to display).
It's working for all days of the week, but I can't submit the value for $totalhours, $holidayhours, $wagegross without a js event like onfocus().
Basically I want to submit this 3 inputs without an click or any other manual action.
any idea ?
<form id="weeklysheet" name="weeklysheet" method="post" action="hourly-function.php">
<tbody>
<tr style="line-height:0px;">
<td class="large">
<p class="employee_name"><?php echo $fullname?></p>
</td>
<td class="small">
<input name="mon" id="mon" type="text" class="days_input" value="<?php echo $data['mon'];?>"onchange="this.form.submit();"/>
</td>
<td class="small">
<input name="tue" id="tue" type="text" class="days_input" value="<?php echo $data['tue'];?>"onchange="this.form.submit();"/>
</td>
<td class="small">
<input name="wed" id="wed" type="text" class="days_input" value="<?php echo $data['wed'];?>"onchange="this.form.submit();"/>
</td>
<td class="small">
<input name="thu" id="thu" type="text" class="days_input" value="<?php echo $data['thu'];?>"onchange="this.form.submit();"/>
</td>
<td class="small">
<input name="fri" id="fri" type="text" class="days_input" value="<?php echo $data['fri'];?>"onchange="this.form.submit();"/>
</td>
<td class="small">
<input name="sat" id="sat" type="text" class="days_input" value="<?php echo $data['sat'];?>"onchange="this.form.submit();"/>
</td>
<td class="small">
<input name="sun" id="sun" type="text" class="days_input" value="<?php echo $data['sun'];?>"onchange="this.form.submit();"/>
</td >
<td class="small">
<input name="totalhours" id="totalhours" type="text" class="cumul_week_input" value="<?php echo $totalhours ?>"onfocus="this.form.submit();"/>
</td>
<td class="small">
<input name="holidayhours" type="text" class="cumul_week_input" value="<?php echo $holidayhours ?>"onfocus="this.form.submit();"/>
</td>
<td class="small">
<input name="wagegross" type="text" class="cumul_week_input" value="<?php echo $wagegross ?>"onfocus="this.form.submit();"/>
</td>
<td>
<input type="submit"name="save" id="save" value="Confirm" style="display:none;" />
</tr>
</tbody>
</form>
You could use jQuery to trigger the submit button's click action after the document has loaded like so:
1.) You can link the jQuery library from Google's Hosted Libraries:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
or, download it from jQuery.com to, say a "js" folder, and include it
<script src="js/jQuery.js"></script>
2.) Place this script tag at below the </form> tag:
<script type="text/javascript">
$(document).ready(function(){ //When the document is done loading
$("input#save").trigger('click'); //"Click" on the input button with an ID of "save"
});
</script>
Or you could do it the pure javascript way without jQuery like so:
<html>
<body onLoad="submitform()">
<form id="weeklysheet" name="weeklysheet" method="post" action="hourly-function.php">
Then place this script tag below the </form> tag:
<script type="text/javascript">
function submitform()
{
document.weeklysheet.submit();
}
</script>
Or, to shorten the method above:
<html>
<body onLoad="Javascript: document.weeklysheet.submit();">
<form id="weeklysheet" name="weeklysheet" method="post" action="hourly-function.php">
Are you looking to submit them individually or all at once? If you want to do it all at once you could give the last element an id and in jquery use $('#div').moueleave
that's the js I'm using, I've try submitting with a timeout and it's working.
at the end my issue is not there, it appear that when I'm submit the form, the 3 php variables push the value of the previous input entry.
I don't know if I'm clear with my explanation...
<script type="text/javascript">
function submitform()
{
//document.getElementById("weeklysheet");
var form = document.getElementById("weeklysheet")
form.submit();
}
function submit()
{
if (document.getElementById("weeklysheet")) {
setTimeout("submitform()", 5000); // set timout
}
return false;
}
</script>
(sorry if it's a long explanation)
I'm coming back with my solution... in case that can help anyone.
so the idea it's to have a table that display a list of employees where you can input the days worked, holidays, sick. Then do the sum of each and calculate the gross depending on a wages frequency (i.e: Weekly, 4 Weekly or Monthly).
Because the user won't complete the all form at once, I needed to make the submission automatic for all inputs at all the time of the current week.
this is the JS:
<script type="text/javascript">
function grab<?=$empnum?>(){
// Monday
var mon<?=$empnum?> = document.getElementById('mon<?=$empnum?>').value;
if ((mon<?=$empnum?> == "h" || mon<?=$empnum?> == "H")){
var Hmon<?=$empnum?> = 1;
var Wmon<?=$empnum?> = 0;
var Smon<?=$empnum?> = 0;
}
else if((mon<?=$empnum?> == "w" || mon<?=$empnum?> == "W")){
var Wmon<?=$empnum?> = 1;
var Hmon<?=$empnum?> = 0;
var Smon<?=$empnum?> = 0;
}
else if((mon<?=$empnum?> == "s" || mon<?=$empnum?> == "S")){
var Smon<?=$empnum?> = 1;
var Hmon<?=$empnum?> = 0;
var Wmon<?=$empnum?> = 0;
}
else{
var Smon<?=$empnum?> = 0;
var Hmon<?=$empnum?> = 0;
var Wmon<?=$empnum?> = 0;
var Xmon<?=$empnum?> = 0;
}
// Tuesday
var tue<?=$empnum?> = document.getElementById('tue<?=$empnum?>').value;
if ((tue<?=$empnum?> == "h" || tue<?=$empnum?> == "H")){
var Htue<?=$empnum?> = 1;
var Wtue<?=$empnum?> = 0;
var Stue<?=$empnum?> = 0;
}
else if((tue<?=$empnum?> == "w" || tue<?=$empnum?> == "W")){
var Wtue<?=$empnum?> = 1;
var Htue<?=$empnum?> = 0;
var Stue<?=$empnum?> = 0;
}
else if((tue<?=$empnum?> == "s" || tue<?=$empnum?> == "S")){
var Stue<?=$empnum?> = 1;
var Htue<?=$empnum?> = 0;
var Wtue<?=$empnum?> = 0;
}
else{
var Stue<?=$empnum?> = 0;
var Htue<?=$empnum?> = 0;
var Wtue<?=$empnum?> = 0;
}
// Wednesday
var wed<?=$empnum?> = document.getElementById('wed<?=$empnum?>').value;
if ((wed<?=$empnum?> == "h" || wed<?=$empnum?> == "H")){
var Hwed<?=$empnum?> = 1;
var Wwed<?=$empnum?> = 0;
var Swed<?=$empnum?> = 0;
}
else if((wed<?=$empnum?> == "w" || wed<?=$empnum?> == "W")){
var Wwed<?=$empnum?> = 1;
var Hwed<?=$empnum?> = 0;
var Swed<?=$empnum?> = 0;
}
else if((wed<?=$empnum?> == "s" || wed<?=$empnum?> == "S")){
var Swed<?=$empnum?> = 1;
var Hwed<?=$empnum?> = 0;
var Wwed<?=$empnum?> = 0;
}
else{
var Swed<?=$empnum?> = 0;
var Hwed<?=$empnum?> = 0;
var Wwed<?=$empnum?> = 0;
}
// Thurday
var thu<?=$empnum?> = document.getElementById('thu<?=$empnum?>').value;
if ((thu<?=$empnum?> == "h" || thu<?=$empnum?> == "H")){
var Hthu<?=$empnum?> = 1;
var Wthu<?=$empnum?> = 0;
var Sthu<?=$empnum?> = 0;
}
else if((thu<?=$empnum?> == "w" || thu<?=$empnum?> == "W")){
var Wthu<?=$empnum?> = 1;
var Hthu<?=$empnum?> = 0;
var Sthu<?=$empnum?> = 0;
}
else if((thu<?=$empnum?> == "s" || thu<?=$empnum?> == "S")){
var Sthu<?=$empnum?> = 1;
var Hthu<?=$empnum?> = 0;
var Wthu<?=$empnum?> = 0;
}
else{
var Sthu<?=$empnum?> = 0;
var Hthu<?=$empnum?> = 0;
var Wthu<?=$empnum?> = 0;
}
// Friday
var fri<?=$empnum?> = document.getElementById('fri<?=$empnum?>').value;
if ((fri<?=$empnum?> == "h" || fri<?=$empnum?> == "H")){
var Hfri<?=$empnum?> = 1;
var Wfri<?=$empnum?> = 0;
var Sfri<?=$empnum?> = 0;
}
else if((fri<?=$empnum?> == "w" || fri<?=$empnum?> == "W")){
var Wfri<?=$empnum?> = 1;
var Hfri<?=$empnum?> = 0;
var Sfri<?=$empnum?> = 0;
}
else if((fri<?=$empnum?> == "s" || fri<?=$empnum?> == "S")){
var Sfri<?=$empnum?> = 1;
var Hfri<?=$empnum?> = 0;
var Wfri<?=$empnum?> = 0;
}
else{
var Sfri<?=$empnum?> = 0;
var Hfri<?=$empnum?> = 0;
var Wfri<?=$empnum?> = 0;
}
// Saturday
var sat<?=$empnum?> = document.getElementById('sat<?=$empnum?>').value;
if ((sat<?=$empnum?> == "h" || sat<?=$empnum?> == "H")){
var Hsat<?=$empnum?> = 1;
var Wsat<?=$empnum?> = 0;
var Ssat<?=$empnum?> = 0;
}
else if((sat<?=$empnum?> == "w" || sat<?=$empnum?> == "W")){
var Wsat<?=$empnum?> = 1;
var Hsat<?=$empnum?> = 0;
var Ssat<?=$empnum?> = 0;
}
else if((sat<?=$empnum?> == "s" || sat<?=$empnum?> == "S")){
var Ssat<?=$empnum?> = 1;
var Hsat<?=$empnum?> = 0;
var Wsat<?=$empnum?> = 0;
}
else{
var Ssat<?=$empnum?> = 0;
var Hsat<?=$empnum?> = 0;
var Wsat<?=$empnum?> = 0;
}
// Sunday
var sun<?=$empnum?> = document.getElementById('sun<?=$empnum?>').value;
if ((sun<?=$empnum?> == "h" || sun<?=$empnum?> == "H")){
var Hsun<?=$empnum?> = 1;
var Wsun<?=$empnum?> = 0;
var Ssun<?=$empnum?> = 0;
}
else if((sun<?=$empnum?> == "w" || sun<?=$empnum?> == "W")){
var Wsun<?=$empnum?> = 1;
var Hsun<?=$empnum?> = 0;
var Ssun<?=$empnum?> = 0;
}
else if((sun<?=$empnum?> == "s" || sun<?=$empnum?> == "S")){
var Ssun<?=$empnum?> = 1;
var Hsun<?=$empnum?> = 0;
var Wsun<?=$empnum?> = 0;
}
else{
var Ssun<?=$empnum?> = 0;
var Hsun<?=$empnum?> = 0;
var Wsun<?=$empnum?> = 0;
}
var W<?=$empnum?> = Wmon<?=$empnum?>*1+Wtue<?=$empnum?>*1+Wwed<?=$empnum?>*1+Wthu<?=$empnum?>*1+Wfri<?=$empnum?>*1+Wsat<?=$empnum?>*1+Wsun<?=$empnum?>*1;
var H<?=$empnum?> = Hmon<?=$empnum?>*1+Htue<?=$empnum?>*1+Hwed<?=$empnum?>*1+Hthu<?=$empnum?>*1+Hfri<?=$empnum?>*1+Hsat<?=$empnum?>*1+Hsun<?=$empnum?>*1;
var S<?=$empnum?> = Smon<?=$empnum?>*1+Stue<?=$empnum?>*1+Swed<?=$empnum?>*1+Sthu<?=$empnum?>*1+Sfri<?=$empnum?>*1+Ssat<?=$empnum?>*1+Ssun<?=$empnum?>*1;
var totals<?=$empnum?> = document.getElementById('totalhrs<?=$empnum?>').value = W<?=$empnum?>;
var holiday<?=$empnum?> = document.getElementById('holihrs<?=$empnum?>').value = H<?=$empnum?>;
var sick<?=$empnum?> = document.getElementById('sickdays<?=$empnum?>').value = S<?=$empnum?>;
var wage<?=$empnum?> = document.getElementById('wagefreq<?=$empnum?>').value;
if(wage<?=$empnum?> == 'Weekly'){
document.getElementById('gross<?=$empnum?>').value = <?=$empsalarywage?>;
}
else if(wage<?=$empnum?> == '4 Weekly'){
document.getElementById('gross<?=$empnum?>').value = (<?=$empsalarywage?> / 4);
}
else if(wage<?=$empnum?> == 'Monthly'){
document.getElementById('gross<?=$empnum?>').value = ((<?=$empsalarywage?> * 12) / 52);
}
document.getElementById('weeklysheet<?=$empnum?>').submit('save<?=$empnum?>');
}
</script>
this the form:
<form id="weeklysheet<?=$empnum?>" name="weeklysheet" method="post" action="salary-function.php" >
<tbody>
<tr style="line-height:0px;">
<td class="large">
<p class="employee_name"><?=$fullname?></p>
</td>
<td class="small">
<input name="mon" id="mon<?=$empnum?>" type="text" class="days_input" maxlength="1" value="<?php echo $mon?>"onchange="grab<?=$empnum?>()"/>
</td>
<td class="small">
<input name="tue" id="tue<?=$empnum?>" type="text" class="days_input" maxlength="1" value="<?php echo $tue?>"onchange="grab<?=$empnum?>()"/>
</td>
<td class="small">
<input name="wed" id="wed<?=$empnum?>" type="text" class="days_input" maxlength="1" value="<?php echo $wed?>"onchange="grab<?=$empnum?>()"/>
</td>
<td class="small">
<input name="thu" id="thu<?=$empnum?>" type="text" class="days_input" maxlength="1" value="<?php echo $thu?>"onchange="grab<?=$empnum?>()"/>
</td>
<td class="small">
<input name="fri" id="fri<?=$empnum?>" type="text" class="days_input" maxlength="1" value="<?php echo $fri?>"onchange="grab<?=$empnum?>()"/>
</td>
<td class="small">
<input name="sat" id="sat<?=$empnum?>" type="text" class="days_input" maxlength="1" value="<?php echo $sat?>"onchange="grab<?=$empnum?>()"/>
</td>
<td class="small">
<input name="sun" id="sun<?=$empnum?>" type="text" class="days_input" maxlength="1" value="<?php echo $sun?>"onchange="grab<?=$empnum?>()"/>
</td >
<td class="small">
<input name="totalhrs" id="totalhrs<?=$empnum?>" type="text" class="cumul_week_input" readonly value="<?php echo $totalhrs?>"/>
</td>
<td class="small">
<input name="holihrs" id="holihrs<?=$empnum?>" type="text" class="cumul_week_input" readonly value="<?php echo $holihrs?>"/>
</td>
<td class="small">
<input name="sickdays" id="sickdays<?=$empnum?>" type="text" class="cumul_week_input" readonly value="<?php echo $sickdays?>"/>
</td>
<td class="small">
<input name="gross" id="gross<?=$empnum?>" type="text" class="cumul_week_input" readonly value="<?php echo $gross?>"/>
</td>
<td>
<input type="submit"name="save" id="save<?=$empnum?>" value="Confirm" style="display:none"/>
<input type="hidden"name="enddate" value="<?php echo $data['currentWeekStart']; ?>"/>
<input type="hidden"name="wagefreq"id="wagefreq<?=$empnum?>" value="<?=$wagefreq?>"/>
</td>
</tr>
</tbody>
</form>
that's something that worked like I want, but if anyone see anything that could be done in a better way, I would really apreciate any corrections...
thank's

Check/Uncheck all checkboxes with Javascript in a PHP array with keys

I have the following code:
Check All |
Uncheck All |
Invert Selection<br />
<table>
<tr>
<td><input type="checkbox" name="names[8]" value="yes" />Paul</td>
<td><input type="checkbox" name="names[11]" value="yes" />Bob</td>
<td><input type="checkbox" name="names[44]" value="yes" />Tom</td>
</tr>
</table>
And the following script:
function setCheckboxes3(act)
{
elts = document.getElementsByName("names[]");
var elts_cnt = (typeof(elts.length) != 'undefined') ? elts.length : 0;
if (elts_cnt)
{
for (var i = 0; i < elts_cnt; i++)
{
elts[i].checked = (act == 1 || act == 0) ? act : (elts[i].checked ? 0 : 1);
}
}
}
The script is working with other arrays without keys, but I can't get it to work with this array which has keys.
Thanks in advance
You can use getElementsByClassName:
<script type="text/javascript" language="javascript">
function setCheckboxes3(act) {
var e = document.getElementsByClassName('names');
var elts_cnt = (typeof(e.length) != 'undefined') ? e.length : 0;
if (!elts_cnt) {
return;
}
for (var i = 0; i < elts_cnt; i++) {
e[i].checked = (act == 1 || act == 0) ? act : (e[i].checked ? 0 : 1);
}
}
</script>
Check All |
Uncheck All |
Invert Selection<br />
<input type="checkbox" name="names[8]" class="names" value="yes" />Paul
<input type="checkbox" name="names[11]" class="names" value="yes" />Bob
<input type="checkbox" name="names[44]" class="names" value="yes" />Tom
OR you can use: getElementsByTagName
<script type="text/javascript" language="javascript">
function setCheckboxes3(act) {
var e = document.getElementsByTagName('input');
var elts_cnt = (typeof(e.length) != 'undefined') ? e.length : 0;
if (!elts_cnt) {
return;
}
for (var i = 0; i < elts_cnt; i++) {
if((e[i].type) == 'checkbox') {
e[i].checked = (act == 1 || act == 0) ? act : (e[i].checked ? 0 : 1);
}
}
}
</script>
Check All |
Uncheck All |
Invert Selection<br />
<input type="checkbox" name="names[8]" value="yes" />Paul
<input type="checkbox" name="names[11]" value="yes" />Bob
<input type="checkbox" name="names[44]" value="yes" />Tom
Did you get a chance to try jQuery?

Categories