add value from new input field then sum with existing database value - php

i want to add stock, then it will sum with the database stock
the field for the total sum(jumlahtotal) didn't work
anyway, i already put js at the header template
<div class="content-wrapper">
<div class="panel-body">
<form action="" method="POST">
<?php echo validation_errors(); ?>
<input type="hidden" name="id" value="<?= $buku['id'];?>">
<input type="hidden" name="judul" value="<?= $buku['judul'];?>">
<input type="hidden" name="pengarang" value="<?= $buku['pengarang'];?>">
<input type="hidden" name="penerbit" value="<?= $buku['penerbit'];?>">
<input type="hidden" name="tahun_terbit" value="<?= $buku['tahun_terbit'];?>">
<input type="hidden" name="isbn" value="<?= $buku['isbn'];?>">
<input type="hidden" name="rak" value="<?= $buku['rak'];?>">
<input type="hidden" name="tgl_input" value="<?= $buku['tgl_input'];?>">
<div class="form-group">
<label for="jumlah">Jumlah Masuk</label>
<input type="number" class="form-control" id="masuk" name="jumlah" value="0">
<?= form_error('masuk','<small class="text-danger">','</small>') ?>
</div>
<div class="form-group">
<label>Jumlah Total</label>
<input type="number" class="form-control" id="jumlah" name="jumlah" value="<?= $buku['jumlah_buku']?>" readonly>
<?= form_error('jumlah','<small class="text-danger">','</small>') ?>
</div>
<div class="form-group">
<input type="number" id="jumlahtotal">
</div>
<button type="submit" name="ubah" class="btn btn-primary">Update Data</button>
</form>
</div>
</div>
<script>
$(document).ready(function() {
$('input').change(function () {
var total = 0;
$('input[name=jumlah]').each(function(){
total = total+parseInt($(this).val());
})
$('input[id=jumlahtotal]').html(total);
})
})
</script>
i already checked the js is running

You are trying to set the innerHTML of an input tag in your code
$('input[id=jumlahtotal]').html(total);
In HTML, input tag is an unpaired tag. It means you don't put anything inside <input> and </input>. It's just <input>. What you are trying is to put <input> someValue </input> but via jquery.
I think you want to do this instead
$('input[id=jumlahtotal]').val(total);
This just sets the value attribute of the input element. And it's the correct way to set the value to an input element.

Related

How to add or remove values using jquery.?

<div class="container">
<div class="form-group">
<form>
<button>click to add/remove<input type="text" name="addfirst" value="10" readonly></button><br>
<button>click to add/remove<input type="text" name="addsecond" value="10" readonly></button><br>
<button>click to add/remove<input type="text" name="addthird" value="10" readonly></button><br>
<button>click to add/remove<input type="text" name="addfourth" value="10" readonly></button><br>
<label>total values</label>
<input type="text" name="total_ammount" value="50" readonly>
</form>
</div>
</div>
if i click button to add first input value which is 10 so it will addition with my total input field values if again the same button so substract the values.
remaining field should do the same work also.
how it is possible by using jquery please explain it.
Please Try the below code.,
function addValue(dhis){
var val=parseFloat(dhis.children().val()) // get the text box value
var total_ammount=parseFloat($('#total_ammount').val())+val
$('#total_ammount').val(total_ammount);
dhis.attr('onclick','subValue($(this))') // change the onclick function
}
function subValue(dhis){
var val=parseFloat(dhis.children().val()) // get the text box value
var total_ammount=parseFloat($('#total_ammount').val())-val
$('#total_ammount').val(total_ammount);
dhis.attr('onclick','addValue($(this))') // change the onclick function
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="form-group">
<form>
<button type="button" onclick="addValue($(this))">click to add/remove<input type="text" name="addfirst" value="10" readonly></button><br>
<button type="button" onclick="addValue($(this))">click to add/remove<input type="text" name="addsecond" value="10" readonly></button><br>
<button type="button" onclick="addValue($(this))">click to add/remove<input type="text" name="addthird" value="10" readonly></button><br>
<button type="button" onclick="addValue($(this))">click to add/remove<input type="text" name="addfourth" value="10" readonly></button><br>
<label>total values</label>
<input type="text" name="total_ammount" id="total_ammount" value="50" readonly>
</form>
</div>
</div>

unable to get value of form with $_post

i want to get value of input of my form via $_post in my insert page but my problem is that i get error massage:undefined index:track_code and i cant get value of it.the names is same what is problem?
this is my problem why stackoverflow want more detail. this is all detail
<form action="insert.php" method="post">
<input name="track_code" type="text" class="tiny-size" value="<?php echo $track_code_rnd ; ?>" style="width: auto;height: auto" disabled />
</form>
<form action="insert.php" method="post" class="form-horizontal form-checkout">
<div class="control-group">
<label class="control-label" for="firstName">نام<span class="red-clr bold">*</span></label>
<div class="controls">
<input name="first_name" type="text" id="firstName" class="span4" >
</div>
</div>
<div class="control-group">
<label class="control-label" for="lastName">نام خانوادگی<span class="red-clr bold">*</span></label>
<div class="controls">
<input name="last_name" type="text" id="lastName" class="span4">
</div>
</div>
<input name="submit" type="submit" class="btn btn-success" value="خرید" style="width: 66px"/>
</form>
<form action="insert.php" method="post">
<input name="track_code" type="text" class="tiny-size" value="<?php echo $track_code_rnd ; ?>" style="width: auto;height: auto" disabled />
</form>
<?php
$track_code = $_POST['track_code'];
?>
Set this one code in your form
<form action="insert.php" method="post">
<input name="track_code" type="text" class="tiny-size" value="<?php echo ($track_code_rnd)?$track_code_rnd:'';?>" style="width: auto;height: auto" disabled />
</form>
Remove disable tag from your input code. Disable controls wont be posted on server. for that you can make it readonly or make it hide
Try Putting the PHP code on the form. as given below.
<?php
if (isset($_POST['submit'])) {
$track_code = $_POST['track_code'];
}
?>
<form action="your_page.php" method="post">
<input name="track_code" type="text" class="tiny-size" value="<?php echo $track_code_rnd; ?>" style="width: auto;height: auto" readonly />
<input name="submit" type="submit" />
</form>
It will execute the whole file as PHP. The first time you open it, $_POST['submit'] won't be set because the form has not been sent. Once you click on the submit button, it will print the information.
Disabled controls never post on server. So make it readonly or hidden.

Data from dynamically created inputs is not passing to controller

I have following form and I want to send the data to controller. The problem is that data from dynamically created inputs is not being passed to the controller. This is the output of dd($request->all());
`array:4 [▼
"_token" => "gzpNTHPfZl8GCtNbCPGJrc4S4zTUmhhMUve4gyet"
"course" => "ICTP"
"section" => "BB3"
"lecture" => "functions and pointers"
]
`
welcome.blade.php
`
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<META http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>
<div>
#if(count($errors)>0)
<div class="alert alert-danger">
#foreach($errors->all() as $error)
<p>{{$error}}</p>
#endforeach
</div>
#endif
</div>
<div>
<form action="{{route('course.save')}}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<label>Select a category</label>
<select>
<option>Category 1</option>
<option>Category 2</option>
<option>Category 3</option>
<option>Category 4</option>
</select>
<br><br>
<label>Select a subcategory</label>
<select>
<option>SubCategory 1</option>
<option>SubCategory 2</option>
<option>SubCategory 3</option>
<option>SubCategory 4</option>
</select>
<br><br>
<label for="course">Course Name:</label>
<input type="text" name="course" id="course"/>
<div id="content">
<div id="section-content-1">
<div id="secs">
<label for="section">Section 1:</label>
<input type="text" name="section" id="section"/>
</div>
<div id="lects">
<label for="lecture">Lecture 1:</label>
<input type="text" name="lecture" id="lecture"/>
</div>
<button type="button" id="add-lect" data-lect="1">Add New Lecture</button><br><br>
</div>
</div>
<button type="button" id="add-sect" data-sect="1" >Add New Section</button>
<br><br><br><br>
<button class="btn-primary" type="submit">Save</button>
</form>
</div>
</body>
</html>
<script src="{{asset('/js/code.js')}}"></script>`
Here is the jquery file code.js
//$('#add-lect').click(function
$('#content').on('click', '#add-lect', function() {
var count = parseInt($(this).parent().children('#lects').children().length / 2) + 1;
lectures = '<label>Lecture ' + count + ':</label><input type="text" name="lecture" id="lecture"/>'
$(this).parent().find('#lects').append(lectures);
});
$('#add-sect').click(function(){
sect_id = parseInt($('#add-sect').attr('data-sect')) + 1;
$('#add-sect').attr('data-sect', sect_id)
var count = ($("#secs").children().length / 2) + 1;
//section = '<div id="section-content-'+sect_id+'"> <div id="secs"> <label for="section">Section'+sect_id+':</label><input type="text" name="section" id="section-"/></div><div id="lects"><label for="lecture">Lecture 1:</label><input type="text" name="lecture" id="lecture"/></div><button type="button" id="add-lect" data-lect="1">Add New Lecture</button<br><br></div>'
section = '<div id="section-content-'+sect_id+'"><div id="secs"><label for="section">Section '+sect_id+':</label> <input type="text" name="section" id="section"/></div><div id="lects"><label for="lecture">Lecture 1:</label><input type="text" name="lecture" id="lecture"/></div><button type="button" id="add-lect" data-lect="1">Add New Lecture</button><br><br></div>'
$('#content').append(section);
});
When you have a lot of input fields with same name, like
<input type="text" name="section" id="section"/>
<input type="text" name="section" id="section"/>
<input type="text" name="section" id="section"/>
<input type="text" name="section" id="section"/>
only the last input field will be passed to server.
To pass arrays of similar data there's special notation for name attribute:
<input type="text" name="section[]"/>
<input type="text" name="section[]"/>
<input type="text" name="section[]"/>
Using this, on server side you will have $_POST['section'] as array, which you can iterate and get values.
I also removed id="section" attribute as id attribute must be unique on a page.
So you need to change names of your inputs, both in blade template and in javascript code.
Update:
In case of one-to-many relations you can extend your naming to:
<input type="text" name="section[1]"/>
<input type="text" name="lecture[1][]"/>
<input type="text" name="lecture[1][]"/>
<input type="text" name="section[2]"/>
<input type="text" name="lecture[2][]"/>
<input type="text" name="lecture[2][]"/>
<input type="text" name="lecture[2][]"/>
<input type="text" name="section[3]"/>
<input type="text" name="lecture[3][]"/>
<input type="text" name="lecture[3][]"/>
So, as you see - you can explicitly define index for names.
In above-mentioned case you can iterate over $_POST['section'] and get $_POST['lecture'] with same index as section.

How to retrieve the dynamically generated or repeated field values in a php file?

I have a form as follow.
<form action="action.php" method="post">
<div class="add_another">
<label for="BrotherAdmissionNumber" class="p-label-required">Admission Number</label>
<input type="text" name="BrotherAdmissionNumber[]" placeholder="Admission Number" />
<label for="varName" class="p-label-required">Name</label>
<input type="text" name="BrotherName[]" placeholder="Name" class="form-control" />
<label for="BrotherGrade" class="p-label-required">Grade</label>
<input type="text" id="varGrade" name="BrotherGrade[]" placeholder="Grade" class="form-control" />
<label for="BrotherClassTr" class="p-label-required">Class Teacher</label>
<input type="text" id="varClassTeacher" name="BrotherClassTr[]" placeholder="Class Teacher" class="form-control" />
<button class="btn add_field_button" style="float: right;">Add Another Brother</button>
</div>
<button class="submit" >Submit</button>
</form>
And I am using following jQuery to add another section to the form.
This will create the set of fields again.
<script>
var max_fields = 10;
var wrapper = jQuery(".add_another");
var add_sec_3 = '<div class="add_another" style="border-top: 1px solid #f0f0f0; border-spacing: 7px;"><label for="BrotherAdmissionNumber" class="p-label-required">Admission Number</label><input type="text" name="BrotherAdmissionNumber[]" placeholder="Admission Number" /><label for="varName" class="p-label-required">Name</label><input type="text" name="BrotherName[]" placeholder="Name" class="form-control" /><label for="BrotherGrade" class="p-label-required">Grade</label><input type="text" id="varGrade" name="BrotherGrade[]" placeholder="Grade" class="form-control" /><label for="BrotherClassTr" class="p-label-required">Class Teacher</label><input type="text" id="varClassTeacher" name="BrotherClassTr[]" placeholder="Class Teacher" class="form-control" />Remove</div>';
jQuery(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
jQuery(wrapper).append(add_sec_3); //add section
jQuery('.add_another .add_another:last').hide();
jQuery('.add_another .add_another:last').show('fast');
}
});
</script>
When clicking on add another button a section is added successfully.
So that's fine.
My issue is, I tried several methods of retrieving for arrays but nothing works.
I want to create another form with hidden fields of brother details to send it to another page.
How can I show the all generated fields values in a php page during the submission?
How do I have to update this action.php file?
action.php
<?php
$BrotherAdmissionNumber = $_POST['BrotherAdmissionNumber'];
$BrotherName = $_POST['BrotherName'];
$BrotherGrade = $_POST['BrotherGrade'];
$BrotherClassTr = $_POST['BrotherClassTr'];
foreach(){
//brothers details start here
?>
<form action="action2.php" method="post">
Admission Number :<?= $BrotherAdmissionNumber ?>
<input type="hidden" value="<?= $BrotherAdmissionNumber ?>" name="BrotherAdmissionNumber" />
Name :<?= $BrotherName ?>
<input type="hidden" value="<?= $BrotherName ?>" name="BrotherName" />
Grade :<?= $BrotherGrade ?>
<input type="hidden" value="<?= $BrotherGrade ?>" name="BrotherGrade" />
Teacher :<?= $BrotherClassTr ?>
<input type="hidden" value="<?= $BrotherClassTr ?>" name="BrotherClassTr"/>
<button name="send_to_other_page">CONFIRM</button> <!-- when this button is clicked the form shoukd send the hidden values -->
</form>
<?php } ?>
Now when send_to_other_page button is clicked the form should send the hidden values to next page action2.php
What I have to update in foreach loop above?
How I can collect these details in action2.php ?
Take a look at this link
You have to serialise form data and then you can easily retrieve and save in database.

PHP Variable in HTML page

Can i add variables to an HTML page ?
Example:
I have this:
<form action="[FORUM_LOGIN_URL/]" enctype="application/x-www-form-urlencoded" id="loginForm" method="post" name="loginForm" onsubmit="return handleLogin();">
<div id="loginFormContainer">
<input id="loginField" name="login" type="hidden" value="login">
<input id="redirectField" name="redirect" type="hidden" value="[REDIRECT_URL/]">
<div>
<label for="userNameField">[LANG]userName[/LANG]:</label><br>
<input id="userNameField" maxlength="[USER_NAME_MAX_LENGTH/]" name="username" readonly="readonly" type="text" value="Liel">
</div>
<div>
<label for="passwordField">אין צורך בסיסמה:</label><br>
<input id="passwordField" name="password" readonly="readonly" type="password">
</div>
<div>
<label for="languageSelection">[LANG]language[/LANG]:</label><br>
<select id="languageSelection" name="lang" onchange="ajaxChat.switchLanguage(this.value);">
</select>
</div>
<div>
<input id="loginButton" name="submit" type="submit" value="[LANG]login[/LANG]">
</div>
</div>
</form>
I need a variable in the value:
<input type="text" readonly="readonly" value="!variable!" name="username" id="userNameField" maxlength="[USER_NAME_MAX_LENGTH/]"/></div>
I have also the lang.php and if i can add variable in lang line:
$lang['langexample'] = 'variable';
You can simply add php code in html with php tag. For example
<?php echo $something; ?>
Before that you need to make sure, you file have to save as ".php" extension.

Categories