Forum Moderators: coopster

Message Too Old, No Replies

Retrieve data from dynamically added textfield.

         

minie

4:13 am on Jun 29, 2009 (gmt 0)

10+ Year Member



hello everyone.
im working on a small system where it contains forms to be completed. this form has a part where the number of textfields are added dynamically according to the number of items to be entered. all the info will be saved into the database. i could retrieve the infos into the forms to be edited in future.

ive done most of it...(i'm able to retrieve all the infos into the form except the infos of the dynamically added textfield)

can anyone please help me out with it? ( how to retrieve and update)
following is the codes of what ive done so far...


<html>

<form method=POST action="amend1.php">
<DIV ALIGN="LEFT"><INPUT TYPE="text" NAME="nsp" SIZE="50" MAXLENGTH="50">
<BR><BR><INPUT TYPE="submit" NAME="go" VALUE="Search "></DIV></form><TR><TD WIDTH="29%">

<?php
// Show simple format of the records so person can choose the reference name/number
// this is then passed to the next page, for all details

include "dbCon.php";

$q="SELECT * FROM daftarhma";

$result = mysql_query( $q )
or die(" - Failed More Information:<br><pre>$q</pre><br>Error: " . mysql_error());

?></DIV></TD>
</html>


<?PHP
session_start();
?>
<HTML>

<?php
$record = $_POST['nsp'];
echo "NoSiriPendaftaran: $record";

include "dbCon.php";

$result=mysql_query(" SELECT * FROM daftarhma WHERE NoSiriPendaftaran='$record'");
$num=mysql_num_rows($result);
$i=0;
while ($i < $num) {

// collect all details for our one reference
//$Kementerian=$_POST($result, $i, "Kementerian");
$Bahagian=mysql_result($result, $i,"Bahagian");
$KodNasional =mysql_result($result, $i,"kodNasional");
$Kategori =mysql_result($result, $i,"kategori");
$SubKategori =mysql_result($result, $i,"subKategori");


//$view='<img src=http://your_website/thumbs/';
//echo "<br>$view$thumb.jpg><br><br>";

//next we display only the details we want to allow to be changed in a form object
// the other details that we won't allow to be changed can be echoed to the screen
//note the hidden input line 3 below. We don't need to echo it to the screen
?>

<TABLE WIDTH="100%" CELLPADDING="10" CELLSPACING="0" BORDER="2"> <TR ALIGN="center" VALIGN="top">
<TD ALIGN="center" COLSPAN="1" ROWSPAN="1" BGCOLOR="#F2F2F2">

<FORM ACTION="amend2.php" METHOD="post">
<INPUT TYPE="hidden" NAME="ud_id" VALUE="<? echo "$record" ?>">

<p align="left">
<BR>Bahagian:<BR>
<input name="2" type="text" value="<? echo "$Bahagian"?>">
</p>
<P ALIGN="LEFT">Kod Nasional:<BR><TEXTAREA NAME="3" COLS="50" ROWS="4">
<? echo "$KodNasional"?>
</TEXTAREA></P>

<p align="left">
<BR>Kategori:<BR>
<input name="4" type="text" value="<? echo "$Kategori"?>">
</p>
<p align="left">
<BR>SubKategori:<BR>
<input name="5" type="text" value="<? echo "$SubKategori"?>">
</p>
<HR><B>
</B>

</P><P><INPUT TYPE="Submit" VALUE="Update the Record" NAME="Submit"> </P></FORM></TD></TR></TABLE>

<?
++$i;
}
?>


<?PHP
session_start();
?>

<?php

$ud_id=$_POST['ud_id'];
$kem=$_POST['2'];
$kod=$_POST['3'];
$kat=$_POST['4'];
$skat=$_POST['5'];

if ($ud_id == "") echo "! No identifier retrieved";
else
echo "Amending record $ud_id";

//clean up any carriage returns etc

//$kem = preg_replace("/[\n\r]*/","",$kem);
//$kod = preg_replace("/[\n\r]*/","",$kod);

include "dbCon.php";

$k=mysql_query(" UPDATE daftarhma
SET Kementerian='$kem', KodNasional='$kod', Kategori='$kat', SubKategori='$skat' WHERE reference='$ud_id'");
echo ($k);
echo "<BR>Record $ud_id <-- Updated<BR><BR>";
?>

this is the part of dynamically adding part...


<!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('penempatan');
var lastRow = tbl.rows.length;
var iteration = lastRow - 1;
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 = 'tarikh' + i;
el.id = 'tarikh' + i;
el.size = 15;
el.maxlength = 15;
secondCell.appendChild(el);

var thirdCell = row.insertCell(2);
var el2 = document.createElement('input');
el2.type = 'text';
el2.name = 'lokasi' + i;
el2.id = 'lokasi' + i;
el2.size = 60;
el2.maxlength = 60;
thirdCell.appendChild(el2);

var fourthCell = row.insertCell(3);
var el3 = document.createElement('input');
el3.type = 'text';
el3.name = 'namaPegawai' + i;
el3.id = 'namaPegawai' + i;
el3.size = 60;
el3.maxlength = 60;
fourthCell.appendChild(el3);


var fifthCell = row.insertCell(4);
var el4 = document.createElement('input');
el4.type = 'text';
el4.name = 'txtRow4' + i;
el4.id = 'txtRow4' + i;
el4.size = 20;
el4.maxlength = 20;
fifthCell.appendChild(el4);
}

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

<body bgcolor="#FFFFCC">
<form action="" method="post" name="frm" id="frm">

<tr>
<td><table border="1" bordercolor="#000000" bgcolor="#FFFFFF" id="penempatan" align="center" width="100%" >
<tr>
<td colspan="5" align="center" bgcolor="#FFFF99"><strong>PLACING</strong></td>
</tr>
<tr>
<td width="5%" align="center" bgcolor="#FFFF99">Bil.</td>
<td width="17%" align="center" bgcolor="#FFFF99">Tarikh</td>
<td width="31%" align="center" bgcolor="#FFFF99">Lokasi</td>
<td width="24%" align="center" bgcolor="#FFFF99">Nama Pegawai</td>
<td width="23%" align="center" bgcolor="#FFFF99">Tandatangan</td>
</tr>


</table>
<input type="button" onclick="addRow();" value="ADD" />
<input type="hidden" name="h" id="h"/>
</td></tr>

<tr>

</form>

</body>
</html>

submiting to dtbs


$num = $_POST['h'];
$NoSiriPendaftaran=$_POST["NoSiriPendaftaran"];
echo ($num);
for($i=1;$i<=$num;$i++)
{
if(isset($_REQUEST["tarikh$i"])){$Tarikh = $_REQUEST["tarikh$i"];}
if(isset($_REQUEST["lokasi$i"])){$Lokasi = $_REQUEST["lokasi$i"];}/*else{$address= 'address';}*/ //for error controling
if(isset($_REQUEST["namaPegawai$i"])){$NamaPegawai = $_REQUEST["namaPegawai$i"];}/*else{$contactNum = 00;}*/ //for error controling

$sql = "Insert Into penempatanhm ( NoSiriPendaftaran, tarikh, lokasi, namaPegawai) Values ('$NoSiriPendaftaran', '$Tarikh','$Lokasi','$NamaPegawai')"; //for error controling
$result=mysql_query($sql) or die(mysql_error());

echo ($sql);
}


would appreciate any helps form anyone....thanking you in advance!

omoutop

7:27 am on Jun 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just an idea:

If $k textfields are to be added then with a small loop (for i=1 to $k) you can create those textfields with unique name like "mytextfield_".$k or something similar

On the post process you retreive them as $_POST["mytextfield_".$k] and manipulate them (again in a small loop)

Hope you get the idea

minie

1:12 am on Jul 1, 2009 (gmt 0)

10+ Year Member



thanks a lot for ur help!