hello everyone..
i am suppose to create a form which contains rows that can be added on request (by clicking the add button).
i've work out on this and manage to link it to database.
bt the values on the dynamically added rows are not being saved into the datase. only the values of the first row is being added.
through some tutorials i got to know that i must use foreach--as to save those values from added rows to db. bt i am not sure of using it as i am just learning up php.
is it true that using foreach can solve this problem?
thanking everyone in advance...
how to add values from dynamically added rows into databaseadd rows dynamically and submit to datbase
15 Replies - 7890 Views - Last Post: 19 August 2010 - 08:36 PM
#1
how to add values from dynamically added rows into database
Posted 11 June 2009 - 07:57 PM
Replies To: how to add values from dynamically added rows into database
#2
Re: how to add values from dynamically added rows into database
Posted 11 June 2009 - 08:04 PM
It would help if you show us your code so we can see how and where you have these dynamic rows. If these rows don't contain any form elements, besides adding the row to the table you will also have to attach elements to the form for submitting. Typically I use hidden form inputs to pass along the info from a dynamic row I am adding.
But again we have to see your code to know how you are doing it.
But again we have to see your code to know how you are doing it.
#3
Re: how to add values from dynamically added rows into database
Posted 13 June 2009 - 10:20 PM
<!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> </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" />
</form>
</body>
</html>
this is my code...ive simplified the original version of my codes.
is this th ecorrect way of writting the add function to add the rows? i followed some tutorials as im very new to php.
how to add the values in the newly added rows into the database?
looking forward for your help.... thanking you in advance!
#4
Re: how to add values from dynamically added rows into database
Posted 13 June 2009 - 11:11 PM
ok dear here is a little code
first page
submit.php page
hope it will help you.
first page
<!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);
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="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 type="hidden" name="h" id="h" />
</label>
</form>
</body>
</html>submit.php page
<?php
$num = $_POST['h'];
?>
<?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 tblName (`name`,`address`,`contactNum`) Values('$name','$address',$contactNum)"; //for error controling
echo $strQuery."<br />"; //you can use myslq_query($strQuery)or die(mysql_error());
}
?>
hope it will help you.
#5
Re: how to add values from dynamically added rows into database
Posted 14 June 2009 - 04:00 AM
hello mr noorahmad...thank you a bunch for your help. im trying out your codes..
meanwhile, ive done this in the submit.php page. Is there any chances of changing this codes to be applied to my previous code? just wondering...
thank you...
meanwhile, ive done this in the submit.php page. Is there any chances of changing this codes to be applied to my previous code? just wondering...
<?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);
?>
thank you...
#6
Re: how to add values from dynamically added rows into database
Posted 17 June 2009 - 02:45 AM
ok dear i changed your code just copy and past this code
<?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
$strQuery = "Insert Into table1 (`NAME`,`ADDRESS`,`CONTACT_NUM`) Values('$name','$address',$contactNum)"; //for error controling
$result=mysql_query($sql) or die(mysql_error());
}
?>
#7
Re: how to add values from dynamically added rows into database
Posted 17 June 2009 - 06:59 PM
hello mr noorahmad, thanks a lot for ur help... really appreciate it.
bt i;m stil having a small problem. i copy n paste ur codes.... but after submiting it to the dtbs, ony the values in the added textfield are saved in the dtbse. the value in the first row is not added.
when i enter a a a, b b b, c c c, d d d, e e e....
this is the data entered into the dtbs. the value of the first row (a a a) is not saved into the dtbs.
Insert Into minie4 (name,address,contactNum) Values('','','')Insert Into minie4 (name,address,contactNum) Values('b','b','b')Insert Into minie4 (name,address,contactNum) Values('c','c','c')Insert Into minie4 (name,address,contactNum) Values('d','d','d')Insert Into minie4 (name,address,contactNum) Values('e','e','e')
need your help with this please...
submit.php
thank you !!
bt i;m stil having a small problem. i copy n paste ur codes.... but after submiting it to the dtbs, ony the values in the added textfield are saved in the dtbse. the value in the first row is not added.
when i enter a a a, b b b, c c c, d d d, e e e....
this is the data entered into the dtbs. the value of the first row (a a a) is not saved into the dtbs.
Insert Into minie4 (name,address,contactNum) Values('','','')Insert Into minie4 (name,address,contactNum) Values('b','b','b')Insert Into minie4 (name,address,contactNum) Values('c','c','c')Insert Into minie4 (name,address,contactNum) Values('d','d','d')Insert Into minie4 (name,address,contactNum) Values('e','e','e')
need your help with this please...
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);
}
?>
<!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>
thank you !!
#8
Re: how to add values from dynamically added rows into database
Posted 17 June 2009 - 09:57 PM
here is the code
submit.php
hope it help you.
<?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>submit.php
<?php
include "dbCon.php";
$num = $_POST['h'];
if($num==0)
{
$name = $_POST['name'];
$address = $_POST['address'];
$contactNum = $_POST['contactNum'];
$strQuery = "Insert Into table1 (`NAME`,`ADDRESS`,`CONTACT_NUM`) Values('$name','$address',$contactNum)<br />"; //for error controling
$result=mysql_query($sql) or die(mysql_error());
}
else
{
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 table1 (`NAME`,`ADDRESS`,`CONTACT_NUM`) Values('$name','$address',$contactNum)<br />"; //for error controling
$result=mysql_query($sql) or die(mysql_error());
}
}
?>
hope it help you.
#9
Re: how to add values from dynamically added rows into database
Posted 20 June 2009 - 07:50 AM
thanks alot mr noorahmad! your codes really helped me alots. i managed to make it work with your help. thank you for your time!
#10
Re: how to add values from dynamically added rows into database
Posted 21 June 2009 - 12:56 AM
Your welcome
#11
Re: how to add values from dynamically added rows into database
Posted 22 June 2009 - 01:16 AM
not working fine,
the columns data will not be posting to php page?
the counter variable also not working..
check once again?
the columns data will not be posting to php page?
the counter variable also not working..
check once again?
#12
Re: how to add values from dynamically added rows into database
Posted 22 June 2009 - 01:48 AM
could you please show me your current code?
#13
Re: how to add values from dynamically added rows into database
Posted 19 August 2009 - 10:14 AM
I'm hoping you are still active noorahmad and that you could help me completing this script above.
I changed the "insert" to mySQL with echo, but it won't return correctly.
I seems to me, a complete beginner, that "frm.h.value=i;" isn't working correctly...
test.php:
submit.php:
I changed the "insert" to mySQL with echo, but it won't return correctly.
I seems to me, a complete beginner, that "frm.h.value=i;" isn't working correctly...
test.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">
<head>
<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 el1 = document.createElement('input');
el1.type = 'text';
el1.name = 'name' + i;
el1.id = 'name' + i;
el1.size = 20;
el1.maxlength = 20;
firstCell.appendChild(el1);
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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title></head>
</head>
<body>
<form action="submit.php" method="post" name="frm" id="frm">
<table border="1" 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:
<!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>Untitled Document</title>
</head>
<body>
<?php
$num = $_POST['h'];
if($num==0)
{
$name = $_POST['name'];
$address = $_POST['address'];
$contactNum = $_POST['contactNum'];
//$strQuery = "Insert Into table1 (`NAME`,`ADDRESS`,`CONTACT_NUM`) Values('$name','$address',$contactNum)<br />"; //for error controling
//$result=mysql_query($sql) or die(mysql_error());
echo $name."<br />".$address."<br />".$contactNum."<br /><br />";
}
else
{
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 table1 (`NAME`,`ADDRESS`,`CONTACT_NUM`) Values('$name','$address',$contactNum)<br />"; //for error controling
//$result=mysql_query($sql) or die(mysql_error());
echo $name."<br />".$address."<br />".$contactNum."<br /><br />";
}
}
?>
</body>
</html>
#14 Guest_chacha*
Re: how to add values from dynamically added rows into database
Posted 14 August 2010 - 11:46 PM
#15 Guest_chacha*
Re: how to add values from dynamically added rows into database
Posted 14 August 2010 - 11:53 PM
|
|

New Topic/Question
Reply




MultiQuote





|