MinnRick
asked on
Can jQuery Parse an HTML Table and Load Its Contents To a Database?
Hello experts. I have a simple ASP.NET page that renders (for purposes of this discussion) a simple table:
<table>
<tr>
<td class='fieldA'>1</td><td class='fieldB'>2</td><td class='fieldC'>3</td>
</tr>
<tr>
<td class='fieldA'>3</td><td class='fieldB'>6</td><td class='fieldC'>9</td>
</tr>
<tr>
<td class='fieldA'>5</td><td class='fieldB'>10</td><td class='fieldC'>15</td>
</tr>
</table>
I'd like to know if it is possible (and if so, how) to use jQuery to do two things:
1. Read/parse the contents of the cells, and
2. Take the parsed data and insert (or trigger an event which inserts) it into a SQL Server database table.
Assume a button click to initiate the process. Thanks much all..
<table>
<tr>
<td class='fieldA'>1</td><td class='fieldB'>2</td><td class='fieldC'>3</td>
</tr>
<tr>
<td class='fieldA'>3</td><td class='fieldB'>6</td><td class='fieldC'>9</td>
</tr>
<tr>
<td class='fieldA'>5</td><td class='fieldB'>10</td><td class='fieldC'>15</td>
</tr>
</table>
I'd like to know if it is possible (and if so, how) to use jQuery to do two things:
1. Read/parse the contents of the cells, and
2. Take the parsed data and insert (or trigger an event which inserts) it into a SQL Server database table.
Assume a button click to initiate the process. Thanks much all..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yes, there are many jquery plugins available for table parsing through jquery
http://www.webdesignbooth.com/15-great-jquery-plugins-for-better-table-manipulation/
by the way, you do not need a specific api's for table elements parsing. You can simply treat it as a html element and give a selector to the jquery
http://www.learningjquery.com/2006/11/how-to-get-anything-you-want-part-1
http://www.webdesignbooth.com/15-great-jquery-plugins-for-better-table-manipulation/
by the way, you do not need a specific api's for table elements parsing. You can simply treat it as a html element and give a selector to the jquery
http://www.learningjquery.com/2006/11/how-to-get-anything-you-want-part-1
ASKER
Dal -
Thanks for the parsing code - just what I was looking for for #1. Would you be able to provide some additional sample code that performs the last bit (send using AJAX..)? That piece of the process has always been the most obscure to me. Much appreciated..
Thanks for the parsing code - just what I was looking for for #1. Would you be able to provide some additional sample code that performs the last bit (send using AJAX..)? That piece of the process has always been the most obscure to me. Much appreciated..
if you simply want to submit the comma separated values (in a get request) to the server,
makeAjaxCall(url, escape(insert_data.join(", ")));
function makeAjaxCall(url, values)
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.X MLHTTP");
}
xmlhttp.onreadystatechange =function( )
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("m yDiv").inn erHTML=xml http.respo nseText;
}
}
xmlhttp.open("GET", url + "?values=" + values ,true);
xmlhttp.send();
}
makeAjaxCall(url, escape(insert_data.join(",
function makeAjaxCall(url, values)
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.X
}
xmlhttp.onreadystatechange
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("m
}
}
xmlhttp.open("GET", url + "?values=" + values ,true);
xmlhttp.send();
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks all - very helpful.
ASKER
Fully and clearly answered my two questions. Excellent assistance.
Note: jQuery is just a JavaScript framework. It can be done using normal JavaScript also.
1. Read/parse the contents of the cells
- Quite easy to do this. Create a statement that loops through all cells in the table, and serialize the data to whatever format your database stores it in. Use an onclick event on a button to perform this action.
2. Take the parsed data and insert (or trigger an event which inserts) it into a SQL Server database table.
- You can use a normal form submit to post the data to a backend page/script which does this. You may use hidden fields to pass the generated content from #1 to the server.