Page 1 of 1

how to add values of the dynamically added rows into dtbase

Posted: Sun Jun 14, 2009 8:11 pm
by minie
hello everyone,
im suppose to create a form with a part where the user can add new rows when the "add" button is click. the values in the dynamically added rows must be added into the database. Following is my code..(simplified version)

(page1.php)

Code: Select all

 
<!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">
<script language="javascript" type="text/javascript">
 
function addRow()
{
          var tbl = document.getElementById('table1');
          var lastRow = tbl.rows.length;
          var iteration = lastRow - 1;
          var row = tbl.insertRow(lastRow);
          
          var firstCell = row.insertCell(0);
          var el = document.createElement('input');
          el.type = 'text';
          el.name = 'txtRow1' + iteration;
          el.id = 'txtRow1' + iteration;
          el.size = 20;
          el.maxlength = 20;
          firstCell.appendChild(el);
          
          var secondCell = row.insertCell(1);
          var el2 = document.createElement('input');
          el2.type = 'text';
          el2.name = 'txtRow1' + iteration;
          el2.id = 'txtRow1' + iteration;
          el2.size = 20;
          el2.maxlength = 20;
          secondCell.appendChild(el2);
          
          var thirdCell = row.insertCell(2);
          var el3 = document.createElement('input');
          el3.type = 'text';
          el3.name = 'txtRow1' + iteration;
          el3.id = 'txtRow1' + iteration;
          el3.size = 20;
          el3.maxlength = 20;
          thirdCell.appendChild(el3);
}
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title></head>
 
<body>
<form action="addDB.php" method="get">
<table width="40%" border="2" cellpadding="0" cellspacing="0" id="table1">
  <tr>
    <td><strong>Name</strong></td>
    <td><strong>Address</strong>&nbsp;</td>
    <td><strong>Contact Num</strong>&nbsp;</td>
  </tr>
  <tr>
    <td><input name="name" type="text" id="name" size="20" maxlength="20" /></td>
    <td><input name="address" type="text" id="address" size="20" maxlength="20" /></td>
    <td><input name="contactNum" type="text" id="contactNum" size="20" maxlength="0" /></td>
  </tr>
 
</table>
<input type="button" value="Add" onclick="addRow();" />
<input name="Submit" type="submit" value="Submit" />
</form>
</body>
</html>
 
 
addDB.php

Code: Select all

 
 
<?php
 
 
include "dbCon.php";
 
$Name=$_POST["name"];
$Address=$_POST["address"];
$ContactNum=$_POST["contactNum"];
 
$sql="INSERT INTO table1 (NAME, ADDRESS, CONTACT_NUM) VALUES ('$Name', '$Address', '$ContactNum')";
 
$result=mysql_query($sql);
 
?>
 
can anyone please help me out on how to modify this code so that the values which are entered in the textbox of the dynamically added rows into the database?
thanking you in advance...

Re: how to add values of the dynamically added rows into dtbase

Posted: Mon Jun 15, 2009 1:15 am
by omniuni
If your code does indeed work, then loop through your $_GET or $_POST array using a foreach($array as $key => $element){} and check the name of they key to see if it contains the text txtRow. If so, apply the function to add it to your database.

Does that help?