I can not get the values from the javascript add row to go dynamically as a row into MySql only the form values show up as the form below as one row. I made it as an array, but no such luck, I have tried this code around a multitude of ways. I don't know what I am doing wrong, kindly write out the correct way.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dynamic Fields js/php to MySql need to submit dynamically to the database</title>
<?php
require ('database.php'); 
?>
 <script type="text/javascript">
 var counter     =     1;
 var collector     =     "";

 function addfields(indx)
 {
     var tbl = document.getElementById('table_id');
     var newtr = document.createElement('tr');
     counter = counter + indx;

     newtr.setAttribute('id','tr'+counter);

     newtr.innerHTML = '<td><input type="checkbox" name="checkb'+counter+'" id="checkb'+counter+'" value="'+counter+'" onclick="checkme('+counter+')"></td><td><input type="text" name="text1[]"></td><td><textarea name="textarea1[]"></textarea></td>';

     tbl.appendChild(newtr);
 }

 function checkme(dx)
{
      collector += dx+",";
}

 function deletetherow(indx)
 {
     var col = collector.split(",");

     for (var i = 0; i < col.length; i++) 
     {
         var remvelem = document.getElementById('tr'+col[i]);
         var chckbx = document.getElementById("checkb"+col[i]);
         if(remvelem && chckbx.checked)
         {
             var tbl = document.getElementById('table_id');
            tbl.removeChild(remvelem);
        }
     }
 }
</script>
</head>
<body>
<form enctype="multipart/form-data" id="1" style="background-color:#ffffff;" action="<?php echo $_SERVER['PHP_SELF']; ?>"></form> 
<table id="table_id" >
  <tr id="tr1" class="trmain">
  <td>
  </td>
   <td>
   <input type="text" name="text1[]">
    </td>
      <td>
        <textarea name="textarea1[]"></textarea>
       </td>  
     </tr>
 </table>
<input type="button" value="Add" onClick="addfields(1);" />&nbsp;
<input type="button" value="Delete" onClick="deletetherow()" />
<input type="submit" value="Send" id="submit"  name="submit"/>

<?php
if(isset($_POST['submit'])) {
for ($i=0; $i < count($_POST['text1']); $i++ )
{
$ced = stripslashes($_POST['text1'][$i]);
$erg = stripslashes($_POST['textarea1'][$i]);
}


$bnt = mysql_query("INSERT INTO tablename (first, second) VALUES ('$ced', '$erg')")or die('Error: '. mysql_error() );
$result = mysql_query($bnt);
}

?>

</body>
</html>

Do you even get to the query part? If you put an echo statement before it, is it visible on the page?

Your javascript, addfields() should NEVER keep going if the variable indx is 1)not a number, 2)equal to 0, and 3)less than 0!

Line 21, you do not really create all input elements in the DOM but rather write it out to innerHTML!

The collector variable is never be cleared after a row is removed. Also, your script will do 1 more loop because the last value in split() is an empty string. If you really want to do something like this, simply collect all input tag name, go through each of the element and work with a tag with type "checkbox" which is being "checked" instead.

Line 70 & 71, I am not sure what you are trying to do here? You did NOT define the index for newly created text1 & textarea1 in line 21 (simply an empty []), and then you expect it to be an array and contains some values?

Again, Line 70 & 71, you keep replacing the newer value into the current one. Then in Line 75, you are attempting to save it into the database?

There are many serious errors in logic both in PHP and JavaScript and vulnerability in your script. Your script is easily being injected (SQL injection vulnerability) because you allow it! Your script is not properly create elemends in the DOM (because of innerHTML). And your script does not properly name elements, so that you could call them later.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.