0
 dynamic table insertion code is as follows: 

new1.php

<?php
session_start();
?>
<!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">
var i=0;
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 = 'name' + i;
          el.id = 'name' + i;
          el.size = 20;
          el.maxlength = 20;
          firstCell.appendChild(el);

          var secondCell = row.insertCell(1);
          var el2 = document.createElement('input');
          el2.type = 'text';
          el2.name = 'address' + i;
          el2.id = 'address' + i;
          el2.size = 20;
          el2.maxlength = 20;
          secondCell.appendChild(el2);

          var thirdCell = row.insertCell(2);
          var el3 = document.createElement('input');
          el3.type = 'text';
          el3.name = 'contactNum' + i;
          el3.id = 'contactNum' + i;
          el3.size = 20;
          el3.maxlength = 20;
          thirdCell.appendChild(el3);
          alert(i);
          i++;
          frm.h.value=i;
          alert(i);

}
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title></head>

<body>
<form action="submit.php" method="post" name="frm" id="frm">
<table width="40%" border="2" cellpadding="0" cellspacing="0" id="table1">
  <tr>
    <td><strong>Name</strong></td>
    <td><strong>Address</strong> </td>
    <td><strong>Contact Num</strong> </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" />
<label>
<input name="h" type="hidden" id="h" value="0" />
</label>
</form>
</body>
</html>




And insertion code is as:

the data is not properly inserting.it is reaching upto database but showing only last row inseted.but in submit.php dynamic values are inserting but showing only last value and other are blank................................................................................................................................

submit.php


<?php
include "connect.php";
$num =  $_POST['h'];

for($i=0;$i<=$num;$i++)
{
if(isset($_REQUEST["name$i"]))
{
$name =  $_REQUEST["name$i"];
}
if(isset($_REQUEST["address$i"])){
$address = $_REQUEST["address$i"];
}else{
$address= 'address';
} 
if(isset($_REQUEST["contactNum$i"])){
$contactNum = $_REQUEST["contactNum$i"];}else{$contactNum = 00;
} //for error controling
$strQuery = "Insert Into emp1 (`NAME`,`ADDRESS`,`CONTACT_NUM`) Values('$name','$address',$contactNum)"; //for error controling
$result=mysql_query($strQuery) or die(mysql_error());
}
?>
1
  • 1. add print_r($_POST); before for loop and let us know what is the result 2. use $_POST array instead of $_REQUEST. Commented Apr 5, 2016 at 14:01

1 Answer 1

1

new1.php

<?php
session_start();
?>
<!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">
var i=0;
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 = 'name[]';
          el.id = 'name' + i;
          el.size = 20;
          el.maxlength = 20;
          firstCell.appendChild(el);

          var secondCell = row.insertCell(1);
          var el2 = document.createElement('input');
          el2.type = 'text';
          el2.name = 'address[]';
          el2.id = 'address' + i;
          el2.size = 20;
          el2.maxlength = 20;
          secondCell.appendChild(el2);

          var thirdCell = row.insertCell(2);
          var el3 = document.createElement('input');
          el3.type = 'text';
          el3.name = 'contactNum[]';
          el3.id = 'contactNum' + i;
          el3.size = 20;
          el3.maxlength = 20;
          thirdCell.appendChild(el3);
          alert(i);
          i++;
          frm.h.value=i;
          alert(i);

}
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title></head>

<body>
<form action="submit.php" method="post" name="frm" id="frm">
<table width="40%" border="2" cellpadding="0" cellspacing="0" id="table1">
  <tr>
    <td><strong>Name</strong></td>
    <td><strong>Address</strong> </td>
    <td><strong>Contact Num</strong> </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="20" /></td>
  </tr>

</table>
<input type="button" value="Add" onclick="addRow();" />
<input name="Submit" type="submit" value="Submit" />
<label>
<input name="h" type="hidden" id="h" value="0" />
</label>
</form>
</body>
</html>

submit.php

<?php 
include "connect.php";

$name = $_POST['name'];
$address = $_POST['address'];
$contactNum = $_POST['contactNum'];

foreach($name as $key => $value){
    $strQuery = "Insert Into emp1 (`NAME`,`ADDRESS`,`CONTACT_NUM`) Values('$name[$key]','$address[$key]',$contactNum[$key])"; //for error controling
    $result=mysql_query($strQuery) or die(mysql_error());
}

?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.