Welcome Guest, Not a member yet? Register   Sign In
$_POST values with a dynamic list
#1

[eluser]tim1965[/eluser]
Hi

Im either going to kick myself at the answer to this or maybe it really is as complicated as it seems.
I have a multi image upload view with two forms on it. The first form handles the upload and the second displays the image and image description. I also give the user the option to edit or delete the image. It is the edit part that i am struggling with. I create the second part of the form in a foreach loop
<p>&lt;?php foreach($picture as $r): ?&gt;
<img src="&lt;?php echo base_url().$path.$r['imagename']; ?&gt;" width="100" height="100" alt="&lt;?php echo $r['photo_desc']; ?&gt;" title="&lt;?php echo $r['photo_desc']; ?&gt;" /><br>
<p>&lt;?php echo $r['photo_desc']; ?&gt;</p>&lt;?php echo anchor('property_registration/multifileupload_v1/delete_photo/'.$r['imagename'],'Delete Photo'); ?&gt;
<div class="add_desc">Edit Description</div><div class="photodesc">&lt;?php echo form_input($photodesc); ?&gt;&lt;input name="image_name" type="hidden" value="&lt;?PHP echo $r['imagename']; ?&gt;" /&gt;&lt;input type="submit" name="update_photo_desc" id="update_photo_desc" value="Update Description" /></div>&lt;?php echo form_error('photodesc'); ?&gt;
&lt;?php endforeach; ?&gt;</p>
To allow users to edit the comment i have added a text field input
Quote:
<div class="add_desc">Edit Description</div><div class="photodesc">&lt;?php echo form_input($photodesc); ?&gt;
Quote:
created by an array in my controller.
Quote:
$data['photodesc'] = array(
'name' => 'photodesc[]',
'id' => 'photodesc[]',
'maxlength' => '50',
'size' => '50',
'style' => 'width:50%'
);
Quote:
I then hide this using javascript, and show it when they click an icon.

So to my problem. Its very simple. The $_POST array values are being overwritten. I onñly want the description field that has been amended and the associated image name. I have tried everything from mergin arrays to stripping out null values. But i cannot seem to crack it. The other option i could go down is to open a new view and get the user to enter the data in that and then post it. So if my approach is hard work then please let me know. Apoilogies as this is not a pure Codeigniter issue.
Thanks
#2

[eluser]tomcode[/eluser]
The following two input fields produce an invalid form when repeated in a loop, they then do not dispose of unique names / ids :

Code:
&lt;input name="image_name" type="hidden" value="&lt;?PHP echo $r['imagename']; ?&gt;" /&gt;&lt;input type="submit" name="update_photo_desc" id="update_photo_desc" value="Update Description" />
#3

[eluser]tim1965[/eluser]
Sorry and apologies if i am being thick, but i dont underatand your response. Why would a hidden field containing the image id and the submit button for the form cause a problem. Could you give me some more detail.
Thanks
#4

[eluser]Phil Sturgeon[/eluser]
There is no "value" for the photodesc form_input. Is this correct?

If you want to put one in, try this:

Code:
&lt;?php echo form_input($photodesc + array('value' => $r['photo_desc'])); ?&gt;
#5

[eluser]tomcode[/eluser]
Both the hidden field and the submit button are repeated in the loop. So Your form will consist of multiple elements having the same id / name.

The name and the id attributes are supposed to be unique.

The name attribute is used to build the $_POST data, so if it's not unique then of course the data gets scrambled.
#6

[eluser]tim1965[/eluser]
Tomcode

Yes i have put the submit button into the foreach so that it displays when the image description field is called. This is to try and ensure that only one field description change is made at a time and so i can associate the description with the image name i.e. put a big button next to the text field marked Update.

The issue is that on submit (doing it this way) i need to combine $_POST['photodesc'] with
$_POST['image_name'] and then strip out all blank values for photodesc. $_POST array below.
Quote:
array(3) { ["photodesc"]=> array(8) { [0]=> string(48) "Need to associate this value with the image_name" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" [5]=> string(0) "" [6]=> string(0) "" [7]=> string(0) "" } ["image_name"]=> array(8) { [0]=> string(16) "e68RDq2YqQYX.jpg" [1]=> string(16) "vgtVJX2PdpZY.jpg" [2]=> string(16) "wKwrwYtj75VM.jpg" [3]=> string(16) "3gGx5tpBhJh5.jpg" [4]=> string(16) "HPmlICUPQzqS.jpg" [5]=> string(16) "jSh1pxyFS1Rh.jpg" [6]=> string(16) "SiQD7TZ1dV8g.jpg" [7]=> string(16) "7n822ZfGyilB.jpg" } ["update_photo_desc"]=> string(18) "Update Description" }
Quote:
So the issue is how do i ensure the text in photodesc[0] is associated with the image_name[0]
NOTE the element postitions will always be correct so photodesc[0]=image_name[0].
So my plea for help is how to do this, or am i flogging a dead camel(apologies to all camels out there) by taking this approach and is there a more elegant way to approach this. Thanks
#7

[eluser]tomcode[/eluser]
Well, one solution would be :

1. suffix all names / ids with numbered [], also the submit buttons
Code:
&lt;input type="hidden" name="img[0]" value="xxxx" /&gt;
    &lt;input type="submit" name="submit[0]" value="Submit" /&gt;

    &lt;input type="hidden" name="img[1]" value="xxxy" /&gt;
    &lt;input type="submit" name="submit[1]" value="Submit" /&gt;

        ...

2. then check the $_POST array which submit button has been used

Or just build multiple forms with an identifier in the submit URL

Code:
form_open('controller/method/form_ident);
#8

[eluser]tim1965[/eluser]
Guys

Many thanx for taking time out to help me with this. In the end i went with opening a new form via anchor popup and grabbing the POST array from that. What i was trying to do was too much effort.




Theme © iAndrew 2016 - Forum software by © MyBB