<!DOCTYPE html>
<html>
<body>
<p>Check form to approve and delete news feeds</p>
<form action="form_action.asp">
<table id="dataTable" width="350px" border="1" style="width:100%">
<tr>
<TD><INPUT type="checkbox" name="chk[]" onchange/>Times of india</TD>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<TD><INPUT type="checkbox" name="chk"/>Hindu</TD>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<TD><INPUT type="checkbox" name="chk"/>BBC</TD>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
<br>
<input type="button" onclick="myFunction()" value="Approve">
<input type="button" onclick="myFun()" value="delete">
<br><br>
<input type="text" id="order" size="50">
<input type="submit" value="Submit">
<table>
</form>
<script>
function myFunction() {
var coffee = document.forms[0];
var txt = "";
var i;
for (i = 0; i < coffee.length; i++) {
if (coffee[i].checked) {
txt = txt + coffee[i].value + " ";
}
}
document.getElementById("order").value = "You ordered a coffee with: " + txt;
}
function myFun() {
var coff = document.forms[0];
var txt1 = "";
var j;
for (j= 0; j < coff.length; j++) {
if (coffee[j].checked) {
txt1 = txt1 + coffee[j].value + " ";
}
}
document.getElementById("order").value = "You ordered a coff with: " + txt1;
}
</script>
</body>
</html>
Expected form should be like below captured like:
Checked and approved row goes to postgres database using PHP code like below is the code form getting expected.
<!DOCTYPE html>
<html>
<head>
<script>
function approve(tableID) {
<?php
//connecting to database
$db = pg_connect("host= port=5432 dbname=NEWS user=postgres password=");
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
//setting default time zone
date_default_timezone_set('Asia/Kolkata');
$chkbox = $_POST['chk'];
foreach($chkbox as $a => $b)
echo "$chkbox[$a] <br />";
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
//inserting news into database table
$query = "INSERT INTO ndem_news_table(news_link, news_title, news_date, news_source, news_time) VALUES('".$news_link."','".$news_title."','".$news_date."','".$dk."','".$dt."')";
$result = pg_query($query);
if (!$result) {
//$errormessage = pg_last_error();
echo "Error with query: ";
//exit();
}
else
{
echo "Row Inserted Successfully <br>";
}
}
//php code to insert into data base
}
function delete(tableID)
{
echo'i am inside of delete';
try {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++)
{
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked)
{
if(rowCount <= 1)
{
alert("Cannot delete all the rows.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
}
catch(e) {
alert(e);
}
}
</script>
</head>
<body>
< button onclick="approve()">Approve</button>
<button onclick="delete()">delete</button>
<table id="dataTable" width="350px" border="1" style="width:100%">
<tr>
<TD><INPUT type="checkbox" name="chk[]" onchange/></TD>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<TD><INPUT type="checkbox" name="chk"/></TD>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<TD><INPUT type="checkbox" name="chk"/></TD>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
</body>
</html>
My table here is hard coded one I want it to be dynamically generated one that table data I am getting from XML feeds of news channels.