Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve data from dynamically generated form

Tags:

php

I'm trying to use a dynamically created form to edit content on a page. I retrieve information from a DB table (in this case image captions) and display them in text areas ready to be edited and saved back into the DB.

This seems to do the job of organizing and displaying the form:

echo"<form action='edit.php' method='post'>";
 for ($limit;$limit<=$all_values;$limit++)
     {
      echo "<textarea cols='15' rows='3' name='caption' value='$caption_arr[$limit]'>
             $caption_arr[$limit]</textarea><br>
      }
      echo "<br><input type='submit' value='Edit' name='pictureEedit'></form>";

But something goes wrong from here. When I enter this in edit.php:

 $caption=$_POST['caption'];
 echo $caption;

And I only get the caption from the last field. When I added check boxes to the form it all worked fine provided I only checked 1 but if I checked 2 or more it would only give me the value for the last one.

I also tried this:

 $caption[$x]=$_POST['caption'];
 foreach ($caption as $key => $value) {echo $key.$value.'<br>';}

but got the same result.

like image 357
spacitron Avatar asked Sep 09 '26 03:09

spacitron


1 Answers

In your form

name='caption'

has to be

name='caption[]'

And later on you can do:

foreach ($_POST['caption'] as $key => $value) {echo $key.'=>'.$value.'<br>';}
like image 166
djot Avatar answered Sep 11 '26 15:09

djot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!