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>
Related
I have two forms, the first gets submitted then the page reloads and shows the second. The problem I have is passing the email address from the first form to the second one. What's the best way to do this without interfering with my actions or changing it from POST to GET?
<?php if(!isset($_POST['crowd_email'])){ ?>
<form id="form" method="POST" action="<?php require( COMMON_PATH . '/components/crowdfunding.php' ); ?>">
<input type="text" name="crowd_email" class="crowd_email" value="" placeholder="Email address">
<input type="submit" class="register-btn" value="Register Interest" name="submit">
</form>
<?php } else { ?>
<form id="form" method="POST" action="<?php require( COMMON_PATH . '/components/crowdfunding-extra.php' ); ?>">
<input type="text" name="first_name" class="crowd_email extra-info" value="" placeholder="First name">
<input type="text" name="tel_no" class="crowd_email extra-info" value="" placeholder="Telephone number">
<input type="text" name="amount" class="crowd_email extra-info" value="" placeholder="Amount to invest">
<input type="submit" class="register-btn" value="Register Interest" name="submitextra">
</form>
<?php } ?>
1) Add a hidden field to the second form.
2) Add the value of the email address (submitted from the first form) to the hidden field when rendering the second form.
e.g.
<?php if(!isset($_POST['crowd_email'])){ ?>
<form id="form" method="POST" action="<?php require( COMMON_PATH . '/components/crowdfunding.php' ); ?>">
<input type="text" name="crowd_email" class="crowd_email" value="" placeholder="Email address">
<input type="submit" class="register-btn" value="Register Interest" name="submit">
</form>
<?php } else { ?>
<form id="form" method="POST" action="<?php require( COMMON_PATH . '/components/crowdfunding-extra.php' ); ?>">
<input type="text" name="first_name" class="crowd_email extra-info" value="" placeholder="First name">
<input type="text" name="tel_no" class="crowd_email extra-info" value="" placeholder="Telephone number">
<input type="text" name="amount" class="crowd_email extra-info" value="" placeholder="Amount to invest">
<input type="hidden" name="email" value="<?php echo $_POST["crowd_email"]; ?>" />
<input type="submit" class="register-btn" value="Register Interest" name="submitextra">
</form>
<?php } ?>
That way, when you submit the second form, the email field will be submitted along with the rest of the new data.
You can just add a hidden input in the second form containing the email value and give it the value from the first submission
<?php if(!isset($_POST['crowd_email'])){ ?>
<form id="form" method="POST" action="<?php require( COMMON_PATH . '/components/crowdfunding.php' ); ?>">
<input type="text" name="crowd_email" class="crowd_email" value="" placeholder="Email address">
<input type="submit" class="register-btn" value="Register Interest" name="submit">
</form>
<?php } else { ?>
<form id="form" method="POST" action="<?php require( COMMON_PATH . '/components/crowdfunding-extra.php' ); ?>">
<!-- New line to hold the email invisbly -->
<input type="hidden" name="crowd-email" value="<?php echo $_POST['crowd_email']; ?>"/>
<input type="text" name="first_name" class="crowd_email extra-info" value="" placeholder="First name">
<input type="text" name="tel_no" class="crowd_email extra-info" value="" placeholder="Telephone number">
<input type="text" name="amount" class="crowd_email extra-info" value="" placeholder="Amount to invest">
<input type="submit" class="register-btn" value="Register Interest" name="submitextra">
</form>
<?php } ?>
guys I have a problem with a PHP page.
I'm trying to make a form to update the record of a DB.
I'm displaying the form with an echo but I have some problems with quotes
<? echo'
<img src="logo.jpg">
<fieldset>
<input name="aspettogen2" type="text" value="<?php echo $aspettogen;?>">
</fieldset>
<fieldset>
<input name="desc" type="text" maxlength="255">
</fieldset>
<fieldset>
<input name="ragsoc" type="text" maxlength="100">
</fieldset>
<fieldset>
<input name="numcivico" type="text" maxlength="20">
</fieldset>
<fieldset>
<input name="validita" type="text">
</fieldset>
<fieldset>
<input name="odierna" type="data">
</fieldset>
<fieldset>
<input name="preavviso" type="text">
</fieldset>
<fieldset>
<input name="scadenza" type="text">
</fieldset>
<fieldset>
<input name="presc" type="text" maxlength="255">
</fieldset>
<fieldset>
<input name="freq" type="number">
</fieldset>
<fieldset>
<input name="datacontr" type="text">
</fieldset>
<fieldset>
<input name="proxcontr" type="text">
</fieldset>
<fieldset>
<input name="note" type="text">
</fieldset>
</form>
</div>
</body>
</html>';
in the first fieldset value="<?php echo $aspettogen;?"> it appears gray because the quote after the echo turn it into a "comment" and i cant print the value..
how can i solve it?
i've tried to do like this Value="'<?php echo $aspettogen;?'"> but i receive the error Parse error: syntax error, unexpected '?' in /var/..... on line 50
You have wrong syntax in that line. I have made the necessary changes.
Code:
<? echo'
<img src="logo.jpg">
<fieldset>
<input name="aspettogen2" placeholder="Aspetto Generale" type="text" value="<?php echo $aspettogen;?>">
</fieldset>
<fieldset>
<input name="desc" placeholder="Descrizione" type="text" maxlength="255">
</fieldset>
<fieldset>
<input name="ragsoc" placeholder="Ragione Sociale" type="text" maxlength="100">
</fieldset>
<fieldset>
<input name="numcivico" placeholder="Numero Civico" type="text" maxlength="20">
</fieldset>
<fieldset>
<input name="validita" placeholder="Validita" type="text">
</fieldset>
<fieldset>
<input name="odierna" placeholder="Data" type="data" id="today" readonly >
</fieldset>
<fieldset>
<input name="preavviso" placeholder="Preavviso" type="text">
</fieldset>
<fieldset>
<input name="scadenza" placeholder="Scadenza" type="text">
</fieldset>
<fieldset>
<input name="presc" placeholder="Prescrizioni" type="text" maxlength="255">
</fieldset>
<fieldset>
<input name="freq" placeholder="Frequenza (in giorni)" type="number">
</fieldset>
<fieldset>
<input name="note" placeholder="Note" type="text">
</fieldset>
<fieldset>
<button name="submit" type="submit" id="contact-submit">Inserisci</button>
</fieldset>
</form>
</div>
</body>
</html>';?>
I added a "contact us" form to a working website.
When I tried it, I noticed that the submit button was not working. I removed the jquery CDN link from the code and after that, the submit button started to work and emails are now sending smoothly.
Can anyone suggest a solution to keep the jquery link and the submit button working properly ?
<form action="" method="POST" id="myForm">
<fieldset>
<input type="text" name="fullname" placeholder="Full Name" /> <br />
<input type="text" name="subject" placeholder="Subject" /> <br />
<input type="text" name="phone" placeholder="Phone" /> <br />
<input type="text" name="emailid" placeholder="Email" /> <br />
<textarea rows="4" cols="20" name="comments" placeholder="Comments"></textarea> <br />
<input type="button" name=" onclick=" myFunction() " value="Submit form"> </fieldset>
</form>
(...)
<script>
function myFunction() { document.getElementById("myForm").submit(); }
</script>
My guess is that's a typo error, look at your code:
<form action="" method="POST" id="myForm"> <fieldset> <input type="text" name="fullname" placeholder="Full Name" /> <br /> <input type="text" name="subject" placeholder="Subject" /> <br /> <input type="text" name="phone" placeholder="Phone" /> <br /> <input type="text" name="emailid" placeholder="Email" /> <br /> <textarea rows="4" cols="20" name="comments" placeholder="Comments"></textarea> <br /> <input type="button" name=" onclick="myFunction()" value="Submit form"> </fieldset> </form>
There is a missing quotation mark ", just after name=":
<input type="button" name=" onclick=" myFunction() " value="Submit form">
So here is your corrected code.
<form action="" method="POST" id="myForm">
<fieldset>
<input type="text" name="fullname" placeholder="Full Name" /> <br />
<input type="text" name="subject" placeholder="Subject" /> <br />
<input type="text" name="phone" placeholder="Phone" /> <br />
<input type="text" name="emailid" placeholder="Email" /> <br />
<textarea rows="4" cols="20" name="comments" placeholder="Comments"></textarea> <br />
<input type="button" name="" onclick=" myFunction() " value="Submit form"> </fieldset>
</form>
Always double-check your code, and work on clean-formatted code, it will help you to catch such mistakes.
I have the following sets of data inside the form that are dynamically added by JavaScript so the user can add unlimited number of data rows:
<div class="row">
<input type="text" value="" name="title[]" />
<input type="text" value="" name="label[]" />
<input type="text" value="" name="value[]" />
<input type="text" value="" name="label[]" />
<input type="text" value="" name="value[]" />
<input type="text" value="" name="label[]" />
<input type="text" value="" name="value[]" />
</div>
<div class="row">
<input type="text" value="" name="title[]" />
<input type="text" value="" name="label[]" />
<input type="text" value="" name="value[]" />
<input type="text" value="" name="label[]" />
<input type="text" value="" name="value[]" />
</div>
<div class="row">
<input type="text" value="" name="title[]" />
<input type="text" value="" name="label[]" />
<input type="text" value="" name="value[]" />
</div>
My objective is then be able to print/post it in the same order so it would look like:
<h2>Title</h2>
<div class="label">Label</div>
<div class="value">Value</div>
<h2>Title</h2>
<div class="label">Label</div>
<div class="value">Value</div>
<h2>Title</h2>
<div class="label">Label</div>
<div class="value">Value</div>
My problem is that I can not figure out how to loop through sets of data and print one by one in the same order. Here is my current code but it just does not make sense what it prints and I know I am doing this the wrong way.
foreach ($_POST['title'] as $title) {
echo $title;
foreach ($_POST['label'] as $label) {
echo $label;
}
}
I would appreciate any help.
Thank you.
EDIT: Just wanted to note that rows may have different number of inputs.
If the labels and values always come in same-sized groups with each title, you can use a for loop nested in your foreach loop.
$group_size = 3;
foreach ($_POST['title'] as $i => $title) {
echo $title;
for ($j = $i*$group_size; $j < ($i+1)*$group_size; $j++) {
echo $_POST['label'][$j];
echo $_POST['value'][$j];
}
}
Try something like this:
<form method="post">
<div class="row">
title 1:
<input type="text" value="" name="title[]" />
label 1:
<input type="text" value="" name="label[]" />
value 1:
<input type="text" value="" name="value[]" />
</div>
<div class="row">
title 2:
<input type="text" value="" name="title[]" />
label 2:
<input type="text" value="" name="label[]" />
value 2:
<input type="text" value="" name="value[]" />
</div>
<div class="row">
title 3:
<input type="text" value="" name="title[]" />
label 3:
<input type="text" value="" name="label[]" />
value 3:
<input type="text" value="" name="value[]" />
</div>
<input type="submit">
</form>
<?php
foreach ($_POST['title'] as $i => $title) {
echo"
<ul>
<li>{$_POST['title'][$i]}</li>
<li>{$_POST['label'][$i]}</li>
<li>{$_POST['value'][$i]}</li>
</ul>
";
}
See demo here. Good luck!
I am trying to save the value of the radio buttons in my database table choice. I get the message Data saved successfully but no value is stored in the table.
Form:
<form id="myForm" method="post" action="">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<center<legend>Choose in which category you'd like to be included</legend></center>
<p><input type="radio" name="choice[]" value="player" id="player" class="custom" />
<label for="player">Player</label>
<input type="radio" name="choice[]" value="coach" id="coach" class="custom" />
<label for="coach">Coach</label>
<input type="radio" name="choice[]" value="supporter" id="supporter" class="custom" />
<label for="supporter">Supporter</label>
<input type="radio" name="choice[]" value="sponsor" id="sponsor" class="custom" />
<label for="sponsor">Sponsor</label>
<input type="radio" name="choice[]" value="alumni" id="alumni" class="custom" />
<label for="alumni">Alumni</label>
<input type="radio" name="choice[]" value="other" id="o" class="custom" />
<label for="o">Other</label>
</fieldset>
</div>
<div data-role="fieldcontain">
<label for="name">Please enter your name:</label>
<input type="text" name="name" id="name" class="required" value="" autocomplete="off" /><br />
<label for="email">Please enter your e-mail:</label>
<input type="text" name="email" id="email" value="" class="required" autocomplete="off" /><br />
<label for="phone">Please enter your phone number:</label>
<input type="number" name="phone" id="phone" value="" class="required" autocomplete="off" />
<br><br>
<label for="other">Other comments</label>
<textarea name="other" id="other" autocomplete="off" placeholder="Anything else you'd like to add?">
</textarea>
<p><strong id="error"></strong></p>
<br><br>
<input type="submit" id="save" name="save" value="Submit Form" />
<p id="response"></p>
</form>
</body>
</html>
PHP:
<?php
$mysqli = new mysqli('localhost', 'root', '', 'E-mail list');
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
if(isset($_POST['save']))
{
$name = $mysqli->real_escape_string($_POST['name']);
$email = $mysqli->real_escape_string($_POST['email']);
$phone = $mysqli->real_escape_string($_POST['phone']);
$other = $mysqli->real_escape_string($_POST['other']);
$choice = $mysqli->real_escape_string($_POST['choice']);
$query = "INSERT INTO Players (`name`,`email`,`phone`,`other`,`choice`) VALUES ('".$name."','".$email."','".$phone."','".$other."','".$choice."')";
if($mysqli->query($query))
{
echo 'Data Saved Successfully.';
}
else
{
echo 'Cannot save data.';
}
}
?>
var_dump($_POST) to sere what data flooding in.
One thing more to check if its save or submit ? in $_POST['save']
EDIT
After getting your full form - the error lies in your center tag
Change <center<legend> TO <center><legend>
The error - ↑ in this tag