Jump to content

php and Looping textboxes


DavidMR

Recommended Posts

Ok, im a newbie here so be gentle

Im working on an application for work, its doing a process called netting off where a recordset of data is extracted in part, this data is then edited, ie various lines from the same office(being unique) and then the data is updated.

so for example my recordset would be :

select * from table where office="ground";

i then populate this in a series of sequential textboxs as follows :

<td><INPUT TYPE="TEXT" NAME = "lineno<?PHP echo $r_data["Line number"] ?>" size=1 maxlength=5 value="<?PHP echo $r_data["Line number"] ?>"></td>
<td><INPUT TYPE="TEXT" NAME=location<?PHP echo $r_data["Line number"] ?> size=8 maxlength=20 value="<?PHP echo $r_data["LOCATION"] ?>"></td>

this is looped thorough the recordset and populating the fields with their counterpart in the database. The user can then edit the fields and will click update the update the data.

the problem i have is that when i get to the end, $_POST["lineno136"] giving the error unidentified index, where the final html code clearly states the name of hte textbox is "name=lineno136"

now if anyone has a better idea or a reason why this isnt working, id be extremely grateful
Link to comment
https://forums.phpfreaks.com/topic/35674-php-and-looping-textboxes/
Share on other sites

Instead of using sequentially named variables, use arrays for your names:
[code]
<td><INPUT TYPE="TEXT" NAME = "lineno[<?php echo $r_data["Line number"] ?>]" size=1 maxlength=5 value="<?php echo $r_data["Line number"] ?>"></td>
<td><INPUT TYPE="TEXT" NAME = "location[<?php echo $r_data["Line number"] ?>]" size=8 maxlength=20 value="<?php echo $r_data["LOCATION"] ?>"></td>
[/code]
Then in the processing script, you can do something like:
[code]<?php
foreach ($_POST['lineno'] as $lineno => $val)
  echo 'Line number: ' . $lineno . ', Location: ' . $_POST['location'][$lineno] . '<br>';
?>[/code]

Ken

ok ill elaborate, sorry for the lack of info...

<?php
set_time_limit(0);
require("./include/common.php");
DbOpen();
PageHeader("Edit line", "", "");
$curtable = 'TABLE';
?>

<?php
if (isset($_GET['location'])) {
$sqlquery = "select * from $curtable where [LOCATION] like '%";
$sqlquery .= $_GET['location'];
$sqlquery .= "%';";
}
else {
$sqlquery = "SELECT top 30 * FROM $curtable";
}
?>

<table width=100% cellspacing=0 cellpadding=0>
<tr>
<td width=50%><h1 style="display: inline">Edit line</h1></td>
</tr>
</table>
<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=0>
<FORM METHOD="POST" NAME="frmData "ACTION=demo.php">
<table width=100% cellspacing=0 cellpadding=0>
<tr>
<th class=Label width=160>Line</th>
<th class=Label width=160>Location </th>
<th class=Label width=160>Identifier </th>
</tr>
<?php
$q_data = mssql_query($sqlquery, $db) or die ("Sql error : Invalid Query String - $sqlquery ");
$rowcount = mssql_num_rows($q_data);
print ("$rowcount records returned.");
while ( $r_data = mssql_fetch_array($q_data) ) {
?>
<tr>
<table width=100% cellspacing=0 cellpadding=0>
<Form Name ="form1" Method ="POST" ACTION = "demo.php?&location=WPP049MG">
<td><INPUT TYPE="TEXT" NAME = "lineno<?PHP echo $r_data["Line number"] ?>" size=1 maxlength=5 value="<?PHP echo $r_data["Line number"] ?>"></td>
<td><INPUT TYPE="TEXT" NAME=location<?PHP echo $r_data["Line number"] ?> size=8 maxlength=20 value="<?PHP echo $r_data["LOCATION"] ?>"></td>
<td><INPUT TYPE="TEXT" NAME=id<?PHP echo $r_data["Line number"] ?> size=1 maxlength=3 value="<?PHP echo $r_data["Identifier"] ?>"></td>
</FORM>
</Table>
</tr>
<?php
}
?>
<table width=100% cellspacing=0 cellpadding=0>
<Form Name ="FrmUpdate" Method ="POST" <?php echo ("action='export.php?sqlquery=" . urlencode ($sqlquery) . "'>"); ?>
</FORM>
</table>

</table>
<?php
$q_data = mssql_query($sqlquery, $db) or die ("Sql error : Invalid Query String - $sqlquery ");
while ( $r_data = mssql_fetch_array($q_data) ) {
$lno = "lineno" . $r_data["Line number"];
//if (isset($_POST["$lno"]) {
//print ($_POST["$lno"]);
print ($_POST[$lno]);
//print $lno;
}
?>
</FORM>
</TABLE>

<?php
PageFooter();
DbClose();
?>


Now this is the code(edited to avoid me losing my job), in the end i do a loop to print the values of the textbox being called by post and a looping variable, but the error i keep getting is :

Notice: Undefined index: lineno137 in c:\inetpub\wwwroot\demo.php on line 85

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.