I have created a table which displays data from the databaseand have given the users the option to add rows to it. I have also added the necessary Ajax script so that whenever users click the button "add" a new row appears at the bottom of the table.
I am not able to find a way to save the data entered by the user in the newly added row to the database.
`<table border = "1" width = "50%" id="bill">
<tr>
<th>Item no</th>
<th>Product Code</th>
<th>Dates</th>
<th>Quantity</th>
<th>Amount</th>
<th>Net Amount</th>
<th>Tax</th>
<th>Choose option</th>
</tr>
<c:forEach var = "row" items = "${result.rows}">
<tr>
<td><c:out value = "${row.Item_no}"/></td>
<td><c:out value = "${row.Product_Code}"/></td>
<td><c:out value = "${row.Dates}"/></td>
<td><c:out value = "${row.Quantity}"/></td>
<td><c:out value = "${row.Amount}"/></td>
<td><c:out value="${row.Amount * row.Quantity}"/></td>
<td><input type="text" name="Tax" value=""></td>
<td><input type="text" name="gross" value=""></td>
</tr>
'
<input type="button" id="addrow" href="#" value="ADD">//the code to display the table with data from data base along with the add rows option
$(document).ready(function() {
var t = $('#bill');
var counter = $('#bill tr').size() + 1;
console.log(counter);
$('#addrow').on( 'click', function () {
t.append('<tr width="80%"><td><input type="text" name="Item_No" id="item" value = "" ></td><td><input type="text" name="Product_code" id="pc"></td>\n\
<td><input type="text" name="Date" id="date"></td><td><input type="text" name="Quantity" id="quantity"></td>\n\
<td><input type="text" name="Amount" id="amount"></td><td><input type="text" name="Net Amount" id="net"></td>\n\
<td><input type="text" name="Tax" id="tax"></td><td><input type="text" name="Gross" id="gross"></td>\n\
</tr>');
counter++;
} ); //The code which displays a new row at the bottom of the table when the users click add option . This is where the user enters his new data and I need to save it to the database.
Whenever you will click on addrow a new row will append in your table , so there are 2 way you can save value to database .They are as follow :
1). Whenever you append new row ,user will fill inputs to it and then you can create one submit btn in your row and i.e :
$('#addrow').on( 'click', function () {
t.append('<tr width="80%">
<form action="url" method="post">
<td><input type="text" name="Item_No" id="item" value = "" ></td>
<td><input type="text" name="Product_code" id="pc"></td>\n\
//your input field
<td><input type="submit" name="submit" value="add to db"><td>
</form>
</tr>');
counter++;
});
so,whenever you will click on submit btn data will go to urlprovided by you and then you can use ,request.getParameter("Product_code"); to get values and save values in database .
2) .You can use ajax ,whenever you append new row add a button also to it
$('#addrow').on( 'click', function () {
t.append('<tr width="80%">
<td><input type="text" name="Item_No" id="item" value = "" ></td>
<td><input type="text" name="Product_code" id="pc"></td>\n\
//your input field
<td><input type="button" name="submit" id="addtodb" value="add to db"><td>//add this button
</tr>');
counter++;
});
When user will click on btn you can get all values of input field and then pass to ajax and save them in database like below :
$('#addtodb').on( 'click', function () {
var pc=$("#pc").val();//getting value of input field
var quantity=$("#quantity").val();
$.ajax({
url:"yourclasspageurl",
method:"POST",
data:{ quantity:quantity,pc:pc} //passing data to specified url
success:function(data){
//do something
}
});
});
Get all , above values same using request.getParameter("quantity"); and save data to database.
Hope this help you .
Related
I am trying to get all the payments that is been selected through the checkboxes and then update the value in the database by '1' which means its paid. But when I try to get the values of checkbox array I only get 1 data in the array and not all selected. Any help?
View
#foreach($sellerID as $p)
<form action="/paid/{{$p->id}}" method="post" enctype="multipart/form-data">
#csrf
<td><input type="checkbox" name="checked[]" value="{{ $p->id }}"></td>
<td> {{$p->order_number}} </td>
<td>
<input id="commission" type="text" class="form-control" name="commission"
value="{{ $p->commission_value }}" readonly autocomplete="notes" style="width: 15%;" autofocus>
</td>
<td> {{$p->product_name}} </td>
<td>
#if($p->sellet_commission_paid == '0')
<button type="submit" class="btn btn-default" style="background-color:red">
<b><em>UNPAID</b></em>
</button>
#else
<b><em>PAID</b></em>
#endif
<td>
</form>
#endforeach
Controller
$checked = $request->input('checked');
// $checkboxes = $request->checked;
dd($checked);
die();
// dd(checkboxes);
you can put almost anything you like inside a form, but not inside a table. table has a more restricted structure which you must follow.
instead of wrapping table rows with forms, you can wrap the whole table with 1 form and submit all of it and just extract the details you need,
You need to put all input element inside form :
<form action="/route">
<input ...../>
<div>
<input ...../>
</div>
</form>
A form-associated element is, by default, associated with its ancestor form element, but may have a form attribute specified to override this.
If a form-associated element has a form attribute specified, then that attribute's value must be the ID of a form element in the element's owner Document.
Try using a foreach loop.
foreach($request->input('checked') as $check){
[sample variable] = $check;
}
The array will now be stored as $check and you should be able to get each value from the input
I am displaying the details of a particular table in my JSP page using a for-each loop in the following manner:
<c:forEach items="${List2}" var="alist">
<tr>
<td>
<input type="checkbox" name="check" value="${alist.check}">
</td>
<td>${alist.two}</td>
<td>${alist.three}</td>
<td>${alist.four}</td>
</tr>
</c:forEach>
<input type="radio" id="agree" value="Agree"/>Agree
<input type="radio" id="disagree" value="Disagree"/> Disagree
<input type="submit" value="Submit"/>
Now, there is no column named "check" in my DB. It is a transient field in my POJO. I do have a Agree/Disagree column in my DB. I want to check particular rows, click "Agree" or "Disagree" and on clicking Submit, the Agree/Disagree column for those records should be updated respectively. I'm using Spring MVC with Hibernate. Please tell me if any more info is required from my side.
You can do this with simple jQuery, Set class(say .chkbox here) to all checkboxes. Whenever any change event happened say,
if checked, the value of checkbox added to JS array.
If unchecked, the value will be removed from JS array.
$(document).ready(function() {
var selectedArray = [];
$('.chkbox').change(function() {
if($(this).is(":checked")) {
selectedArray.push($(this).val());
} else {
selectedArray.pop($(this).val());
}
//alert(selectedArray);
$("#textbox1").val(selectedArray);
});
});
Finally I used textbox to hold all the selected checkboxes value. You can make it into a hidden give a name attribute.
<input type="checkbox" class="chkbox" value="101"> This is 101<br />
<input type="checkbox" class="chkbox" value="102"> This is 102<br />
<input type="checkbox" class="chkbox" value="103"> This is 103<br />
<input type="checkbox" class="chkbox" value="110"> This is 110<br />
<input type="checkbox" class="chkbox" value="111"> This is 111<br />
<input type="checkbox" class="chkbox" value="112"> This is 112
<br />
<input type="hidden" id="textbox1" name="selectedArray" />
Sent it to a controller and split the values by comma (,).
Update :
In your code, you can update like this.
<c:forEach items="${List2}" var="alist">
<tr>
<td>
<input type="checkbox" class="chkbox" name="check" value="${alist.check}">
</td>
<td>${alist.two}</td>
<td>${alist.three}</td>
<td>${alist.four}</td>
</tr>
<input type="hidden" id="textbox1" name="selectedArray" />
</c:forEach>
That's all about required.
See the JS Demo how it works. Hope this helps.
I having a problem to validate multiple row data in Codeigniter by using form validation. Below code are ok to validate the 1st row of data. The subsequent row data i added via javascript was not able to validate. After i clicked submit, the next row that i disappeared. Anyone can help to this?
Appreciate and thanks in advance for any help.
HTML
<!-- Form -->
<?php echo form_open(base_url('air'), array('class'=> 'form-horizontal','id'=> 'calc_form')) ?>
<table>
<tbody id="item-row">
<tr>
<td>1</td>
<td><input class="form-control" type="text" name="load[1][grossweight]" value="<? =set_value('load[1][grossweight]')?>" placeholder="Gross Weight" >
<p><?=form_error('load[1][grossweight]'); ?></p>
</td>
</tr>
</tbody>
</table>
<input class="btn btn-success" onclick="addRow(this.form);" type="button" value="Add Item" /></strike>
<input id="getrates" type="submit" class="btn btn-primary btn-lg" name="getrate" value="Get Rate" />
</form>
Javascript Jquery
<script type="text/javascript">
var rowNum = 1;
function addRow(frm) {
rowNum ++;
var row = '<tr id="rowNum'+rowNum+'"><td>'+rowNum+'</td><td><input class="form-control" type="text" name="load['+rowNum+'][grossweight]" value="<?=set_value('load[rowNum][grossweight]')?>" placeholder="Gross Weight" ></td></tr>';
jQuery('#item-row').append(row);
}
function removeRow(rnum) {
jQuery('#rowNum'+rnum).remove();
}
</script>
Codeigniter Controller
// Posted array
$loads = $this->input->post('load');
if(!empty($loads))
{
foreach($loads as $id => $data)
{
$this->form_validation->set_rules('load['. $id .'][grossweight]', 'Gross Weight', 'required');
}
}
if ($this->form_validation->run() === FALSE)
{
$this->load->view('air/index', $data);
}
How can I add validation for minimum length to a Textbox and display custom error messages?
I want validation for the following:
UserName to have a minimum length of 6
Password and Confirm Password to match
Address1 is required
Here is the code for the popup template. The specified minlength in the template is not working but the maxlength is working properly.
<script id="popup_editor" type="text/x-kendo-template">
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<label for="UserName"><b>UserName*</b></label>
</td>
<td>
<div class="control-row">
<input type="text"
name="UserName"
id="UserName"
class="k-input k-textbox"
required
**minLength**="6"
maxlength="8"
pattern="[a-zA-Z0-9]+"
validationMessage="Please enter username"/>
<span class="k-invalid-msg" data-for="UserName" ></span>
</div>
</td>
<td></td>
<td></td>
</tr>
<tr>
<td>
<div>
<label for="Password"><b>Password*</b></label>
</div>
</td>
<td>
<div class="k-edit-label">
<input type="password"
id="Password"
name="Password"
class="k-input k-textbox"required
validationMessage="Please enter Password"/>
<span class="k-invalid-msg" data-for="Password"></span>
</div>
</td>
<td>
<div>
<label for="ConfirmPassword" style=""><b>Confirm Password</b></label>
</div>
</td>
<td>
<div class="k-edit-label">
<input type="password"
id="ConfirmPassword"
name="ConfirmPassword"
class="k-input k-textbox"required
validationMessage="Please enter Confirm Password"/>
</div>
</td>
</tr>
<tr>
<td>
<div>
<label for="Company_Name"><b>Company Name*</b></label>
</div>
</td>
<td>
<div class="k-edit-label">
<input name="Company_Name"
id="Company_Name"
required
pattern="[a-zA-Z0-9]+"
validationMessage="Please enter Valid CompanyName"/>
</div>
</td>
<td></td>
<td></td>
</tr>
<tr>
<td>
<div>
<label for="First_Name"><b>First Name*</b></label>
</div>
</td>
<td>
<div class="k-edit-label">
<input type="text"
name="First_Name"
id="First_Name"
data-type="string"
data-bind="value:First_Name"
class="k-input k-textbox" required
pattern="[a-zA-Z]+"
validationMessage="Please enter FirstName"/>
</div>
</td>
<td>
<div>
<label for="Last_Name"><b>Last Name*</b></label>
</div>
</td>
<td>
<div class="k-edit-label">
<input type="text"
id="Last_Name"
name="Last_Name"
class="k-input k-textbox" required
pattern="[a-zA-Z]+"
validationMessage="Please enter LastName"/>
</div>
</td>
</tr>
<tr>
<td>
<div>
<label for="Address1"><b>Address1*</b></label>
</div>
</td>
<td>
<div class="k-edit-label">
<textArea style="resize: none;"
rows="5"
cols="18"
name="Address1"
maxlength="150"
id="Address1" required
pattern="[a-zA-Z0-9]+"
validationMessage="Please enter Address">
</textarea>
</div>
</td>
</tr>
</table>
You can add custom validation for popup editing within the dataSource of your grid.
var dataSource = new kendo.data.DataSource({
transport: {
...
},
schema: {
model: {
id: "UserName",
fields: {
UserName: {}
Password: {}
ConfirmPassword: {}
Company_Name: {}
First_Name: {}
Last_Name: {}
Address1: {
validation: {
minLength: function (input) {
if (input.is("[name=UserName]")) {
return input.val().length >= 6;
}
return true;
},
match: function (input) {
if (input.is("[name=ConfirmPassword]")) {
return input.val() == $("#Password").val();
}
return true;
}
}
}
}
}
}
});
There are a few things to respect:
The validation runs for ALL input elements within your popup, therefore
you only have to define it for ONE of the fields in your model. Which one does not matter.
you have to check which input element is checked in the current run, which does the if statement in my example.
you have to append a return true at the end of each and every rule you define or otherwise you'll get an error message for every input you're not explicitly checking. If there's no return value passed, kendo automatically assumes the check had a false result.
Every validation rule needs its own validation message or otherwise your validation tooltip box will only display a warning logo without any text. You can add it as an html attribute (data-{validation rule}-msg) in your input elements, like this:
<input type="text"
name="UserName"
id="UserName"
class="k-input k-textbox"
required
maxlength="8"
pattern="[a-zA-Z0-9]+"
validationMessage="Please enter username"
data-minLenght-msg="Username must be at least 6 characters"/>
<input type="password"
id="ConfirmPassword"
name="ConfirmPassword"
class="k-input k-textbox"
required
validationMessage="Please enter Confirm Password"
data-match-msg="Password and confirmation must be equal"/>
Hope this helps
In rules add this:
match: function (input) {
if ((input.is('[name=\'Password\']') || input.is('[name=\'ConfirmPassword\']'))&& $('#ConfirmPassword').length !== 0) {
if ($('#Password').val().length > 0 && $('#ConfirmPassword').val().length > 0) {
if (input.is('[name=\'Password\']')) {
return input.val() === $('#ConfirmPassword').val();
}
else if (input.is('[name=\'ConfirmPassword\']')) {
return input.val() === $('#Password').val();
}
}
}
return true;
},
minLength: function (input) {
if (input.is("[name=UserName]")) {
return input.val().length >= 6;
}
return true;
},
requiredAddress: function (input) {
if (input.is("[name=Address1]")) {
return $('#Address1').val() !== '' ? false : true;
}
return true;
}
Having a hard time figuring out how to add an increment value for each id attribute within the div contents with cloned jQuery object.
http://jsfiddle.net/hvK8d/
===================== HTML=====================
<div class="upload-file-container">
<div class="uploadFile left clearfix">
<input type="file" id="FileUpload1">
<table id="RadioButtonList1">
<tbody>
<tr>
<td><input type="radio" value="Resume" id="RadioButtonList1_1">
<label for="RadioButtonList1_1">Resume</label></td>
<td><input type="radio" value="Letter of Recommendation" id="RadioButtonList1_2">
<label for="RadioButtonList1_2">Letter of Recommendation</label></td>
<td><input type="radio" value="Other" id="RadioButtonList1_3">
<label for="RadioButtonList1_3">Other</label></td>
</tr>
</tbody>
</table>
</div>
Remove </div>
<div class=""><a class="plus" href="javascript:;">plus one</a></div>
===================== JQUERY =====================
//Cloning upload file control
$('.remove').live('click', function () {
if (confirm("Are you sure you wish to remove this item?")) {
$(this).parent().slideUp('fast', function () {
$(this).remove();
});
}
return false;
});
$('.plus').click(function () {
console.log('cloning');
var divCloned = $('.upload-file-container:first').clone();
divCloned.hide().insertAfter('.upload-file-container:last').slideDown('fast');
return false;
});
For the sake of completeness I will put here a small solution making use of a "template."
A class for hiding the template:
.upload-file-container.template {
display: none;
}
A small function to do replacements:
$.fn.template = function(variables) {
return this.each(function() {
this.innerHTML = this.innerHTML.replace(/{{(.+)}}/g, function(match, variable) {
return variables[variable];
});
return this;
});
};
A template:
<div class="upload-file-container template">
<div class="uploadFile left clearfix">
<input type="file" id="FileUpload{{id}}">
<table id="RadioButtonList{{id}}"><tbody>
<tr>
<td>
<input type="radio" value="Resume" id="RadioButtonList{{id}}_1">
<label for="RadioButtonList{{id}}_1">Resume</label>
</td>
</tr>
</tbody></table>
</div>
</div>
Usage:
var count = 0;
var divCloned = $(".upload-file-container.template")
.clone()
.removeClass("template")
.template({
id: count++
});
Instead of using numbered IDs, you should be using the array-like notation (e.g. RadioButtonList[]) in the name attribute, and wrap your labels around the inputs:
<td>
<label for="RadioButtonList1_1">
<input type="radio" value="Resume" name="RadioButtonList1[]">
Resume
</label>
</td>
<td>
<label for="RadioButtonList1_2">
<input type="radio" value="Letter of Recommendation" name="RadioButtonList2[]">
Letter of Recommendation
</label>
</td>
<td>
<label for="RadioButtonList1_3">
<input type="radio" value="Other" name="RadioButtonList3[]">
Other
</label>
</td>
P.S. You should also consider using a more descriptive name than RadioButtonList.