Jump to content

[SOLVED] Trouble with dynamic fields (newbie)


blythy

Recommended Posts

Hello, basically I have a form that allows the user to enter a number, and then on the next page that number is used to generate that amount of fields to enter image names.

This works so far, I'm name my field names using a for loop ie

[code]for($i=1; $i<=$numberOfFields; $i++)
{<input type=\"text\" name=\"addMorePics$i\" />}[/code]

So say the user chooses 3 fields, they'll be named addMorePics1, addMorePics2 and addMorePics3. In my next page I'm trying to add them into one variable that encloses each filename with img tags as such(note I'm using a hidden field to pass the number of fields):

[code]for($i=1; $i<=$numberOfFields; $i++)
{$morePics = morePics ."<img src=\"../images/"$_POST['addMorePics$i']" />";}[/code]

The idea is that say the values from the fields were picture1.jpg, picture2.jpg and picture3.jpg the variable when echo'd would produce the following html code:
[code]
<img src=""../images/picture1.jpg" /><img src=""../images/picture2.jpg" /><img src=""../images/picture3.jpg" />
[/code]

Obviously this is not working and I apologise for my ignorance in not knowing why or the correct way to do it, but I'm only just starting out with PHP. Any help would be greatly appreciated,
blythy
the first page
[code]
<form method="post" action="next_page.php">
<input type="text" name="num_fields">
<input type="submit" value="Go">
</form>
[/code]
next_page.php
[code]
$num=$_POST['num_fields'];
for($count=1;$count<$num;$count++)
{
      echo "<input type=\"text\" name=\"image{$count}\">";
}
[/code]
Sorry I musn't have made it very clear, forgive me it is 3am  :'(

I have the number of fields displaying properly, the problem then lies in using php to make use of these fields. In your example if I entered 3 in the first page, I will end up with three input fields named image1, image2 and image3 respectively. I am having trouble then in the page after using a loop to to assign the input of image1, image2 and image3 to a variable. I know my problem is that I'm trying to do it this way
[code]"$_POST['image$i']"[/code]
inside a for loop. I was hoping that while $i increments in my for loop, the "$_POST['image$i']" will increment also(ie "$_POST['image1]" then "$_POST['image2]"), but this seems not to be the case.

I'm sorry if this is no clearer

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.