I have a script which add new table rows by javascript: The script which i have used can be found here:
http://viralpatel.net/blogs/dynamically-add-remove-rows-in-html-table-using-javascript/
The code looks like this:
<SCRIPT language="javascript">
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
if(rowCount >= 5) {
alert("You can have max 5 rows. :(");
break;
}
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
//alert(newcell.childNodes);
switch(newcell.childNodes[0].type) {
case "text":
newcell.childNodes[0].value = "";
break;
case "checkbox":
newcell.childNodes[0].checked = false;
break;
}
}
}
function deleteRow(tableID) {
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("You must have minimum 1 response");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
}catch(e) {
alert(e);
}
}
</SCRIPT>
And i'm inserting it properly in the database with PHP & MYSQL. But my problem starts when i want to edit the post. This is my code:
<INPUT type="button" value="Add new answer" class="button" onclick="addRow('dataTable')" />
<INPUT type="button" value="Remvoe answer" class="button" onclick="deleteRow('dataTable')" />
<TABLE id="dataTable" width="100%">
<?php
$statement = $dbConn->prepare("SELECT answer, id FROM poll_user_answers WHERE user_id =? AND qid=?");
$statement -> execute(array($user_id,$view_question_pending['id']));
while($show_answers_pending = $statement->fetch(PDO::FETCH_ASSOC)) {
?>
<TR>
<TD><INPUT type="checkbox" name="chk"/></TD>
<TD><INPUT type="text" value="<?php echo $show_answers_pending['answer'];?> " name="chkApp[<?php echo $show_answers_pending['id'];?>]" placeholder="Napisi odgovor ovdje" style="border: 1px solid #c8c8c8; padding: 5px; width: 100%; height: 20px;"/></TD>
</TR>
<?php } ?>
And this is insertion of the stuff in the database:
if($_POST['yes'] == 'Promijenite pitanje') {
//$answer = $_POST['answer'];
//$clicks = '0';
$question = $_POST['poll_question'];
//Update the question
$statement = $dbConn->prepare("UPDATE poll_user_questions SET question = ? WHERE id =?");
$statement->execute(array($question, $view_question_pending['id']));
**//Insertion of answers into database**
foreach($_POST['chkApp'] as $id => $votes){
**//DELETE ALL STUFF FIRST**
$statement = $dbConn->prepare("DELETE FROM poll_user_answers WHERE id=?");
$statement -> execute(array($id));
**//INSERT NEW ANSWERS**
$statement = $dbConn->prepare("INSERT INTO poll_user_answers (user_id, qid, answer) VALUES (:user_id, :qid, :answer)");
$statement->execute(array(
'user_id' => $user_id,
'qid' => $view_question_pending['id'],
'answer' => $votes
));
}
//header('Location: '.selfURL());
}
When i mark the checkboxes and hit Delete button then it delete the rows but problem is it still saves all the values and i think it's because i've looped them with this code.
while($show_answers_pending = $statement->fetch(PDO::FETCH_ASSOC)) {
?>
<TR>
<TD><INPUT type="checkbox" name="chk"/></TD>
<TD><INPUT type="text" value="<?php echo $show_answers_pending['answer'];?> " name="chkApp[<?php echo $show_answers_pending['id'];?>]" placeholder="Napisi odgovor ovdje" style="border: 1px solid #c8c8c8; padding: 5px; width: 100%; height: 20px;"/></TD>
</TR>
<?php } ?>
How can i hit the delete button and save the new values in to the database for example if i first created 5 answers and i want to remove 2 and save the 3 into the dabase.. I really really hope you understood my question.