Page 1 of 1

retrieving varibles submitted from a form from array??

Posted: Thu Jan 05, 2006 11:14 am
by stylus
Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I cannot figure out the data that I submit is not being displayed in as the values when this form is sumitted.  All said and done I will need the data to be added to a database.

The "field_count" works, when I type in 2 submit it reloads, and 2 is still the value of that input box. but the 3 other fields that have the _$i won't.

Code: Select all

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

<body>

<form action="test_form.php" method="post">
<input type="text" name="field_count" size="5" value="<?php echo $_POST['field_count']; ?>">
<input name="submit" value="Go" type="submit" onClick="document.add_marginworksheet.submit()">
<table>
<?php
      $field_count = $_POST['field_count'];
            for($i= 1;$i <= $field_count;++$i) 
	{ 
	   echo "
                        <tr>
                           <td align='left' width='90'>Model:<br><input type='text' size='10' name='model_$i' value=\"".$_POST['model_$i']."\" ></td>
                           <td align='left'>Description:<br><input type='text' size='40' name='description_$i' value=\"".$_POST['description_$i']."\" ></td>
                           <td align='right'>Price:<br><input type='text' size='10' name='price_$i' value=\"".$_POST['price_$i']."\" ></td>
	       </tr> ";
	};
?>
</table>
<input type="submit" value="submit" name="Submit">
</form>


</body>
</html>

Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Thu Jan 05, 2006 11:29 am
by John Cartwright
variables inside single quotes won't parse.. instead change the single quotes to double quotes, or if you insist on using single quotes escape them.

Posted: Thu Jan 05, 2006 1:39 pm
by stylus
I updated my code, and it still won't display the values that have the _$i attached, I get the error message "Notice: Undefined index: model_$i in D:\Data\Inetpub\wwwroot\test_form.php on line 19" for each input.

The error says "model_$i" not "model_1" so I set it manually and it works, it seems as though in my value statement it is not parsing the "$i" part.


Code: Select all

<form action="test_form.php" method="post">
<input type="text" name="field_count" size="5" value="<?php echo $_POST['field_count']; ?>">
<input name="submit" value="Go" type="submit" onClick="document.add_marginworksheet.submit()">
<table>
<?PHP 
      $field_count = $_POST['field_count'];
            for($i= 1;$i <= $field_count;++$i) 
	{ 
	   echo "
                        <tr>
                           <td align=left width=90>Model:<br><input type=text size=10 name=\"model_$i\" value=\"".$_POST['model_$i']."\"></td>
                           <td align=left width=90>Description:<br><input type=text size=10 name=\"description_$i\" value=\"".$_POST['description_$i']."\"></td>
                           <td align=right width=90>Price:<br><input type=text size=10 name=\"price_$i\" value=\"".$_POST['price_$i']."\"></td>
	       </tr> ";
	};
?>
</table>
<input type="submit" value="submit" name="Submit">
</form>

Posted: Thu Jan 05, 2006 2:13 pm
by twigletmac
As Jcart said, variables within single quotes will not be parsed, so try changing things like:

Code: Select all

$_POST['model_$i']
to

Code: Select all

$_POST['model_'.$i]
Mac