Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
Looking through your code now, but why are you using raw javascript instead of jQuery, as you mention in your question, to act on the DOM? Also, are you deleting all rows before adding the new ones, including the good rows? Why not delete individual fields and run an update to update the old questions. That's confusing me –  Zarathuztra Feb 16 at 14:00
    
Hi, This is the script i've downloaded, I'm really total noob when it comes to javascript. the delete all rows, well that was what i tried to do just to insert new ones... having any ideas how to solve this thingy? Cheerz –  Mensur Feb 16 at 14:04
    
@Mensure Then where is the jQuery you're using? –  Zarathuztra Feb 16 at 14:05
    
@Zarathuztra i'm not... i have removed jquery stuff.. .sry –  Mensur Feb 16 at 14:09
    
@Mensure, please add it back in so that we can see if that's your issue. –  Zarathuztra Feb 16 at 14:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.