Jump to content

Dynamic Fields js/php to MySql need to submit dynamically to the database


jungle

Recommended Posts

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);
    var chckbx = document.getElementById("checkb"+col);
    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);" /> 
 <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>

Because you only tell it to insert 2 values:

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

That should probably be in your loop where you are retrieving $ced and $erg from $_POST.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.