Welcome Guest, Not a member yet? Register   Sign In
Processing dynamic forms
#1

[eluser]KeyStroke[/eluser]
Hi,

I have a form that's used to approve uploaded images in bulk before displaying them on the site. Each image will have two radio buttons next to it: "approve" and "delete".

My question is how do you process this kind of forms? the names of the radio buttons constantly change depending on the image's ID in the database, so I can't use the usual:
Code:
$field_value = $_POST['field_name'];

Any ideas?


Appreciate your help.
#2

[eluser]Leggy[/eluser]
if you set <input type="hidden" name="id" value="ID" /> and for update you would do <input type="radio" name="update-ID" value="whatever" /> you can use that for it by doing $id = $_POST['id'] and do $_POST['update-'$id] to get the post.
#3

[eluser]KeyStroke[/eluser]
Good idea, although I was hoping I wouldn't need to add more HTML.

How would you differentiate the values and the ideas though since they're stored in the same POST array?
#4

[eluser]JoostV[/eluser]
Say, the ID of an image is 23. In your form, you would add two radiobuttons as such:

Code:
<input type="radio" name="process[23]" value="delete" />
<input type="radio" name="process[23]" value="approve" checked="checked" />

Then on submit, loop through radiobuttons as such:

Code:
foreach ($_POST['process'] as $key=>$value) {
   if ($value== 'delete') {
       // Do something to delete image with the ID $key
   } else {
       // Do something to approve image with the ID $key
   }
}

This code will loop through all the image radio buttons, as long as you keep naming them:
Code:
<input type="radio" name="process[<?php echo $ID; ?>]" value="delete" />
<input type="radio" name="process[<?php echo $ID; ?>]" value="approve" checked="checked" />

Of course, if the user can make a mistake in filling out the form, it is necessary to determine whether to the radiobuttons according to their $_POST value or to their default value (I chose 'approve' here)
#5

[eluser]KeyStroke[/eluser]
Thanks Joost.

So you mean PHP will treat 'process[xx]' (where xx are numbers) as an array within the POST array, and the numbers as the keys?
#6

[eluser]JoostV[/eluser]
Hi Keystroke,

Yep, PHP will store the POST value of a radio button
Code:
<input type="radio" name="process[23]" value="delete" />
into
Code:
$_POST['process'][23] = 'delete';

SO, in order to process all process[ID] radio buttons, you just have to loop through array
Code:
$_POST['process']

Of course, you can also use
Code:
$this->input->post('process')

Make sure to validate you user input. Accept only 'approve' and 'validate' options and throw an error on all other input.




Theme © iAndrew 2016 - Forum software by © MyBB