Dynamically Add/Remove rows in HTML table using Java. Script[ad name=”AD_INBETWEEN_POST”] A good web design involves the better user interaction and ability to fetch the data in a better way.
In this article we will create a user interface where user can add/delete multiple rows in a form using JavaScript. First check the user interface. For fetching user data, you may have to create a form wherein user can add multiple entries and submit them simultaneously. Thus for this you will need a way to add or remove fields dynamically into the HTML page. In my previous article, I had discussed about the way one can add dynamic form components into the web page. In this article we will create a user interface where user can add/delete multiple rows in a form using Java. Script. First check the user interface. In this example, we have created a table which will display our html components. On pressing Add Row, a dynamic row will be created and added in the table. And on selecting the checkbox and pressing Delete Row, the row will be removed. Following is the source. Code< HTML>. TITLE> Add/Remove dynamic rows in HTML table < /TITLE>. SCRIPT language="javascript">. Row(table. ID) {. Element. By. Id(table. ID). var row. Count = table. Row(row. Count). var cell. Cell(0). var element. Element("input"). Child(element. 1). Cell(1). cell. 2. HTML = row. Count + 1. Cell(2). var element. Element("input"). Child(element. 2). Row(table. ID) {. Element. By. Id(table. ID). var row. Count = table. Count; i++) {. var row = table. Nodes[0]. if(null != chkbox & & true == chkbox. Row(i). row. Count- -. SCRIPT>. < INPUT type="button" value="Add Row" onclick="add. Row('data. Table')" />. INPUT type="button" value="Delete Row" onclick="delete. Row('data. Table')" />. TABLE id="data. Table" width="3. TD> < INPUT type="checkbox" name="chk"/> < /TD>. TD> 1 < /TD>. TD> < INPUT type="text" /> < /TD>. TABLE>. For adding dynamic row in table, we have used insert. Row() method. This method will insert a row at position specified by the index arguement. Also for removing row, we have used delete. Row() method. Note that for inserting dynamic row, we have to created cells by using row. Cell() method. Check the online demo. Online Demo. Click here. Add/Remove rows from table having Drop Down List. What if my table has a selectbox or drop down list and I want to implement add/remove row functionality? A lot of people asked me this question (you can check comment section), so I thought to update this article and add one more case. Adding / Removing rows in a table having drop down list. Following is the code: < HTML>. TITLE> Add/Remove dynamic rows in HTML table < /TITLE>. SCRIPT language="javascript">. Row(table. ID) {. Element. By. Id(table. ID). var row. Count = table. Row(row. Count). var col. Count = table. rows[0]. Count; i++) {. var newcell = row. Cell(i). newcell. HTML = table. rows[0]. HTML. //alert(newcell. Nodes). switch(newcell. Nodes[0]. type) {. Nodes[0]. value = "". Nodes[0]. checked = false. Nodes[0]. selected. Index = 0. function delete. Row(table. ID) {. Element. By. Id(table. ID). var row. Count = table. Count; i++) {. var row = table. Nodes[0]. if(null != chkbox & & true == chkbox. Count < = 1) {. Cannot delete all the rows."). Row(i). row. Count- -. SCRIPT>. < INPUT type="button" value="Add Row" onclick="add. Row('data. Table')" />. INPUT type="button" value="Delete Row" onclick="delete. Row('data. Table')" />. TABLE id="data. Table" width="3. TD> < INPUT type="checkbox" name="chk"/> < /TD>. TD> < INPUT type="text" name="txt"/> < /TD>. SELECT name="country">. OPTION value="in"> India< /OPTION>. OPTION value="de"> Germany< /OPTION>. OPTION value="fr"> France< /OPTION>. OPTION value="us"> United States< /OPTION>. OPTION value="ch"> Switzerland< /OPTION>. SELECT>. < /TABLE>. Check the add. Row() method in above code. I have edited this piece of code from our previous example. The add. Row() method iterates through all the columns and copy the content of 1st column to new column. This way a new row is created which is exact copy of 1st row. Also the switch{} case in the code make sure that the value are not copied as it is. For example, if you have entered text “abc” in textbox of 1st row and you press Add Row button, the new row will be added and the textbox of this new row will have value “abc”. So to make sure the values are not copied, we have added the switch{} code. You may want to comment the switch{} code if the copying of the values in newly added row if desired to you. Online Demo. Click here. Getting Submitted Form Values in PHPThe main purpose of above code is to take input from user and persist it in database. Hence we may want to submit the above form and fetch its values in PHP to store the data in database. If you notice we have multiple input textboxes with the same name. So in order to get these values in PHP request parameter, we need to modify our HTML. We need to append [] at the end of name of each input boxes including select box. Thus our textbox definition: < INPUT type="text" name="txt">. Will change into: < INPUT type="text" name="txt[]">. Also the checkbox and select box definition change to: < INPUT type="checkbox" name="chk[]"/>. SELECT name="country[]">. OPTION value="in"> India< /OPTION>. OPTION value="de"> Germany< /OPTION>. OPTION value="fr"> France< /OPTION>. OPTION value="us"> United States< /OPTION>. OPTION value="ch"> Switzerland< /OPTION>. SELECT>. Once we have changed the HTML code, add a submit button at the end of the form and submit this form to Fetchrequest. Following will be the code to get the values from request in PHP and displaying them on screen: < ? POST['chk']. $txtbox = $_POST['txt']. POST['country']. foreach($txtbox as $a => $b). If you read this far, you should follow me on twitter here. SQL Tutorial - w. Last update on September 0. UTC/GMT +8 hours)Learn Structured Query Language. SQL Tutorial of w. SQL without any prior experience. Having said that, it by no means superficial. On the contrary, it offers all the material one needs to successfully build a database and write SQL queries ranging from a one liner like "SELECT * FROM table_name" to fairly non- trivial ones taking multiple tables in the account. At the outset, we need to tell you, this SQL Tutorial adheres to SQL: 2. ANSI. This is important because if you are learning something as important as SQL, there is no point learning if you don't know which version or standard you are studying. We have diligently added as many features as possible while creating this SQL Tutorial. There is Syntax, Query, Explanation of a query and pictorial presentation to help you understand concepts better. On top of these, we have hundreds of Exercises with an online editor, quizzes. So you may practice concepts and queries without leaving your browser. SQL Queries: Introduction. In June 1. 97. 0 Dr. E. F. Codd published the paper, "A Relational Model of Data for Large Shared Data Banks" in the Association of Computer Machinery (ACM) journal. Codd's model is now accepted as the definitive model for relational database management systems (RDBMS). Using Codd's model the language, Structured English Query Language (SEQUEL) was developed by IBM Corporation in San Jose Research Center. The language was first called SEQUEL but Official pronunciation of SQL is ESS QUE ELL. In 1. 97. 9 Oracle introduced the first commercially available implementation of SQL. Later other players join in the race. Today, SQL is accepted as the standard RDBMS language. Note: If you are not habituated with database management system your can learn fromhere. What is SQL? SQL stands for Structured Query Language and it is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database systems. It is used for managing data in relational database management system which stores data in the form of tables and relationship between data is also stored in the form of tables. SQL statements are used to retrieve and update data in a database. SQL works with database programs like DB2, My. SQL, Postgre. SQL, Oracle, SQLite, SQL Server, Sybase, MS Access and much more. There are many different versions of the SQL language, but to be in compliance with the ANSI standard, they support the major keyword such as SELECT, UPDATE, DELETE, INSERT, WHERE, and others. The following picture shows the communicating with an RDBMS using SQL. History of SQL Here is the year wise development history : 1. E. F. Codd publishes Definition of Relational Model. Initial version of SQL Implemented (D. Chamberlin)IBM experimental version: System R (1. SQLIBM commercial versions: SQL/DS and DB2 (the early 1. Oracle introduces commercial version before IBM's SQL/DSINGRES 1. Share. Base 1. 98. Data General (1. 98. Sybase (1. 98. 6)by 1. SQL products. SQL Standard Revisions SEQUEL/Original SQL - 1. SQL/8. 6 - Ratification and acceptance of a formal SQL standard by ANSI (American National Standards Institute) and ISO (International Standards Organization). SQL/9. 2 - Major revision (ISO 9. Entry Level SQL- 9. FIPS 1. 27- 2. SQL/9. Added regular expression matching, recursive queries (e. SQL/2. 00. 3 - Introduced XML- related features (SQL/XML), Window functions, Auto generation. SQL/2. 00. 6 - Lots of XML Support for XQuery, an XML- SQL interface standard. SQL/2. 00. 8 - Adds INSTEAD OF triggers, TRUNCATE statement. Constructs of SQL Here is list of the key elements of SQL along with a brief description: Queries : Ret rives data against some criteria. Statements : Controls transactions, program flow, connections, sessions, or diagnostics. Clauses : Components of Queries and Statements. Expressions : Combination of symbols and operators and a key part of the SQL statements. Predicates : Specifies conditions. Some Key terms of SQL 2. To know the key terms of SQL 2. SQL 9. 2 AND SQL 2. SQL features and statements. In SQL 9. 2, SQL statements are grouped into following categories: Data manipulation : The Data Manipulation Language (DML) is the subset of SQL which is used to add, update and delete data. Data definition : The Data Definition Language (DDL) is used to manage table and index structure. CREATE, ALTER, RENAME, DROP and TRUNCATE statements are to name a few data definition elements. Data control : The Data Control Language (DCL) is used to set permissions to users and groups of users whether they can access and manipulate data. Transaction : A transaction contains a number of SQL statements. After the transaction begins, all of the SQL statements are executed and at the end of the transaction, permanent changes are made in the associated tables. Procedure : Using a stored procedure, a method is created which contains source code for performing repetitive tasks. In SQL 2. 00. 3 statements are grouped into seven categories which are called classes. See the following table : Class Example SQL data statements. SELECT, INSERT, UPDATE, DELETESQL connection statements. CONNECT, DISCONNECTSQL schema statements. ALTER, CREATE, DROPSQL control statements. CALL, RETURNSQL diagnostic statements. GET DIAGNOSTICSSQL session statements SET CONSTRAINTSQL transaction statements. COMMIT, ROLLBACKPL- SQL, TSQL and PL/pg. SQL PL/SQL - Procedural Language/Structured Query Language ( PL/SQL) is Oracle Corporation's procedural extension language for SQL and the Oracle relational database. TSQL - Transact- SQL (T- SQL) is Microsoft's and Sybase's proprietary extension to SQL. PL/pg. SQL - Procedural Language/Postgre. SQL(PL/pg. SQL) is a procedural programming language supported by the Postgre. SQL. Database and Table Manipulation. Command Description CREATE DATABASE database_name Create a database. DROP DATABASE database_name Delete a database. CREATE TABLE "table_name" ("column_1" "column_1_data_type", "column_2" "column_2_data_type", .. Create a table in a database. ALTER TABLE table_name ADD column_name column_datatype Add columns in an existing table. ALTER TABLE table_name DDROP column_name column_datatype. Delete columns in an existing table. DROP TABLE table_name Delete a table. Data Types: Data Type. Description. CHARACTER(n)Character string, fixed length n. CHARACTER VARYING(n) or. VARCHAR(n)Variable length character string, maximum length n. BINARY(n)Fixed- length binary string, maximum length n. BOOLEANStores truth values - either TRUE or FALSE. BINARY VARYING(n) or. VARBINARY(n)Variable length binary string, maximum length n. INTEGER(p)Integer numerical, precision p. SMALLINTInteger numerical precision 5. INTEGERInteger numerical, precision 1. BIGINTInteger numerical, precision 1. DECIMAL(p, s)Exact numerical, precision p, scale s. NUMERIC(p, s)Exact numerical,precision p, scale s.(Same as DECIMAL ). FLOAT(p)Approximate numerical, mantissa precision p. REALApproximate numericalmantissa precision 7. FLOATApproximate numericalmantissa precision 1. DOUBLE PRECISIONApproximate numericalmantissa precision 1. DATETIMETIMESTAMPComposed of a number of integer fields, representing an absolute point in time, depending on sub- type. INTERVALComposed of a number of integer fields, representing a period of time, depending on the type of interval. COLLECTION (ARRAY, MULTISET)ARRAY(offered in SQL9. XMLStores XML data. It can be used wherever a SQL data type is allowed, such as a column of a table. Index Manipulation: Command Description CREATE INDEX index_name ON table_name (column_name_1, column_name_2, ..)Create a simple index. CREATE UNIQUE INDEX index_name ON table_name (column_name_1, column_name_2, ..) Create a unique index. DROP INDEX table_name. Drop a index. SQL Operators: Operators. Description. SQL Arithmetic Operator Arithmetic operators are addition(+), subtraction(- ), multiplication(*) and division(/). The + and - operators can also be used in date arithmetic. SQL Comparison Operator. A comparison (or relational) operator is a mathematical symbol which is used to compare two values. SQL Assignment operator. In SQL the assignment operator ( = ) assigns a value to a variable or of a column or field of a table. SQL Bitwise Operator The bitwise operators are & ( Bitwise AND ), | ( Bitwise OR ) and ^ ( Bitwise Exclusive OR or XOR ). The valid datatypes for bitwise operators are BINARY, BIT, INT, SMALLINT, TINYINT, and VARBINARY. SQL Logical Operator. The Logical operators are those that are true or false.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
November 2017
Categories |