Can anyone help me to get the addition of Basic Salary and Allowance I and insert it in Total Day Rate without clicking a button. here's my php code.
Here's a Screen Shot of my UI.
Code :
<?php
$connection = mysql_connect("localhost", "root", "");
$db = mysql_select_db("laboursalary", $connection);
if(isset($_POST['submit'])){
$ID = $_POST['ID'];
$Name = $_POST['Name'];
$Location = $_POST['Location'];
$Category = $_POST['Category'];
$LabourSupplier = $_POST['LabourSupplier'];
$Home = $_POST['Home'];
$Mobile = $_POST['Mobile'];
$BasicSalary = $_POST['BasicSalary'];
$Allowance1 = $_POST['Allowance1'];
$Allowance2 = $_POST['Allowance2'];
$DayRate = $_POST['$DayRate'];
$OTrate = $_POST['OTrate'];
if($ID !=''||$Name !=''){
$query = mysql_query("insert into attendance(ID, Name, Location, Category,LabourSupplier,Home,Mobile,BasicSalary,Allowance1,Allowance2,DayRate,OTrate) values ('$ID','$Name','$Location','$Category','$LabourSupplier','$Home','$Mobile','$BasicSalary','$Allowance1','$Allowance2','$DayRate','$OTrate')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
}
mysql_close($connection);
?>
After I enter the values for Basic Salary and Allowance 1, I want to get the addition of those two in Day Rate automatically.
this is my HTML code.
<form id="details" action="" method="POST">
<fieldset> ID:
<input class="input" type="text" name="ID" value="" />
</fieldset>
<fieldset> Name:
<input class="input" type="text" name="Name" value="" />
</fieldset>
<fieldset> Location:
<input class="input" type="text" name="Location" value="" />
</fieldset>
<fieldset> Category:
<input class="input" type="text" name="Category" value="" />
</fieldset>
<fieldset> Labour Supplier:
<input class="input" type="text" name="LabourSupplier" value="" />
</fieldset>
<fieldset> Telephone:
<input class="input" type="text" name="Home" value="" />
</fieldset>
<fieldset>Mobile:
<input class="input" type="text" name="Mobile" value="" />
</fieldset>
<fieldset> Basic Salary:
<input class="input" type="number" name="BasicSalary" value="" />
</fieldset>
<fieldset> Allowance I:
<input class="input" type="number" name="Allowance1" value="" />
</fieldset>
<fieldset>Allowance II:
<input class="input" type="number" name="Allowance2" value="" />
</fieldset>
<fieldset>Total Day Rate:
<input class="input" type="number" name="DayRate" value="" />
</fieldset>
<fieldset>OT Rate:
<input class="input" type="number" name="OTrate" value="" />
</fieldset>
<fieldset>
<button name="submit" type="submit" id="submit">Insert</button>
<button onclick="goBack()" name="Back" type="back" id="details-back">Back</button>
</fieldset>
</form>
As I understood your question, your HTML code should be like,
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<form id="details" action="" method="POST">
<fieldset> ID:
<input class="input" type="text" name="ID" value="" />
</fieldset>
<fieldset> Name:
<input class="input" type="text" name="Name" value="" />
</fieldset>
<fieldset> Location:
<input class="input" type="text" name="Location" value="" />
</fieldset>
<fieldset> Category:
<input class="input" type="text" name="Category" value="" />
</fieldset>
<fieldset> Labour Supplier:
<input class="input" type="text" name="LabourSupplier" value="" />
</fieldset>
<fieldset> Telephone:
<input class="input" type="text" name="Home" value="" />
</fieldset>
<fieldset>Mobile:
<input class="input" type="text" name="Mobile" value="" />
</fieldset>
<fieldset> Basic Salary:
<input class="input" type="number" name="BasicSalary" value="0" id="bassal" />
</fieldset>
<fieldset> Allowance I:
<input class="input" type="number" name="Allowance1" value="0" id="all1" />
</fieldset>
<fieldset>Allowance II:
<input class="input" type="number" name="Allowance2" value="0" id="all2" />
</fieldset>
<fieldset>Total Day Rate:
<input class="input" type="number" name="DayRate" value="" id="DayRate" />
</fieldset>
<fieldset>OT Rate:
<input class="input" type="number" name="OTrate" value="" />
</fieldset>
<fieldset>
<button name="submit" type="submit" id="submit">Insert</button>
<button onclick="goBack()" name="Back" type="back" id="details-back">Back</button>
</fieldset>
</form>
<script >
$(document).ready(function (){
$('#bassal').on('input', function() {
$('#DayRate').val(parseInt($('#bassal').val()) + parseInt($("#all1").val()) + parseInt($("#all2").val()));
});
$('#all1').on('input', function() {
$('#DayRate').val(parseInt($('#bassal').val()) + parseInt($("#all1").val()) + parseInt($("#all2").val()));
});
$('#all2').on('input', function() {
$('#DayRate').val(parseInt($('#bassal').val()) + parseInt($("#all1").val()) + parseInt($("#all2").val()));
});
});
</script>
I've added jQuery, that does your work, and you don't need to do addition on PHP side.
This is how it would be done using a submit for post as these are all _POST values... Though to accomplish this without pressing a button would be JS/Angular/J Query/AJAX...
....
$BasicSalary = $_POST['BasicSalary'];
$Allowance1 = $_POST['Allowance1'];
$Allowance2 = $_POST['Allowance2'];
$DayRate = $_POST['$DayRate'];
$OTrate = $_POST['OTrate'];
//Set a new variable with the addition of the two `Basic Salary` and `Allowance 1`
//for the insertion into your column `dayRate`
$adustedDayRate = $BasicSalary + $Allowance1;
if($ID !=''||$Name !=''){
if($query != false){
$query = mysql_query("INSERT INTO `attendance` (ID, Name, Location, Category,LabourSupplier,Home,Mobile,BasicSalary,Allowance1,Allowance2,DayRate,OTrate) values ('$ID','$Name','$Location','$Category','$LabourSupplier','$Home','$Mobile','$BasicSalary','$Allowance1','$Allowance2','$adustedDayRate','$OTrate')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}esle{ $_SESSION['err'] = "ERROR: ".--- MySQL error handle here --- }
}
else{
echo "Some Fields are Blank....!!</p>";
}
}
mysql_close($connection);
Using Angular, you could place a placeholder element in your form input for `DayRate, then add the call back for the ng-model element for the two inputs you wish to add. Something like this here:
<div ng-app="">
<p>BasicSalary : <input type="number" ng-model="BasicSalary" placeholder="Basic Salary"></p>
<p>AllowanceI : <input type="number" ng-model="AllowanceI" placeholder="Allowance I"></p>
<p>DayRate : <input type="number" ng-model="DayRate" placeholder="{{BasicSalary -- AllowanceI}}"></p>
Your code would look like:
<fieldset> Basic Salary:
<input class="input" type="number" ng-model="BasicSalary" name="BasicSalary" value="" />
</fieldset>
<fieldset> Allowance I:
<input class="input" type="number" ng-model="Allowance1" name="Allowance1" value="" />
</fieldset>
<fieldset>Allowance II:
<input class="input" type="number" name="Allowance2" value="" />
</fieldset>
<fieldset>Total Day Rate:
<input class="input" type="number" name="DayRate" placeholder="{{ BasicSalary -- Allowance1 }}" value="" />
</fieldset>
Here is a working fiddle of the angular method, simply add the angular library to your server using composer or hosted links, no additional JS is required with the Angular library elements. You can change the value of DayRate as the two other combining inputs are being input.
https://jsfiddle.net/qkpfb90o/1/
Hope this helps!
You can also try with this.
<form id="details" action="" method="POST">
<fieldset> ID:
<input class="input" type="text" name="ID" value="" />
</fieldset>
<fieldset> Name:
<input class="input" type="text" name="Name" value="" />
</fieldset>
<fieldset> Location:
<input class="input" type="text" name="Location" value="" />
</fieldset>
<fieldset> Category:
<input class="input" type="text" name="Category" value="" />
</fieldset>
<fieldset> Labour Supplier:
<input class="input" type="text" name="LabourSupplier" value="" />
</fieldset>
<fieldset> Telephone:
<input class="input" type="text" name="Home" value="" />
</fieldset>
<fieldset>Mobile:
<input class="input" type="text" name="Mobile" value="" />
</fieldset>
<fieldset> Basic Salary:
<input class="input" type="number" name="BasicSalary" value="" id="BasicSalary" onkeyup="doSum()"/>
</fieldset>
<fieldset> Allowance I:
<input class="input" type="number" name="Allowance1" value="" id="Allowance1" onkeyup="doSum()"/>
</fieldset>
<fieldset>Allowance II:
<input class="input" type="number" name="Allowance2" />
</fieldset>
<fieldset>Total Day Rate:
<input class="input" type="number" name="DayRate" value="" id="DayRate" />
</fieldset>
<fieldset>OT Rate:
<input class="input" type="number" name="OTrate" value="" />
</fieldset>
<fieldset>
<button name="submit" type="submit" id="submit">Insert</button>
<button onclick="goBack()" name="Back" type="back" id="details-back">Back</button>
</fieldset>
<script>
function doSum() {
var Allowance1 = isNaN(document.getElementById('Allowance1').value) ? document.getElementById('Allowance1').value : 0;
var BasicSalary = isNaN(document.getElementById('BasicSalary').value) ? document.getElementById('BasicSalary').value : 0;
var tot = parseInt(Allowance1) + parseInt(BasicSalary);
document.getElementById('DayRate').value = tot;
}
</script>
In php, if you name your form fields using numerical indexes, they work as arrays in the $_POST object.
<form method="post" action="post.php">
<input type="text" name="question[0][name]" />
<input type="text" name="question[0][email]"/>
<input type="text" name="question[0][password]" />
<hr>
<input type="text" name="question[1][name]" />
<input type="text" name="question[1][email]"/>
<input type="text" name="question[1][password]" />
<hr>
<input type="submit" value="Add" />
<hr>
<p><?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo json_encode($_POST, JSON_NUMERIC_CHECK);
}
?></p>
</form>
outputs
{"question":[{"name":"a","email":"aa","password":"aaa"},{"name":"b","email":"bb","password":"bbb"}]}
If the ordering of the fields is not sequential starting at zero and incrementing by only one each time the name is repeated, then they are all interpreted as keys instead. So
<form method="post" action="post.php">
<input type="text" name="question[1][name]" />
<input type="text" name="question[1][email]"/>
<input type="text" name="question[1][password]" />
<hr>
<input type="text" name="question[0][name]" />
<input type="text" name="question[0][email]"/>
<input type="text" name="question[0][password]" />
<hr>
<input type="submit" value="Add" />
<hr>
<p><?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo json_encode($_POST, JSON_NUMERIC_CHECK);
}
?></p>
</form>
outputs
{"question":{"1":{"name":"a","email":"aa","password":"aaa"},"0":{"name":"b","email":"bb","password":"bbb"}}}
Is there a way to get $_POST to ignore the order of arrays of post keys so they are interpreted as an array?
Please check if helpful or not :
<form method="post" action="#">
<input type="text" name="question[1][name]" />
<input type="text" name="question[1][email]"/>
<input type="text" name="question[1][password]" />
<hr>
<input type="text" name="question[0][name]" />
<input type="text" name="question[0][email]"/>
<input type="text" name="question[0][password]" />
<hr>
<input type="submit" value="Add" />
<hr>
<p>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
ksort($_POST['question']);
print_r($_POST['question']);
}
?>
</p>
</form>
Somehow when I submit this form the values are stored last name then first name. How can I change the order?
<form action="..posted" method="post" id="register-form">
<div class=register-form-div>
First Name: <input class=register-form-input type="text" name="register[First name]" />
</div>
<div class=register-form-div>
Last Name: <input class=register-form-input type="text" name="register[Last Name]" />
</div>
<input type="submit" id="register-submit" name="register-submit" value="Create Account" />
</form>
try that
<form action="..posted" method="post" id="register-form">
<div class=register-form-div>
First Name: <input class=register-form-input type="text" name="register[1][First name]" />
</div>
<div class=register-form-div>
Last Name: <input class=register-form-input type="text" name="register[0][Last Name]" />
</div>
<input type="submit" id="register-submit" name="register-submit" value="Create Account" />
</form>
I am currently developing an app and i am getting difficulties on IE8.
I have the following code:
<form class="fillClaimForm" name="fillClaimForm" method="POST" enctype="multipart/form-data" action="<?php print BASE_URL; ?>?action=insertBlogger" >
<label for="fblogName">Blog's name: </label>
<input type="text" name="fblogName" id="fblogName" class="cfInput" placeholder ="Complex" required />
<label for="fblogUrl">URL: </label>
<input type="text" name="fblogUrl" id="fblogUrl" class="cfInput" placeholder ="www.complex.com" required />
<label>Blog's logo: </label>
<div class="upload-file-container"><span>UPLOAD</span>
<input type="file" name="fblogLogo" id="fblogLogo"/>
<p class="ErrLoad error" style="display:none;">Please load your logo</p>
</div>
<label for="fEmail">Email adresse: </label>
<input type="email" name="fEmail" id="fEmail" class="cfInput" required />
<label for="frNum">Phone number: </label>
<input type="tel" name="frNum" id="frNum" class="cfInput" required />
<input type="hidden" name="idVid" id ="idVid" value=""/>
<input type="hidden" name="idRubrique" id ="idRubrique" value=""/>
<button id="sendClaim" class="sendClaim">SEND</button>
Consequently, I am doing a form submission using jquery:
......submit(function(){
validate=false;
formInfo = validateForm(validate,$thisLi);
if(formInfo.validate){
**fillClaimForm.submit();**
}
return false;
});
It is working on all browsers except IE8
Can someone kindly help me.
Thanks
Close your form.
Also give the same name as the id for your form and use
$("#formId").submit();
Code
<form class="fillClaimForm" name="fillClaimForm" method="POST" enctype="multipart/form-data" action="<?php print BASE_URL; ?>?action=insertBlogger" >
<label for="fblogName">Blog's name: </label>
<input type="text" name="fblogName" id="fblogName" class="cfInput" placeholder ="Complex" required />
<label for="fblogUrl">URL: </label>
<input type="text" name="fblogUrl" id="fblogUrl" class="cfInput" placeholder ="www.complex.com" required />
<label>Blog's logo: </label>
<div class="upload-file-container"><span>UPLOAD</span>
<input type="file" name="fblogLogo" id="fblogLogo"/>
<p class="ErrLoad error" style="display:none;">Please load your logo</p>
</div>
<label for="fEmail">Email adresse: </label>
<input type="email" name="fEmail" id="fEmail" class="cfInput" required />
<label for="frNum">Phone number: </label>
<input type="tel" name="frNum" id="frNum" class="cfInput" required />
<input type="hidden" name="idVid" id ="idVid" value=""/>
<input type="hidden" name="idRubrique" id ="idRubrique" value=""/>
<button id="sendClaim" class="sendClaim">SEND</button>
</form>
$("#sendClaim").click(function(){
validate=false;
formInfo = validateForm(validate,$thisLi);
if(formInfo.validate){
$("#fillClaimForm").submit();
}
return false;
});
Try this:
document.forms.fillClaimForm.submit();
Or if the ...... in your code represents creating of a submit handler on that same form you can probably just say this.submit().
hello guys? can you help me with this? i want to get the id of the first input and the text value of the second input in a specific p tag
<p class="receiveitem">
<label for="mat_id">Material</label>
<input type="text" id="4"value="GI Coulping" disabled/>
<label for="rec_qty">Quantity</label>
<input type="text" class="rec_qty" />
<input type="button" class="receive" value=Receive />
</p>
<p class="receiveitem">
<label for="mat_id">Material</label>
<input type="text" id="5"value="Vulca Seal" disabled/>
<label for="rec_qty">Quantity</label>
<input type="text" class="rec_qty" />
<input type="button" class="receive" value=Receive />
</p>
<p class="receiveitem">
<label for="mat_id">Material</label>
<input type="text" id="6"value="teflon tape" disabled/>
<label for="rec_qty">Quantity</label>
<input type="text" class="rec_qty" />
<input type="button" class="receive" value=Receive />
</p>
so when i click the receive button that's in the p tag.
it will output someting like
ID: 4 and Value: 10
already have a js.but i can't get it done.
$(document).ready(function(){
$('input#receive').click(function(){
var mat = $('input[type=text]:first').attr('id');
var qty = $('input[type=text]:last').val;
alert(mat + qty);
});
});
and i dont know where to put the this in my variables mat and qty.please help?
Here is a modified approach using the more semantically correct data attributes (which are broadly supported) (building on #Chase's answer) (and a demo)
<p class="receiveitem">
<label for="mat_id">Material</label>
<input name="mat_id" type="text" data-id="4" value="GI Coulping" disabled="disabled"/>
<label for="rec_qty">Quantity</label>
<input type="text" name="rec_qty" />
<input name="rec_qty" type="button" class="receive" value="Receive" />
</p>
<p class="receiveitem">
<label for="mat_id">Material</label>
<input name="mat_id" type="text" data-id="5" value="Vulca Seal" disabled="disabled"/>
<label for="rec_qty">Quantity</label>
<input type="text" name="rec_qty" />
<input type="button" class="receive" value="Receive" />
</p>
<p class="receiveitem">
<label for="mat_id">Material</label>
<input name="mat_id" type="text" data-id="6" value="teflon tape" disabled="disabled"/>
<label for="rec_qty">Quantity</label>
<input type="text" name="rec_qty" />
<input type="button" class="receive" value="Receive" />
</p>
With this JavaScript
$(document).on("click", "input.receive", function () {
var mat = $(this).siblings("[name='mat_id']").data("id");
var qty = $(this).siblings("[name='rec_qty']").val();
alert("mat id: " + mat + " qty val: " + qty);
});
This approach won't abuse the id attribute which is designed for node identification and not data storage.
$(document).on('click', 'input#receive', function(e){
e.preventDefault();
var qty = $(this).prev().prev('input');
alert(qty);
});
You really need to change your markup to use data or unique IDs though.