tags:

views:

85

answers:

3

Hello

I am working on an image gallery at the moment. When a user is logged in, an x appears over each image and a checkbox next to it to enable the user to delete that image. Only if the checkbox is checked will the image be deleted.

They are both in a (one for each image in the gallery), the small image "x.gif" is an input type="image" that submits the $_POST form. The checkbox inherits the name of the image and is to prevent accidentally pressing the x button and deleting an image.

The problem is that the checkbox name, upon posting it gets converted from (for instance)

"Image.jpg" to "Image_jpg"

When i test it with print_r($_POST);

Should I create code for replacing _jpg or _gif into .jpg and .gif after the post or is there a way to make it possible to use period signs in input names?

Any help is greatly appreciated.

+1  A: 

This is standard PHP behaviour. More info here.

Doing a manual str_replace on _jpg or _gif is a bad idea because those strings could also appear within a file name:

winter_impression_jpg_strong_colours.jpg

In that case, the file name would become unusable.

You could alternatively either

  • Mask the dots using some other combination of characters (e.g. ___DOT___)

  • Change the logic: Have numbered fields (checkbox_1 checkbox_2....) and store the name in a separate field (checkbox_1_name = "image.jpg")

Pekka
Thanks a lot for the quick reply. It's true that a replace could change file names like that, so it would be better to do a replace just at the 4 last characters. However, as someone suggested, a hidden field would do the trick and work well with the code i have so far since values can have dots in them.
Rakoon
@Rakoon yup, a hidden field will do fine. (That's what I meant by changing the logic.)
Pekka
A: 

Rather than using input type="image" use the normal img tag and place the x image in that and then modify your code as per those changes otherwise you can simply replace the underscore with a dot.

Sarfraz
Thanks for the quick reply. Decided to go for a solution with hidden inputs.
Rakoon
A: 

I would recommend changing the structure of your form to have a field for confirmation (the checkbox) one field for the name (hidden) and your submission button (image):

<input type="checkbox" name="confirmation" />
<input type="hidden" name="target" value="file.jpg" />
<input type="image" />
icio
The hidden field is a great idea. I feel kinda bad for not thinking of that myself. Think I'll use that.Thanks a lot for the reply.
Rakoon