good morning,
maybe a easy question for you but a big step for me ;)
I have this simple and table:
<table>
<tr>
<td><input type="text" class="myFieldA" onclick="doSomething()" value="11"></td>
<td><input type="text" class="myFieldB" onclick="doSomething()" value="22"></td>
</tr>
<tr>
<td><input type="text" class="myFieldA" onclick="doSomething()" value="33"></td>
<td><input type="text" class="myFieldB" onclick="doSomething()" value="44"></td>
</tr>
<tr>
<td><input type="text" class="myFieldA" onclick="doSomething()" value="55"></td>
<td><input type="text" class="myFieldB" onclick="doSomething()" value="66"></td>
</tr>
</table>
function doSomething() {
//code
}
As you can see I have 3 rows which the same structure.
If you click on any input field the function doSomething will call.
Now my question (example):
If I click on the input field with the value 44 I would like to get all values of this row (row 2).
The output should be:
33
44
Can you explain me please how I can realize this?
Thank you very much !!
var doSomething;
$( document ).ready(function() {
doSomething = function(el) {
const tdInputChild = $(el).parent().parent().find('input');
console.log('myFieldA',$(tdInputChild[0]).val());
console.log('myFieldB',$(tdInputChild[1]).val());
// For Dynamic you can use loop or map to get val.
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td><input type="text" class="myFieldA" onclick="doSomething(this)" value="11"></td>
<td><input type="text" class="myFieldB" onclick="doSomething(this)" value="22"></td>
</tr>
<tr>
<td><input type="text" class="myFieldA" onclick="doSomething(this)" value="33"></td>
<td><input type="text" class="myFieldB" onclick="doSomething(this)" value="44"></td>
</tr>
<tr>
<td><input type="text" class="myFieldA" onclick="doSomething(this)" value="55"></td>
<td><input type="text" class="myFieldB" onclick="doSomething(this)" value="66"></td>
</tr>
</table>
To get all the inputs for a row, you can add the click event to the row itself.
$("tr").click...
then access the inputs with $(this).find(".myFieldA") (etc)
$("table tr").click(function() {
console.log($(this).find(".myFieldA").val(), $(this).find(".myFieldB").val());
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td><input type="text" class="myFieldA" value="11"></td>
<td><input type="text" class="myFieldB" value="22"></td>
</tr>
<tr>
<td><input type="text" class="myFieldA" value="33"></td>
<td><input type="text" class="myFieldB" value="44"></td>
</tr>
<tr>
<td><input type="text" class="myFieldA" value="55"></td>
<td><input type="text" class="myFieldB" value="66"></td>
</tr>
</table>
In some cases there may be a td you don't want to have a click event on, so it's easier to add the click event to the cells you do want or to all the cells excluding the ones you don't want:
$("td.click").click...
$("td:not(.noclick)").click...
with the event on the td (or on the input if you prefer) you can access the tr with
var tr = $(this).closest("tr");
$("table tr td:not(.noclick)").click(function() {
var row = $(this).closest("tr");
console.log(row.find(".myFieldA").val(), row.find(".myFieldB").val());
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td><input type="text" class="myFieldA" value="11"></td>
<td><input type="text" class="myFieldB" value="22"></td>
<td class="noclick">click here doesn't give values</td>
</tr>
<tr>
<td><input type="text" class="myFieldA" value="33"></td>
<td><input type="text" class="myFieldB" value="44"></td>
<td class="noclick">click here doesn't give values</td>
</tr>
<tr>
<td><input type="text" class="myFieldA" value="55"></td>
<td><input type="text" class="myFieldB" value="66"></td>
<td class="noclick">click here doesn't give values</td>
</tr>
</table>
If you don't know how many inputs there are and just want an array of values, you can use .map
var values = row.find(":input").map((i,e)=> $(e).val()).toArray();
where row is the tr from previous and .toArray() converts to a "clean" array (useful with console.log)
$("table tr td:not(.noclick)").click(function() {
var row = $(this).closest("tr");
var values = row.find(":input").map((i,e)=> $(e).val()).toArray();
console.log(values)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td><input type="text" class="myFieldA" value="11"></td>
<td><input type="text" class="myFieldB" value="22"></td>
<td class="noclick">click here doesn't give values</td>
</tr>
<tr>
<td><input type="text" class="myFieldA" value="33"></td>
<td><input type="text" class="myFieldB" value="44"></td>
<td class="noclick">click here doesn't give values</td>
</tr>
<tr>
<td><input type="text" class="myFieldA" value="55"></td>
<td><input type="text" class="myFieldB" value="66"></td>
<td class="noclick">click here doesn't give values</td>
</tr>
</table>
This can then be extended to give you an array of objects with key/value
var values = row.find(":input").map((i,e)=> {
return {
key : e.className,
value : $(e).val()
}
}).toArray();
$("table tr td:not(.noclick)").click(function() {
var row = $(this).closest("tr");
var values = row.find(":input").map((i,e)=> {
return {
key : e.className,
value : $(e).val()
}
}).toArray();
console.log(values)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td><input type="text" class="myFieldA" value="11"></td>
<td><input type="text" class="myFieldB" value="22"></td>
<td class="noclick">click here doesn't give values</td>
</tr>
<tr>
<td><input type="text" class="myFieldA" value="33"></td>
<td><input type="text" class="myFieldB" value="44"></td>
<td class="noclick">click here doesn't give values</td>
</tr>
<tr>
<td><input type="text" class="myFieldA" value="55"></td>
<td><input type="text" class="myFieldB" value="66"></td>
<td class="noclick">click here doesn't give values</td>
</tr>
</table>
If you prefer an object (probably the most useful) rather than a key/value array:
var values = {};
row.find(":input").each((i,e) =>
values[e.className] = $(e).val()
);
$("table tr td:not(.noclick)").click(function() {
var row = $(this).closest("tr");
var values = {};
row.find(":input").each((i,e) =>
values[e.className] = $(e).val()
);
console.log(values)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td><input type="text" class="myFieldA" value="11"></td>
<td><input type="text" class="myFieldB" value="22"></td>
<td class="noclick">click here doesn't give values</td>
</tr>
<tr>
<td><input type="text" class="myFieldA" value="33"></td>
<td><input type="text" class="myFieldB" value="44"></td>
<td class="noclick">click here doesn't give values</td>
</tr>
<tr>
<td><input type="text" class="myFieldA" value="55"></td>
<td><input type="text" class="myFieldB" value="66"></td>
<td class="noclick">click here doesn't give values</td>
</tr>
</table>
Since the click event is bubbling up, you can set a listener on the row instead on each input.
$('table tr').click(doSomething);
function doSomething() {
let vals = [];
$(this).find('input').each(function() {
vals.push(this.value);
});
console.log(vals.join());
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td><input type="text" class="myFieldA"value="11"></td>
<td><input type="text" class="myFieldB" value="22"></td>
</tr>
<tr>
<td><input type="text" class="myFieldA" value="33"></td>
<td><input type="text" class="myFieldB" value="44"></td>
</tr>
<tr>
<td><input type="text" class="myFieldA" value="55"></td>
<td><input type="text" class="myFieldB" value="66"></td>
</tr>
</table>
This is pretty simple without adding jQuery.
Add a click event listener on each of your table rows (your trs), then loop through the children (this will be your td elements).
Finally, get the value of each child of your td, then output it:
let rows = document.querySelectorAll('tr');
let results = document.getElementById('results');
rows.forEach((row) => {
row.addEventListener('click', getValues);
});
function getValues() {
let currentRow = event.currentTarget;
let output = [];
// get the children (your td elements) of the currently clicked row
for (let i = 0; i < currentRow.children.length; i++) {
//get the value of the child input element
output.push(currentRow.children[i].firstChild.value);
}
results.innerText= output;
}
<table>
<tr>
<td><input type="text" class="myFieldA" value="11"></td>
<td><input type="text" class="myFieldB" value="22"></td>
</tr>
<tr>
<td><input type="text" class="myFieldA" value="33"></td>
<td><input type="text" class="myFieldB" value="44"></td>
</tr>
<tr>
<td><input type="text" class="myFieldA" value="55"></td>
<td><input type="text" class="myFieldB" value="66"></td>
</tr>
</table>
<br />
<div id="results">
</div>
Related
How can I make Jquery to detect if the input field inside the table is empty?
If inputs are empty I would to like to hide the row. How can I do it? I manage to hide the row if the input weren't inside the row.
<table id="entryTable">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
<tr>
<tr>
<td><input type="text" name="f_name" /></td>
<td><input type="text" name="l_name" /></td>
<td><input type="text" name="age" /></td>
</tr>
<tr>
<td><input type="text" name="f_name" /></td>
<td><input type="text" name="l_name" /></td>
<td><input type="text" name="age" /></td>
</tr>
</table>
$('#entryTable tr').each(function() {
if ($(this).find('td:empty').length) $(this).remove();
});
You can try something like this:
$('#entryTable tbody tr').each(function() {
var n = $(this).find("td").filter(function() {
if ($("input", this).length) return $("input", this).val().length == 0;
return $(this).text().length == 0
})
if (n.length == $(this).find("td").length) $(this).remove();
});
Problem is that none of your td is empty because of the input.
Demo
$('#entryTable tbody tr').each(function() {
var n = $(this).find("td").filter(function() {
if ($("input", this).length) return $("input", this).val().length == 0;
return $(this).text().length == 0
})
if (n.length == $(this).find("td").length) $(this).remove();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="entryTable">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" name="f_name" /></td>
<td><input type="text" name="l_name" /></td>
<td><input type="text" name="age" /></td>
</tr>
<tr>
<td><input type="text" name="f_name" /></td>
<td><input type="text" name="l_name" /></td>
<td><input type="text" name="age" /></td>
</tr>
<tr>
<td><input type="text" name="f_name" /></td>
<td><input type="text" name="l_name" value="name" /></td>
<td><input type="text" name="age" /></td>
</tr>
</tbody>
</table>
$('#entryTable tbody tr').each(function() {
var n = $(this).find("td").filter(function() {
if ($("input", this).length)
return $("input", this).val().length == 0;
return $(this).text().length == 0
})
if (n.length == $(this).find("td").length) $(this).remove();
});
tabel,tr,td{
border:1px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="entryTable">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<tr>
<td><input type="text" name="f_name" value="do" /></td>
<td><input type="text" name="l_name" value="not" /></td>
<td><input type="text" name="age" /></td>
</tr>
<tr>
<td><input type="text" name="f_name" /></td>
<td><input type="text" name="l_name" /></td>
<td><input type="text" name="age" /></td>
</tr>
<tr>
<td><input type="text" name="f_name" value="do" /></td>
<td><input type="text" name="l_name" value="not" /></td>
<td><input type="text" name="age" /></td>
</tr>
<tr>
<td><input type="text" name="f_name" value="do" /></td>
<td><input type="text" name="l_name" value="not" /></td>
<td><input type="text" name="age" /></td>
</tr>
<tr>
<td><input type="text" name="f_name" value="do" /></td>
<td><input type="text" name="l_name" value="not" /></td>
<td><input type="text" name="age" /></td>
</tr>
</table>
I've been stuck for hours on a problem that might be actually simple but I can't manage to find the solution.
To be short, I want to know the index (ie the first / the second / the third etc...) of a checkbox when clicking on it.
Here's the jsfiddle showing what's currently working and what is not. I've tried many things but couldn't find the solution I'm looking for.
https://jsfiddle.net/cpydwqk3/2/ <div>(this example applies only for the class "admin" which applies herself on the checkbox "admin").
function usermodif(identifiant) {
alert($('.admin').index(this)); //return -1
}
$("tr").click(function() {
alert($("tr").index(this)); //work but applies on whole line
});
<table>
<caption>Utilisateurs</caption>
<tr>
<th scope="col">pseudo</th>
<th scope="col">points</th>
<th scope="col">points_session</th>
<th scope="col">admin</th>
<th scope="col">banni</th>
</tr>
<tr>
<th scope="row">firstguy</th>
<td><input type="text" name="points" value="45" id="points"></td>
<td><input type="text" name="points_session" value="6" id="points_session"></td>
<td><input type="checkbox" class="admin" name="admin" onclick="usermodif(this.className)"> </td>
<td><input type="checkbox" class="banni" name="banni"> </td>
</tr>
<tr>
<th scope="row">Nico</th>
<td><input type="text" name="points" value="21" id="points"></td>
<td><input type="text" name="points_session" value="21" id="points_session"></td>
<td><input type="checkbox" class="admin" name="admin" onclick="usermodif(this.className)" checked> </td>
<td><input type="checkbox" class="banni" name="banni"> </td>
</tr>
<tr>
<th scope="row">anonyme</th>
<td><input type="text" name="points" value="0" id="points"></td>
<td><input type="text" name="points_session" value="0" id="points_session"></td>
<td><input type="checkbox" class="admin" name="admin" onclick="usermodif(this.className)"> </td>
<td><input type="checkbox" class="banni" name="banni"> </td>
</tr>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
Thanks in advance guys!
Traverse first tp the closest TR. Than get that closest TR index.
TR has an index amongst TBODY TRs, but not the checkbox.
Also, never use inline JavaScript on* attributes, same as you hopefully don't use inline style attributes. JS and CSS should be in one place only, and that's their respective files or tags.
$(".admin").on("click", function() {
const $par = $(this).closest("tr");
alert($par.index());
});
<table>
<tr>
<th scope="row">firstguy</th>
<td><input type="text" name="points" value="45" id="points"></td>
<td><input type="text" name="points_session" value="6" id="points_session"></td>
<td><input type="checkbox" class="admin" name="admin"> </td>
<td><input type="checkbox" class="banni" name="banni"> </td>
</tr>
<tr>
<th scope="row">firstguy</th>
<td><input type="text" name="points" value="45" id="points"></td>
<td><input type="text" name="points_session" value="6" id="points_session"></td>
<td><input type="checkbox" class="admin" name="admin"> </td>
<td><input type="checkbox" class="banni" name="banni"> </td>
</tr>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
Variables in below codes of js are
a= first column of table
b= second column
c= third column
d= forth column
$(document).on('keyup','input.expenses',function(){
var $row = $(this).closest('tr'),
// collect the 3 inputs in this row only
$exp = $row.find('.expenses'),
a = $exp.eq(0).val(),// first is a
b = $exp.eq(1).val(),// second is b
c = $exp.eq(2).val(),// third is c
d = $exp.eq(3).val();// fourth is d
var total = (a*c - b*c)-d;
$row.find("#toplamdisplay").val(total);
})
As it seems all variables will be calculated in the same line (same tr)
How can I use variable d (field of d) which is located in other line ?
For example
Current tags are
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
</tr>
New tags I want to use are something like this
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
</tr>
<tr>
<td>d</td>
</tr>
What I want is to have one payment field in last instead of having it in column. Please see below tags
<html>
<head>
<title></title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js" /></script>
</head>
<body>
<table border="1">
<tr>
<td colspan="2">
<script>
$(document).on('keyup','input.expenses',function(){
var $row = $(this).closest('tr'),
// collect the 3 inputs in this row only
$exp = $row.find('.expenses'),
a = $exp.eq(0).val(),// first is a
b = $exp.eq(1).val(),// second is b
c = $exp.eq(2).val(),// third is c
d = $exp.eq(3).val();// fourth is d
//(B TIMES C)-(A TIMES C)
var total = (a*c - b*c)-d;
$row.find("#toplamdisplay").val(total);
$row.find('.expenses_sum').text( total)
})
$(document).on('keyup','input.expenses',function(){
$expenses_sum = $(this).parents('table').find('.expenses_sum');
$expenseTotal = $(this).parents('table').find('#expenses_sum2');
$expenseTotal.val('0');
$.each($expenses_sum,function(index,object){
if($(object).val()!='')
{
$expenseTotal.val(parseInt($expenseTotal.val())+parseInt($(object).val()));
}
})
});
$(document).on('keyup','input.expenses',function(){
$expenses_sum = $(this).parents('table').find('.expenses_sum2');
$expenseTotal = $(this).parents('table').find('#expenses_sum3');
$expenseTotal.val('0');
$.each($expenses_sum,function(index,object){
if($(object).val()!='')
{
$expenseTotal.val(parseInt($expenseTotal.val())+parseInt($(object).val()));
}
})
});
</script>
</td>
</tr>
<tr>
<th>Quantity</th>
<th>Quantity Returned</th>
<th>Unit Price</th>
<th>Total Price</th>
<th bgcolor="red">Payment</th>
</tr>
<tr>
<td><input type="number" name="" class="expenses Quantity" /></td>
<td><input type="number" name="" data-diff="true" class="expenses QuantityBack" /></td>
<td><input type="number" name="" class="expenses title UnitPrice" value="150" /></td>
<td><span class="toplamc"></span></td>
<td bgcolor="red"><input type="number" name="" data-diff="true" class="expenses paid" />
<input type="number" hidden name="" readonly id="toplamdisplay" class="expenses_sum" /></td>
</tr>
<tr>
<td><input type="number" name="" class="expenses Quantity" /></td>
<td><input type="number" name="" data-diff="true" class="expenses QuantityBack" /></td>
<td><input type="number" name="" class="expenses title UnitPrice" value="200" /></td>
<td><span class="toplamc"></span></td>
<td bgcolor="red"><input type="number" name="" data-diff="true" class="expenses paid" />
<input type="number" hidden name="" readonly id="toplamdisplay" class="expenses_sum" /></td>
</tr>
<tr>
<td rowspan="3"></td>
<td rowspan="3"></td>
<td>Current Value</td>
<td><input type="number" readonly id="expenses_sum2" class="expenses_sum2" /></td>
<td rowspan="3"></td>
</tr>
<tr>
<td>Last Value</td>
<td><input type="number" readonly class="expenses_sum2" name="" value="66" /></td>
</tr>
<tr>
<td><b>Total</b></td>
<td><input type="number" name="TotalCredit" readonly id="expenses_sum3"></td>
</tr>
<tr>
<td colspan="3"></td>
<td><font color="red">I NEED FIELD OF PAYMENT TO BE HERE</font></td>
</tr>
</table>
</body>
</html>
Please anyone can help me to edit js to work with my second logic of tags
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="lightgray">
<fieldset>
<legend align="center"><blink><font color="grakgreen">Registration</font></blink></legend>
<form action="SaveServlet" method="post">
<table align="center">
<tr>
<th colspan="6">APPLICATION FOR EMPLOYMENT<BR>(Pre-Employment Questionnaire)</th>
</tr>
<tr>
</tr>
<tr></tr>
<tr>
</tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr bgcolor="lightgreen">
<th colspan="6">PERSONAL INFORMATION</th>
</tr>
<tr>
<td>NAME:</td>
<td><input type="text" name="lnam" value="" size="30" placeholder="last name"></td>
<td><input type="text" name="fnam" value="" placeholder="first name"></td>
<td colspan="2"><input type="text" name="mnam" value=""size="46" placeholder="middle name"></td>
</tr>
<tr>
<td>PRESENT ADDRESS:</td>
<td><input type="text" name="stree" value=""size="30"placeholder="Street or House NO:"></td>
<td><input type="text" name="cit" value="" placeholder="city"></td>
<td><input type="text" name="stat" value="" placeholder="state"></td>
<td><input type="text" name="zi" value="" placeholder="zip code"></td>
</tr>
<tr>
<td>PERMANENT ADDRESS:</td>
<td><input type="text" name="pstree" value=""size="30" placeholder="Street or House NO:"></td>
<td><input type="text" name="pcit" value="" placeholder="city"></td>
<td><input type="text" name="pstat" value=""placeholder="state"></td>
<td><input type="text" name="pzi" value="" placeholder="zip code"></td>
</tr>
<tr>
<td>PHONE NUMBER:</td>
<td><input type="text" name="phn" value="" size="30"placeholder="Phnoe number"></td>
<td colspan="2">ALTERNATE PHONE NUMBER:</td>
<td><input type="text" name="alpn" value=""placeholder="Alternate phone number"></td>
</tr>
<tr>
<td colspan="3">ARE YOU PREVENTED FROM LAWFULLY BECOMEING EMPLOYEED IN THIS COUNTRY BECAUSE OF VISA OR IMMIGRATION STATUS?</td>
<td colspan="2">Yes<input type="radio" name="ye" value="yes">No<input type="radio" name="ye" value="no"></td>
</tr>
<tr>
</tr>
<tr></tr>
<tr>
</tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr bgcolor="lightgreen">
<th colspan="6">EMPLOYEMENT DESIRED</th>
</tr>
<tr>
<td>POSITION:</td>
<td><input type="text" name="positio" value="" size="30"></td>
<td>DATE YOU CAN START:</td>
<td><input type="text" name="cdat" value=""></td>
<td>SALARY DESIRED:</td>
<td><input type="text" name="salar" value=""></td>
</tr>
<tr>
<td>ARE YOU EMPLOYEED NOW?</td>
<td><input type="text" name="empno" value=""size="30"></td>
<td colspan="3">IF SO MAY WE INQUIRE OF YOUR PRESENT EMPLOYER? </td>
<td><input type="text" name="inquir" value=""></td>
</tr>
<tr>
<td>EVER APPLIED TO THIS COMPANY BEFORE?</td>
<td><input type="text" name="applie" value=""size="30"></td>
<td>WHERE?</td>
<td><input type="text" name="wher" value=""></td>
<td>WHEN?</td>
<td><input type="text" name="whe" value=""></td>
</tr>
<tr>
<td>REFERED BY:</td>
<td><input type="text" name="rnam" value=""size="30"></td>
</tr>
<tr>
</tr>
<tr bgcolor="lightgreen">
</tr>
<tr>
<th>EDUCATION</th>
<th>NANME AND LOCATION OF SCHOOL</th>
<th>*NO OF YEARS ATTENDED</th>
<th>*DID YOU GRADUATE?</th>
<th>SUBJECTS STUDIED</th>
</tr>
<tr>
<td>SSC</td>
<td><input type="text" name="schol" value=""size="30"></td>
<td><input type="text" name="year" value=""></td>
<td><input type="text" name="graduat" value=""></td>
<td><input type="text" name="subject" value=""></td>
</tr>
<tr>
<td>INTER/DIPLOMA</td>
<td><input type="text" name="nschol" value=""size="30"></td>
<td><input type="text" name="nyear" value=""></td>
<td><input type="text" name="ngraduat" value=""></td>
<td><input type="text" name="nsubject" value=""></td>
</tr>
<tr>
<td>DEGREE/B.TECH/B.E</td>
<td><input type="text" name="naschol" value="" size="30"></td>
<td><input type="text" name="nayear" value=""></td>
<td><input type="text" name="nagraduat" value=""></td>
<td><input type="text" name="nasubject" value=""></td>
</tr>
<tr>
<td>PG</td>
<td><input type="text" name="nameschol" value="" size="30"></td>
<td><input type="number" name="nameyear" value=""></td>
<td><input type="text" name="namegraduat" value=""></td>
<td><input type="text" name="namesubject" value=""></td>
</tr>
<tr>
</tr>
<tr></tr>
<tr>
</tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr bgcolor="lightgreen">
<th colspan="6">GENERAL</th>
</tr>
<tr>
<td>SUBJECTS OF SPECICAL STUDY OR RESEARCH WORK</td>
<td><textarea rows="3" cols="35" name="specia"></textarea></td>
<td>SPECIAL SKILLS</td>
<td colspan="2"><textarea rows="3" cols="47" name="skill"></textarea></td>
</tr>
<tr>
</tr>
<tr></tr>
<tr>
</tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr bgcolor="lightgreen">
<th colspan="6"><b>FORMER EMPLOYERS</b>(LIST BELLOW LAST THREE EMPLOYERS, STARTING WITH LAST ONE FIRST).</th>
</tr>
<tr>
<th colspan="2"> DATE MONTH AND YEAR</th>
</tr>
<tr>
<th>From</th>
<th>TO</th>
<th>NAME AND ADDRESS OF EMPLOYER</th>
<th>SALARY</th>
<th>POSITION</th>
<th>REAON FOR LEAVING</th>
</tr>
<tr>
<td><input type="text" name="fdat" value="" placeholder="mm/dd/yyyy" size="28"></td>
<td><input type="text" name="tdat" value="" placeholder="mm/dd/yyyy" size="28"></td>
<td><input type="text" name="nem" size="35"></td>
<td><input type="text" name="nsalar"></td>
<td><input type="text" name="npositio"></td>
<td><input type="text" name="nreaso"></td>
</tr>
<tr>
<td><input type="text" name="efdat" value="" placeholder="mm/dd/yyyy" size="28"></td>
<td><input type="text" name="etdat" value="" placeholder="mm/dd/yyyy" size="28"></td>
<td><input type="text" name="eem" size="35"></td>
<td><input type="text" name="esalar"></td>
<td><input type="text" name="epositio"></td>
<td><input type="text" name="ereaso"></td>
</tr>
<tr>
<td><input type="text" name="dfdat" value="" placeholder="mm/dd/yyyy" size="28"></td>
<td><input type="text" name="dtdat" value="" placeholder="mm/dd/yyyy" size="28"></td>
<td><input type="text" name="dem"size="35"></td>
<td><input type="number" name="dsalar"></td>
<td><input type="text" name="dpositio"></td>
<td><input type="text" name="dreaso"></td>
</tr>
<tr>
</tr>
<tr></tr>
<tr>
</tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr bgcolor="lightgreen">
<th colspan="6">REFERENCES:GIVE THE NAME NAMES OF 3 PERSONS NOT RELATED TO YOU, WHOM YOU HAVE KNOW AT LEAST ONE YEAR.</th>
</tr>
<tr>
<th>NAME</th>
<th>ADDRESS</th>
<th>BUSSINESS</th>
<th>YEARS ACQUAINTED</th>
</tr>
<tr>
<td><input type="text" name="unam" value=""></td>
<td><input type="text" name="uaddres" value=""></td>
<td><input type="text" name="ubusines" value=""></td>
<td><input type="text" name="uyea" value=""></td>
</tr>
<tr>
<td><input type="text" name="knam" value=""></td>
<td><input type="text" name="kaddres" value=""></td>
<td><input type="text" name="kbusines" value=""></td>
<td><input type="text" name="kyea" value=""></td>
</tr>
<tr>
<td><input type="text" name="nname" value=""></td>
<td><input type="text" name="naddress" value=""></td>
<td><input type="text" name="nbusiness" value=""></td>
<td><input type="text" name="nyear" value=""></td>
</tr>
<tr>
</tr>
<tr></tr>
<tr>
</tr>
<tr></tr>
</table>
<center>
<input type="reset" value="reset">
<input type="submit" value="submit">
</center>
</form>
</fieldset>
</body>
</html>
SaveServlet code, I used annotation
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
res.setContentType("html/text");
PrintWriter out=res.getWriter();
String lnam=req.getParameter("lnam");
String fnam=req.getParameter("fnam");
String mnam=req.getParameter("mnae");
String stree=req.getParameter("stree");
String cit=req.getParameter("cit");
String stat=req.getParameter("stat");
String zi=req.getParameter("zi");
String pstree=req.getParameter("pstree");
String pcit=req.getParameter("pcit");
String pstat=req.getParameter("pstat");
String pzi=req.getParameter("pzi");
String phn=req.getParameter("phn");
String alpn=req.getParameter("alpn");
String ye=req.getParameter("ye");
String positio=req.getParameter("positio");
String cdat=req.getParameter("cdat");
String salar=req.getParameter("salar");
String empno=req.getParameter("empno");
String inquir=req.getParameter("inquir");
String applie=req.getParameter("applie");
String wher=req.getParameter("wher");
String whe=req.getParameter("whe");
String rnam=req.getParameter("rnam");
String schol=req.getParameter("schol");
String year=req.getParameter("year");
String graduat=req.getParameter("graduat");
String subject=req.getParameter("subject");
String nschol=req.getParameter("nschol");
String nyear=req.getParameter("nyear");
String ngraduat=req.getParameter("ngraduat");
String nsubject=req.getParameter("nsubject");
String naschol=req.getParameter("naschol");
String nayear=req.getParameter("nayear");
String nagraduat=req.getParameter("nagraduat");
String nasubject=req.getParameter("nasubject");
String nameschol=req.getParameter("nameschol");
String nameyear=req.getParameter("nameyear");
String namegraduat=req.getParameter("namegraduat");
String namesubject=req.getParameter("namesubject");
String specia=req.getParameter("specia");
String skill=req.getParameter("skill");
String fdat=req.getParameter("fdat");
String tdat=req.getParameter("tdat");
String nem=req.getParameter("nem");
String nsalar=req.getParameter("nsalar");
String npositio=req.getParameter("npositio");
String nreaso=req.getParameter("nreaso");
String efdat=req.getParameter("efdat");
String etdat=req.getParameter("etdat");
String eem=req.getParameter("eem");
String esalar=req.getParameter("esalar");
String epositio=req.getParameter("epositio");
String ereaso=req.getParameter("ereaso");
String dfdat=req.getParameter("dfdat");
String dtdat=req.getParameter("dtdat");
String dem=req.getParameter("dem");
String dsalar=req.getParameter("dsalar");
String dpositio=req.getParameter("dpositio");
String dreaso=req.getParameter("dreaso");
String unam=req.getParameter("unam");
String uaddres=req.getParameter("uaddres");
String ubusines=req.getParameter("ubusines");
String uyea=req.getParameter("uyea");
String knam=req.getParameter("knam");
String kaddres=req.getParameter("kaddres");
String kbusines=req.getParameter("kbusines");
String kyea=req.getParameter("kyea");
String nnam=req.getParameter("nnam");
String naddres=req.getParameter("naddres");
String nbusines=req.getParameter("nbusines");
String nyea=req.getParameter("nyea");
Empb b=new Empb();
b.setLnam(lnam);
b.setFnam(fnam);
b.setMnam(mnam);
b.setStree(pstree);
b.setCit(cit);
b.setStat(stat);
b.setZi(zi);
b.setPstree(pstree);
b.setPcit(pcit);
b.setPstat(pstat);
b.setPzi(pzi);
b.setPhn(phn);
b.setAlpn(alpn);
b.setYe(ye);
b.setPositio(positio);
b.setCdat(cdat);
b.setSalar(salar);
b.setEmpno(empno);
b.setInquir(inquir);
b.setApplie(applie);
b.setWher(wher);
b.setWhe(whe);
b.setRnam(rnam);
b.setSchol(schol);
b.setYear(year);
b.setGraduat(graduat);
b.setSubject(subject);
b.setNschol(nschol);
b.setNyear(nyear);
b.setNgraduat(ngraduat);
b.setNsubject(nsubject);
b.setNaschol(naschol);
b.setNayear(nayear);
b.setNagraduat(nagraduat);
b.setNasubject(nasubject);
b.setNameschol(nameschol);
b.setNameyear(nameyear);
b.setNamegraduat(namegraduat);
b.setNamesubject(namesubject);
b.setSpecia(specia);
b.setSkill(skill);
b.setFdat(fdat);
b.setTdat(tdat);
b.setNem(nem);
b.setNsalar(nsalar);
b.setNpositio(npositio);
b.setNreaso(nreaso);
b.setEfdat(efdat);
b.setEtdat(etdat);
b.setEem(eem);
b.setEsalar(esalar);
b.setEpositio(epositio);
b.setEreaso(ereaso);
b.setDfdat(dfdat);
b.setDtdat(dtdat);
b.setDem(dem);
b.setDsalar(dsalar);
b.setDpositio(dpositio);
b.setDreaso(dreaso);
b.setUnam(unam);
b.setUaddres(uaddres);
b.setUbusines(ubusines);
b.setUyea(uyea);
b.setKnam(knam);
b.setKaddres(kaddres);
b.setKbusines(kbusines);
b.setKyea(kyea);
b.setNnam(nnam);
b.setNaddres(naddres);
b.setNbusines(nbusines);
b.setNyea(nyea);
int status=EmpDbs.save(b);
if(status>0)
{
out.print("<p>Record saved successfully!</p>");
req.getRequestDispatcher("SoftEmp.html").include(req, res);
}
else
{
out.println("Sorry! unable to save record");
}
out.close();
}
}
When I click on submit button SaveServlet is downloading like shown in the image but values are inserted and page is not refresh/rested:
Try using
<input type="submit" value="submit" name="submit"/>
I have my edit page and i would like to submit info to db once fields have been edited, it seems like something simple yet i cannot find a good example that pertains to my situation, i really don't know how to go about resolving this, this is the last part of my project. I would appreciate any suggestions Please.
This is my edit page code:
<title>
</title>
</head>
<body ng-app="app" ng-controller="decontroller" class="container">
<div id="banner" style="text-align:center; margin-left:auto; margin- right:auto; display:block;">
</div>
<h2></h2>
<h3>Personal Information:</h3>
<div id="validation-errors">
</div>
<form action="" method="post" accept-charset="utf-8" ng-submit="addEdit()">
<table class="table table-bordered">
<tbody>
<tr>
<td>ParticipantID</td>
<td>{{edit.Stlc_id}}</td>
</tr>
<tr>
<td>First Name:<br>
</td>
<td>{{edit.First_Name}}</td>
</tr>
<tr>
<td>Last Name:<br>
</td>
<td>{{edit.Last_Name}}</td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name ="Address" ng-model="edit.Address" ></td>
</tr>
<tr>
<td>Phone:</td>
<td><input size="20" name ="phone" ng-model="edit.Phone_Number" ></td>
</tr>
<tr>
<td>Assistive Devices:</td>
<td><input name ="AssistiveDevices" ng-model="edit.Assistive_Devices" ></td>
</tr>
<tr>
<td>Lanyard Code</td>
<td>
<input name ="Lanyard_Status" ng-model="edit.Lanyard_Status" /> </td>
</tr>
<tr>
<td>Comments</td>
<td>
<textarea cols="100" name="comments" ng-model="edit.Comments">.</textarea>
</td>
</tr>
<tr>
<td>Disenrolled</td>
<td><input name="disenrolled" ng-model="edit.Disenrolled" ></td>
</tr>
<tr>
<td>Deceased</td>
<td><input name="deceased" ng-model="edit.Deceased" ></td>
</tr>
</tbody>
</table>
</form>
<h3>Days in Center<br></h3>
<table class="table table-bordered">
<tbody>
<tr>
<td>Monday</td>
<td>Tuesday</td>
<td>Wednesday</td>
<td>Thursday</td>
<td>Friday</td>
</tr>
<tr>
<td><input name="Attendance_Monday" ng-model="edit.Attendance_Monday" ></td>
<td><input name="Attendance_Tuesday" ng-model="edit.Attendance_Tuesday" ></td>
<td><input name="Attendance_Wednesday" ng-model="edit.Attendance_Wednesday" ></td>
<td><input name="Attendance_Thursday" ng-model="edit.Attendance_Thursday" ></td>
<td><input name="Attendance_Friday" ng-model="edit.Attendance_Friday" > </td>
</tr>
</tbody>
</table>
<h3>Transportation Types</h3>
<table class="table table-bordered">
<tr>
<td>Type of Transportation</td>
<td>Approved For</td>
<td>Comments</td>
</tr>
<tr>
<td width="300px">Wheel Chair Van</td>
<td><input name="WheelChair_Van" ng-model="edit.WheelChair_Van"></td>
<td><textarea cols="100" name="WheelChair_Van comments" ng-model="edit.Comments" ></textarea></td>
</tr>
<tr>
<td width="300px">Transit Van 240</td>
<td><input name="TransitVan_240" ng-model="edit.TransitVan_240"></td>
<td><textarea cols="100" name="TransitVan_240 comments" ng-model="edit.Comments" ></textarea></td>
</tr>
<tr>
<td width="300px">Transit Van 360</td>
<td><input name="TransitVan_360" ng-model="edit.TransitVan_360"></td>
<td><textarea cols="100" name="TransitVan_360 comments" ng- model="edit.Comments"></textarea></td>
</tr>
<tr>
<td width="300px">Subaru Impreza</td>
<td><input name="Subaru_Impreza" ng-model="edit.Subaru_Impreza"></td>
<td><textarea cols="100" name="Subaru_Impreza comments" ng- model="edit.Comments"></textarea></td>
</tr>
</table>
<h3>Pick up and Drop Off Times</h3>
<br>
<table class="table table-bordered">
<tr>
<td width="300px">Pick Up Time:</td><td><input type="text" name="Pick_Up_Time" value=""></td>
</tr>
<tr>
<td width="300px">Drop off Time</td><td><input type="text" name="Drop_Off_Time" value=""></td>
</tr>
</table>
<br>
<h3>Personal Care Hours Pick Up/Drop Off</h3>
<table class="table table-bordered">
<tbody>
<tr>
<td>Monday</td>
<td>Tuesday</td>
<td>Wednesday</td>
<td>Thursday</td>
<td>Friday</td>
<td>Saturday</td>
<td>Sunday</td>
</tr>
<tr>
<td><input type="text" name="Monday_Pick_Up" ng-model="edit.Monday_Pick_Up" placeholder="Pick Up Time"></td>
<td><input type="text" name="Tuesday_Pick_Up" ng-model="edit.Tuesday_Pick_Up" placeholder="Pick Up Time"></td>
<td><input type="text" name="Wednesday_Pick_Up" ng-model="edit.Wednesday_Pick_Up" placeholder="Pick Up Time"></td>
<td><input type="text" name="Thursday_Pick_Up" ng-model="edit.Thursday_Pick_Up" placeholder="Pick Up Time"></td>
<td><input type="text" name="Friday_Pick_Up" ng-model="edit.Friday_Pick_Up" placeholder="Pick Up Time"></td>
<td><input type="text" name="Saturday_Pick_Up" ng-model="edit.Saturday_Pick_Up" placeholder="Pick Up Time"></td>
<td><input type="text" name="Sunday_Pick_Up" ng-model="edit.Sunday_Pick_Up" placeholder="Pick Up Time"></td>
</tr>
<tr>
<td><input type="text" name="Monday_Drop_Off" ng-model="edit.Monday_Drop_Off" placeholder="Drop Off Time"></td>
<td><input type="text" name="Tuesday_Drop_Off" ng-model="edit.Tuesday_Drop_Off" placeholder="Drop Off Time"></td>
<td><input type="text" name="Wednesday_Drop_Off" ng-model="edit.Wednesday_Drop_Off" placeholder="Drop Off Time"></td>
<td><input type="text" name="Thursday_Drop_Off" ng-model="edit.Thursday_Drop_Off" placeholder="Drop Off Time"></td>
<td><input type="text" name="Friday_Drop_Off" ng-model="edit.Friday_Drop_Off" placeholder="Drop Off Time"></td>
<td><input type="text" name="Saturday_Drop_Off" ng-model="edit.Saturday_Drop_Off" placeholder="Drop Off Time"></td>
<td><input type="text" name="Sunday_Drop_Off" ng-model="edit.Sunday_Drop_Off" placeholder="Drop Off Time"></td>
</tr>
</tbody>
</table>
<input type="submit" name="submit" ng-click="saveEdit()" />
</form>
<pre>{{edit | json}}</pre>
<a href="http://localhost:8080/stlc/index.php/transport/list_show_data/transport_vi ew">
<button type="button" class="btn btn-primary">Back</button>
</a>
<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"> </script>
<script type="text/javascript">
var app = angular.module('app',[]);
app.controller('decontroller', function($scope,$http){
$scope.edit=<?php echo json_encode($aggregate_data_view);?>;
$scope.saveEdit = function(){
console.log("hey i'm submitting!");
console.log($scope.edit);
$http.post('?php echo site_url("index.php/transport/saveData")?>', $scope.edit).
success(function(data){console.log(":)") }).
error(function(data){
console.log(":(")
});
};
});
</script>
</body>
</html>
Here is my Controller:
public function saveData()
{
}
it is empty now cause i really don't know what to do,nothing has worked.
In my page I have three button. First two create random AVL data on the page
the second one save that data to the db
HTML:
<button data-ng-click="createAvl()">Create Avl</button>
<button data-ng-click="create1000Avl()">Create 1000 Avl</button>
<button data-ng-click="saveAvl()">Save Avl</button>
ngController:
app.controller("myCtrl", function ($scope, $http) {
$scope.avl = []; --here is where i save the avl
$scope.avl.push({
'tracker_avl_id': getRandomArbitrary(0, 1000000, 1),
'plate_num': getRandomArbitrary(0, 10000, 1),
'x_lat': getRandomArbitrary(-69, -66, 0),
'y_long': getRandomArbitrary(8, 10, 0),
'azimuth': getRandomArbitrary(0, 359, 1),
'engine_status': engine,
'gps_fix': gps_fix,
'event_time': getRandomDate()
});
$scope.saveAvl = function () {
$scope.ResponseDetails = 'Working...'; -- some variables to show progress
$scope.startDate = getDate();
$scope.startTime = new Date().getTime();
$scope.timeEllapsed = null;
-- use $.param jQuery function to serialize data from JSON
var data = $.param({
avl_list: $scope.avl
});
var config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
}
}
-- send the data to my webservice
$http.post('/AvlApi/putTrackerAVL', data, config)
.success(function (data, status, headers, config) {
var end = new Date().getTime() - $scope.startTime;
$scope.timeEllapsed = 'milliseconds passed: ' + end;
$scope.ResponseDetails = data;
})
.error(function (data, status, header, config) {
$scope.ResponseDetails = "Data: " + data +
"<hr />status: " + status +
"<hr />headers: " + header +
"<hr />config: " + config;
});
};
My Webservice is in IIS using C#
[HttpPost]
public JsonResult putTrackerAVL(List<avl> avl_list)
{
try
{
avl_list.ForEach(car => db.avl.Add(car));
db.SaveChanges();
}
catch (Exception ex)
{
return Json(new { status = "Fail", message = ex.InnerException });
}
return Json(new { status = "Success" });
}