blythy Posted January 3, 2007 Share Posted January 3, 2007 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 Link to comment https://forums.phpfreaks.com/topic/32654-solved-trouble-with-dynamic-fields-newbie/ Share on other sites More sharing options...
fert Posted January 3, 2007 Share Posted January 3, 2007 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] Link to comment https://forums.phpfreaks.com/topic/32654-solved-trouble-with-dynamic-fields-newbie/#findComment-151937 Share on other sites More sharing options...
blythy Posted January 3, 2007 Author Share Posted January 3, 2007 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 Link to comment https://forums.phpfreaks.com/topic/32654-solved-trouble-with-dynamic-fields-newbie/#findComment-151943 Share on other sites More sharing options...
fert Posted January 3, 2007 Share Posted January 3, 2007 [code]for($i=1; $i<=$numberOfFields; $i++){$name="addMorePics".$i;$morePics.="<img src=\"../images/"{$_POST[$name]}" />";}[/code] Link to comment https://forums.phpfreaks.com/topic/32654-solved-trouble-with-dynamic-fields-newbie/#findComment-151946 Share on other sites More sharing options...
blythy Posted January 3, 2007 Author Share Posted January 3, 2007 Didn't quite work with the curly brackets, so I took them out and bingo![code] for($i=1; $i<=$numberOfFields; $i++) { $name="addMorePics".$i; $morePics.="<img src=\"../images/{$_POST[$name]}\" />"; }[/code]Thankyou so much for your help! Link to comment https://forums.phpfreaks.com/topic/32654-solved-trouble-with-dynamic-fields-newbie/#findComment-151959 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.