Forum Moderators: coopster
im working on a form which contains a part where the rows can be added dynamically by clicking on add button. i've done that part, but im having trouble in adding those values in the added rows into database.
these are the simplified codes of my project.
page1.php
<!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('student');
var lastRow = tbl.rows.length;
var iteration = lastRow ;
var row = tbl.insertRow(lastRow);
var firstCell = row.insertCell(0);
var textNode = document.createTextNode(iteration);
firstCell.appendChild(textNode);
var secondCell = row.insertCell(1);
var el = document.createElement('input');
el.type = 'text';
el.name = 'name' + i;
el.id = 'name' + i;
el.size = 20;
el.maxlength = 20;
secondCell.appendChild(el);
var thirdCell = row.insertCell(2);
var el2 = document.createElement('input');
el2.type = 'text';
el2.name = 'add' + i;
el2.id = 'add' + i;
el2.size = 20;
el2.maxlength = 20;
thirdCell.appendChild(el2); var fourthCell = row.insertCell(3);
var el3 = document.createElement('input');
el3.type = 'text';
el3.name = 'cont' + i;
el3.id = 'cont' + i;
el3.size = 20;
el3.maxlength = 20;
fourthCell.appendChild(el3);
i++;
frm.h.value=i;
}
</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="post" name="frm" id="frm">
<table width="40%" border="2" cellpadding="0" cellspacing="0" id="student">
<tr>
<td>Bil</td>
<td>Name</td>
<td>Address</td>
<td>Contact Num </td>
</tr>
<tr>
<td>1</td>
<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 type="submit" name="Submit" value="Submit" />
<label>
<input type="hidden" name="h" id="h" />
</label>
</form>
</body>
</html>
addDB.php
<?phpinclude "dbCon.php";
$num=$_POST['h'];
$Name=$_POST["name"];
$Address=$_POST["address"];
$ContactNum =$_POST["contactnum"];
?>
<?php
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';} //for error controling
if(isset($_REQUEST["contactnum".$i])){$contactNum = $_REQUEST["contactNum".$i];}//else{$contactNum = 00;} //for error controling
$strQuery = "Insert Into minie (name,address,contactNum) Values('$Name','$Address','$ContactNum')"; //for error controling
echo $strQuery."<br />"; //you can use myslq_query($strQuery)or die(mysql_error());
}
$result=mysql_query($strQuery);
*/
?>
can anyone please help me out on how to change these codes so that i can save the values in the added rows into database?
thanking you in advance...
and so on...
--------------------------------
Before:
include "dbCon.php";
write:
die(var_dump($_REQUEST));
And see how data is arriving. That will answer your question!
---------------------------------
Then you can do something like:
// use foreach NOT: for($i=0;$i<=$num;$i++)
foreach($_REQUEST['data'] as $data)
{
// code
}
ive done all the corrections that you have told, but it is stil not working.
i guess im not applying it correctly. can you elaborate a little on the foreach part?
and for the "data[name][]" part...
should it be "data[address][]" and "data[contactnum][]" for the other two?
need your help...thank you...
ive done all the corrections that you have told, but it is stil not working.
i guess im not applying it correctly. can you elaborate a little on the foreach part?
and for the "data[name][]" part...
should it be "data[address][]" and "data[contactnum][]" for the other two?
need your help...thank you...
Did you "die(var_dump($_REQUEST));" that helps a lot.
your code following for($i=0;$i<=$num;$i++)
is good (but i dont see it closer, that is your job)
you go well, go on...
(im going offline now, good luck)I
-_- .zZZ
NomikOS
<tr>
<td>1</td>
<td>
<input name="data[1][name]" type="text" id="name" size="20" maxlength="20" />
</td>
<td>
<input name="data[1][address]" type="text" id="address" size="20" maxlength="20" />
</td>
</tr>
<tr>
<td>2</td>
<td>
<input name="data[2][name]" type="text" id="name" size="20" maxlength="20" />
</td>
<td>
<input name="data[2][address]" type="text" id="address" size="20" maxlength="20" />
</td>
</tr>
<tr>
<td>2</td>
<td>
<input name="data[n][name]" type="text" id="name" size="20" maxlength="20" />
</td>
<td>
<input name="data[n][address]" type="text" id="address" size="20" maxlength="20" />
</td>
</tr>
if you can assign n en data[n] with js (that's easy) and then:
if ($_POST)
foreach ($_POST as $data)
var_dump($data, 0);
you will be OK, try it!
and "$result=mysql_query($strQuery); " must be inside of foreach! (before })
ahora si me voy ..zZZ
help please....
<!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()
{
i++;
frm.h.value=i;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);
}
</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" />
<input type="hidden" name="h" id="h" />
</form>
</body>
</html>
submit.php
<?php
include "dbCon.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';}*/ //for error controling
if(isset($_REQUEST["contactNum$i"])){$contactNum = $_REQUEST["contactNum$i"];}/*else{$contactNum = 00;}*/ //for error controling
$sql = "Insert Into minie4 (name,address,contactNum) Values('$name','$address','$contactNum')"; //for error controling
$result=mysql_query($sql) or die(mysql_error());
echo ($sql);
}
?>
help please...