retrieving varibles submitted from a form from array??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stylus
Forum Newbie
Posts: 17
Joined: Fri Dec 16, 2005 9:22 am

retrieving varibles submitted from a form from array??

Post 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]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
stylus
Forum Newbie
Posts: 17
Joined: Fri Dec 16, 2005 9:22 am

Post 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>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply