how to create a dynamic table in a webpage - html

I am trying to learn how to create webpage with a "dynamic" table inside. I have zero experience in creating webpages but still I was able to construct the shape of the table by means of the use of html (the image below is a very basic in a very basic version):
For summarizing, let's say I want that the web page allows you to introduce A, B, ... J manually using the keyboard as input and it should show you automatically the sum of all of the rows in the column K. I have no idea what technology/code should I use... javascript? PHP? I have good skills in python, could it help somehow? I would like to do it as fast as possible.
Thanks in advance

You can do this by manually creating the table and adding Event Listeners that listen to changes in the table inputs. You can then use the values of the table inputs to calculate the output and display it in the K column:
const inputs = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'];
// Get the output column
const output = document.getElementById('output');
// Calculate the total from all inputs
function calculate() {
// Start with zero
let total = 0;
// For each element, get the value and add it to total.
inputElements.forEach(inputElement => {
// If the element does not have a value, we just add 0 (this is what the "|| 0" is for)
total += Number(inputElement.value) || 0;
});
// Display the total in the output column
output.innerHTML = total;
}
// Get all the input columns
const inputElements = inputs.map(input => document.getElementById(`input-${input}`));
// Add Event Listeners to all the input columns
inputElements.forEach(inputElement => inputElement.addEventListener('change', calculate));
table input {
width: 45px;
}
<table id="container">
<thead>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>E</th>
<th>F</th>
<th>G</th>
<th>H</th>
<th>I</th>
<th>J</th>
<th>K</th>
</tr>
</thead>
<tbody>
<tr>
<td><input id="input-A" type="number" /></td>
<td><input id="input-B" type="number" /></td>
<td><input id="input-C" type="number" /></td>
<td><input id="input-D" type="number" /></td>
<td><input id="input-E" type="number" /></td>
<td><input id="input-F" type="number" /></td>
<td><input id="input-G" type="number" /></td>
<td><input id="input-H" type="number" /></td>
<td><input id="input-I" type="number" /></td>
<td><input id="input-J" type="number" /></td>
<td id="output"></td>
</tr>
</tbody>
</table>

Related

Current input value does not get passed to event handler in AngularJS

<tr ng-repeat="item in groups">
<td hidden><input type="hidden" value="{{item.id_group}}" /></td>
<td><input type="text" value="{{item.description}}" class="form-control" /></td>
<td>
Edit |
Delete
</td>
</tr>
So this code was supposed to show values in a table and when the user changes anything in the description and click on Edit, it should POST the new value to the server. Instead it's posting the old value, I need some help please to identify why this is happening.
Try using Ng-model
<tr ng-repeat="item in groups track by $index">
<td hidden><input type="hidden" ng-model="groups[$index].id_group" /></td>
<td><input type="text" ng-model="groups[$index].description" class="form-control" /></td>
<td>
Edit |
Delete
</td>
</tr>

HTML code for button click limitation

This is my code, and it's for SMS spoofing service. I need a system, which will control how many SMS messages can user send. For example every user has 5 messages to send, and all above 5 is forbiden.
I need you to help me to make a simple HTML code, which will show me the message like "It's enough" when they click submit button more then 5 times.
<tr>
<td>Password:</td>
<td><input type="password" name="pass" placeholder="toro"></td>
</tr>
<tr>
<td>To: </td>
<td><input type="text" name="to" size="30" placeholder="+38164,+38162 etc..." /></td>
</tr>
<tr>
<td>From: </td>
<td><input type="text" name="from" size="30" placeholder="Facebook, 911 etc... " /></td>
<tr>
<td>Message: </td>
<td><textarea name="message"></textarea></td>
</tr>
<tr>
<td><input type="submit" name="submit" /></td>
<tr>
Not possible with HTML. Can be done with jquery or javascript as below. But my suggestion would be to have a counter in server side. Because, if the page is refreshed, the counter would be reset and again SMS would be able to be sent for another 5 more times and the cycle continues.. This is not proper restriction to have only client side check.
<script>
var clickcount = 0;
var maxlimit = 5;
$(document).ready(function() {
$("#btnSubmit").click(function(){
if (clickcount >=maxlimit)
{
alert ("Sorry, You have used your max limit of "+maxlimit+" sms for the day ");
}
else if ($("#formID").valid())
{
clickcount++;
$("#formID").submit();
}
});
});
</script>

How to read multiple textboxes and insert into database?

How can I update multiple textbox in database using ASP.NET Web Pages(razor syntax). I want to edit emails of students. And the number of rows containing textbox varies or dynamic. Somebody help.
<tr>
<th>Name</th>
<th>Email</th>
</tr>
<tr>
<td>Jason</td>
<td><input type="text" name="txtbox1"></td>
</tr>
<tr>
<td>Kripkee</td>
<td><input type="text" name="txtbox2"></td>
</tr>
<tr>
<td>Kane</td>
<td><input type="text" name="txtbox3"></td>
</tr>
<tr>
<td>Michael</td>
<td><input type="text" name="txtbox4"></td>
</tr>
<tr>
<td><input type="submit" value="Update"></td>
</tr>
</table>
</form>
You can try this:
Assuming you have a view model providing a list of students.
View -
#foreach (var student in Model.Students)
{
<tr>
<td>#student.Name</td>
<td><input name="studentEmails[#student.Id]"/></td>
</tr>
}
Controller -
public ActionResult UpdateEmails(IDictionary<int, string> studentEmails)

ColdFusion/SQL : ELEMENT XXX IS NOT DEFINED IN FORM

I have a form on my cfm page, that is to insert a new row into an SQL datasource. The cfm page hosts the form - and when submitted the -action.cfm page inserts the data and relocates the user to a page listing the new information. The problem I am getting is when I submit the form I am getting:: Element NEWUSERID is undefined in FORM.
the form on the insertScores.cfm page:
<form method="post" action="insertScores-action.cfm">
<table>
<tr>
<td>User ID</td>
<td><input name="newUserID" type = "text" size = "50"></td>
</tr>
<tr>
<td>First Name</td>
<td><input name="newFirstName" type="text" size="50"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input name="newLastName" type="text" size="50"></td>
</tr>
<tr>
<td>Email</td>
<td><input name="newEmailAdd" type="text" size="50"></td>
</tr>
<tr>
<td>Score</td>
<td><input name="newScore" type="text" size="50"></td>
</tr>
<tr>
<td>Pass Date</td>
<td><input name="newPassDate" type="text" size="50" value="dd/mm/yyyy"></td>
</tr>
<tr>
<td>Level</td>
<td><input name="newLevel" type="text" size="50"></td>
</tr>
<tr>
<td><input type="submit" value="Insert Scores"></td>
</tr>
</table>
The insertScores-action.cfm page
<cfquery name="insertScores" datasource="staffwrite">
INSERT INTO protinfo_scores (
userID
, first_name
, last_name
, email
, curr_score
, curr_score_date
, level
)
VALUES (
'#form.newUserID#'
, '#form.newFirstName#'
, '#form.newLastName#'
, '#form.newEmailAdd#'
, '#form.newScore#'
, '#form.newPassDate#'
, '#form.newLevel#'
)
</cfquery>
<cfquery name = "queryScore" datasource="staff">
SELECT userid, level
FROM protinfo_scores
WHERE userid LIKE '#form.newUserid#'
AND level = '#form.newLevel#'
</cfquery>
<cflocation URL="newScore.cfm?userid=#url.userid#&level=#url.level#">
newScore.cfm
<center>
<h2>Your ammendments have been made.</h2><hr>
</center>
<cfquery name = "queryScore" datasource="staff">
SELECT first_name, last_name, email, curr_score, curr_score_date, userid, level
FROM protinfo_scores
WHERE userid LIKE '#URL.userid#'
AND level = #URL.level#
</cfquery>
<cfoutput query="queryScore">
<table>
<tr bgcolor=beige>
<td>Name</td>
<td width="40">#queryScore.first_name# #queryScore.last_name#</td>
</tr>
<tr>
<td>Email</td>
<td width="40">#queryScore.email#</td>
</tr>
<tr bgcolor=beige>
<td>Username</td>
<td width="100">#queryScore.userid#</td>
</tr>
<tr>
<td>Level</td>
<td width="100">#queryScore.level#</td>
</tr>
<tr bgcolor=beige>
<td>Current Score</td>
<td width="40">#queryScore.curr_score#</td>
</tr>
<tr>
<td>Date Passed</td>
<td width="40">#queryScore.curr_Score_date#</td>
</tr>
</table>
</cfoutput>
and when submitted the -action.cfm page inserts the data and relocates
the user to a page listing the new information
I can't tell from the wording if this is the desired effect or the actual effect. If it is the actual effect, your error is occurring on newScore.cfm which would make sense because the data in the form scope does not persist after the cflocation and you're trying to use form.newUserID after the output table..
If this is the desired effect and NOT the actual effect...
As Dan said, you're looking at two different files. You show code from insertScores-application.cfm but your action is pointing to insertScores-action.cfm.
The code in your question is not exact copy/paste or even all of your code (you're missing a </form> tag to say the least). In this case I'd suggest showing us the actual code AND the error message. Many times I see element XXX is undefined in FROM (notice it says FROM and not FORM, something easy to miss when you're staring at the same thing for hours). The error message will tell you what line number the error is on. Is the line number actually among the code you posted?
I changed my <form></form> to a <cfform></cfform> and all now appears to work swimmingly. I was under the impression this would not make any difference but clearly it does.

HTML: Is it possible to have a FORM tag in each TABLE ROW in a XHTML valid way?

I can best describe this as follows:
I want this (entire table in editmode and save button in every row).
<table>
<tr>
<td>Id</td>
<td>Name</td>
<td>Description</td>
<td> </td>
</tr>
<tr>
<td><input type="hidden" name="id" value="1" /></td>
<td><input type="text" name="name" value="Name" /></td>
<td><input type="text" name="description" value="Description" /></td>
<td><input type="submit" value="Save" /></td>
</tr>
<tr>
<td><input type="hidden" name="id" value="2" /></td>
<td><input type="text" name="name" value="Name2" /></td>
<td><input type="text" name="description" value="Description2" /></td>
<td><input type="submit" value="Save" /></td>
</tr>
<!-- and more rows here ... -->
</table>
Where should I put the <form> tags?
It's worth mentioning that this is possible in HTML5, using the "form" attribute for input elements:
<table>
<tr>
<td>Id</td>
<td>Name</td>
<td>Description</td>
<td> </td>
</tr>
<tr>
<td><form id="form1"><input type="hidden" name="id" value="1" /></form></td>
<td><input form="form1" type="text" name="name" value="Name" /></td>
<td><input form="form1" type="text" name="description" value="Description" /></td>
<td><input form="form1" type="submit" value="Save" /></td>
</tr>
<tr>
<td><form id="form2"><input type="hidden" name="id" value="1" /></form></td>
<td><input form="form2" type="text" name="name" value="Name" /></td>
<td><input form="form2" type="text" name="description" value="Description" /></td>
<td><input form="form2" type="submit" value="Save" /></td>
</tr>
</table>
While clean in its lack of JS and use of original elements, unfortunately this isn't working in IE10.
I had a similar question and this answer in question HTML: table of forms? solved it for me. (Not sure if it is XHTML, but it works in an HTML5 browser.)
You can use css to give table layout to other elements.
.table { display: table; }
.table>* { display: table-row; }
.table>*>* { display: table-cell; }
Then you use the following valid html.
<div class="table">
<form>
<div>snake<input type="hidden" name="cartitem" value="55"></div>
<div><input name="count" value="4" /></div>
</form>
</div>
You can't. Your only option is to divide this into multiple tables and put the form tag outside of it. You could end up nesting your tables, but this is not recommended:
<table>
<tr><td><form>
<table><tr><td>id</td><td>name</td>...</tr></table>
</form></td></tr>
</table>
I would remove the tables entirely and replace it with styled html elements like divs and spans.
I wrote the below over ten years ago, when the world was a different place. These days I know of many ways to crack this particular nut, but a quick and dirty solution that will validate is to do much the same but use CSS tables for layout, not a regular HTML table.
I'd say you can, although it doesn't validate and Firefox will re-arrange the code (so what you see in 'View generated source' when using Web Developer may well surprise). I'm no expert, but putting
<form action="someexecpage.php" method="post">
just ahead of the
<tr>
and then using
</tr></form>
at the end of the row certainly gives the functionality (tested in Firefox, Chrome and IE7-9). Working for me, even if the number of validation errors it produced was a new personal best/worst! No problems seen as a consequence, and I have a fairly heavily styled table. I guess you may have a dynamically produced table, as I do, which is why parsing the table rows is a bit non-obvious for us mortals. So basically, open the form at the beginning of the row and close it just after the end of the row.
The answer of #wmantly is basicly 'the same' as I would go for at this moment.
Don't use <form> tags at all and prevent 'inappropiate' tag nesting.
Use javascript (in this case jQuery) to do the posting of the data, mostly you will do it with javascript, because only one row had to be updated and feedback must be given without refreshing the whole page (if refreshing the whole page, it's no use to go through all these trobules to only post a single row).
I attach a click handler to a 'update' anchor at each row, that will trigger the collection and 'submit' of the fields on the same row. With an optional data-action attribute on the anchor tag the target url of the POST can be specified.
Example html
<table>
<tbody>
<tr>
<td><input type="hidden" name="id" value="row1"/><input name="textfield" type="text" value="input1" /></td>
<td><select name="selectfield">
<option selected value="select1-option1">select1-option1</option>
<option value="select1-option2">select1-option2</option>
<option value="select1-option3">select1-option3</option>
</select></td>
<td><a class="submit" href="#" data-action="/exampleurl">Update</a></td>
</tr>
<tr>
<td><input type="hidden" name="id" value="row2"/><input name="textfield" type="text" value="input2" /></td>
<td><select name="selectfield">
<option selected value="select2-option1">select2-option1</option>
<option value="select2-option2">select2-option2</option>
<option value="select2-option3">select2-option3</option>
</select></td>
<td><a class="submit" href="#" data-action="/different-url">Update</a></td>
</tr>
<tr>
<td><input type="hidden" name="id" value="row3"/><input name="textfield" type="text" value="input3" /></td>
<td><select name="selectfield">
<option selected value="select3-option1">select3-option1</option>
<option value="select3-option2">select3-option2</option>
<option value="select3-option3">select3-option3</option>
</select></td>
<td><a class="submit" href="#">Update</a></td>
</tr>
</tbody>
</table>
Example script
$(document).ready(function(){
$(".submit").on("click", function(event){
event.preventDefault();
var url = ($(this).data("action") === "undefined" ? "/" : $(this).data("action"));
var row = $(this).parents("tr").first();
var data = row.find("input, select, radio").serialize();
$.post(url, data, function(result){ console.log(result); });
});
});
A JSFIddle
You just have to put the <form ... > tag before the <table> tag and the </form> at the end.
Hopte it helps.
In fact I have the problem with a form on each row of a table, with javascript (actually jquery) :
like Lothre1 said, "some browsers in the process of rendering will close form tag right after the declaration leaving inputs outside of the element"
which makes my input fields OUTSIDE the form, therefore I can't access the children of my form through the DOM with JAVASCRIPT..
typically, the following JQUERY code won't work :
$('#id_form :input').each(function(){/*action*/});
// this is supposed to select all inputS
// within the form that has an id ='id_form'
BUT the above exemple doesn't work with the rendered HTML :
<table>
<form id="id_form"></form>
<tr id="tr_id">
<td><input type="text"/></td>
<td><input type="submit"/></td>
</tr>
</table>
I'm still looking for a clean solution (though using the TR 'id' parameter to walk the DOM would fix this specific problem)
dirty solution would be (for jquery):
$('#tr_id :input').each(function(){/*action*/});
// this will select all the inputS
// fields within the TR with the id='tr_id'
the above exemple will work, but it's not really "clean", because it refers to the TR instead of the FORM, AND it requires AJAX ...
EDIT : complete process with jquery/ajax would be :
//init data string
// the dummy init value (1=1)is just here
// to avoid dealing with trailing &
// and should not be implemented
// (though it works)
var data_str = '1=1';
// for each input in the TR
$('#tr_id :input').each(function(){
//retrieve field name and value from the DOM
var field = $(this).attr('name');
var value = $(this).val();
//iterate the string to pass the datas
// so in the end it will render s/g like
// "1=1&field1_name=value1&field2_name=value2"...
data_str += '&' + field + '=' + value;
});
//Sendind fields datawith ajax
// to be treated
$.ajax({
type:"POST",
url: "target_for_the_form_treatment",
data:data_string,
success:function(msg){
/*actions on success of the request*/
});
});
this way, the "target_for_the_form_treatment" should receive POST data as if a form was sent to him (appart from the post[1] = 1, but to implement this solution i would recommand dealing with the trailing '&' of the data_str instead).
still I don't like this solution, but I'm forced to use TABLE structure because of the dataTables jquery plugin...
Im late to the party, but this worked great for me and the code should explain itself;
<script type="text/javascript">
function formAJAX(btn){
var $form = $(btn).closest('[action]');
var str = $form.find('[name]').serialize();
$.post($form.attr('action'), str, function(data){
//do stuff
});
}
<script>
HTML:
<tr action="scriptURL.php">
<td>
Field 1:<input type="text" name="field1"/>
</td>
<td>
Field 2:<input type="text" name="field2" />
</td>
<td><button type="button" onclick="formAJAX(this)">Update</button></td>
</tr>
If you try to add a form warping a tr element like this
<table>
<form>
<tr>
<td><input type="text"/></td>
<td><input type="submit"/></td>
</tr>
</form>
</table>
some browsers in the process of rendering will close form tag right after the declaration leaving inputs outside of the element
something like this
<table>
<form></form>
<tr>
<td><input type="text"/></td>
<td><input type="submit"/></td>
</tr>
</table>
This issue is still valid for warping multiple table cells
As stereoscott said above, nesting tables are a possible solution which is not recommended.
Avoid using tables.
<table >
<thead >
<tr>
<th>No</th><th>ID</th><th>Name</th><th>Ip</th><th>Save</th>
</tr>
</thead>
<tbody id="table_data">
<tr>
<td>
<form method="POST" autocomplete="off" id="myForm_207" action="save.php">
<input type="hidden" name="pvm" value="207">
<input type="hidden" name="customer_records_id" value="2">
<input type="hidden" name="name_207" id="name_207" value="BURÇİN MERYEM ONUK">
<input type="hidden" name="ip_207" id="ip_207" value="89.19.24.118">
</form>
1
</td>
<td>
207
</td>
<td>
<input type="text" id="nameg_207" value="BURÇİN MERYEM ONUK">
</td>
<td>
<input type="text" id="ipg_207" value="89.19.24.118">
</td>
<td>
<button type="button" name="Kaydet_207" class="searchButton" onclick="postData('myForm_207','207')">SAVE</button>
</td>
</tr>
<tr>
<td>
<form method="POST" autocomplete="off" id="myForm_209" action="save.php">
<input type="hidden" name="pvm" value="209">
<input type="hidden" name="customer_records_id" value="2">
<input type="hidden" name="name_209" id="name_209" value="BALA BAŞAK KAN">
<input type="hidden" name="ip_209" id="ip_209" value="217.17.159.22">
</form>
2
</td>
<td>
209
</td>
<td>
<input type="text" id="nameg_209" value="BALA BAŞAK KAN">
</td>
<td>
<input type="text" id="ipg_209" value="217.17.159.22">
</td>
<td>
<button type="button" name="Kaydet_209" class="searchButton" onclick="postData('myForm_209','209')">SAVE</button>
</td>
</tr>
</tbody>
</table>
<script>
function postData(formId,keyy){
//alert(document.getElementById(formId).length);
//alert(document.getElementById('name_'+keyy).value);
document.getElementById('name_'+keyy).value=document.getElementById('nameg_'+keyy).value;
document.getElementById('ip_'+keyy).value=document.getElementById('ipg_'+keyy).value;
//alert(document.getElementById('name_'+keyy).value);
document.getElementById(formId).submit();
}
</script>