Re: Form Processing Question - Empty Fields

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Tijnema wrote:
On 5/23/07, Stephen <stephen-d@xxxxxxxxxx> wrote:
I have a script to process the post from a form.

The form is used to upload photos and has fields for the filename, alt text, caption and a checkbox to indicate if this photo is the main page image rotation.

I wanted to build a general routine to build the form to allow for a varying number of photos to upload.

 I use main_image[] for the various checkboxes for each photo.

I see that the checkbox is *not* set, I get nothing back, and if the second photo is checked (and the first not checked), I get main_image[0] = 'on'.

 And I see this happens for the caption[] array as well.

 This seems to me to be pretty ugly.

 Am I understanding this properly?

 Do I really have to use main_image1, main_image2....

 Thanks
 Stephen


No, you don't need to, it's the way you write your HTML form.

Again, this is Off topic, as this has nothing to do with PHP, but
well, we are here to help people in general.

Worryingly I agree with Greg here - this is on-topic because it relates to the way PHP processes GET and POST vars.

As you didn't provided your code, I'm giving a general example which
worked for my photo site, and should work for your example too. You
should replace <foto_id_here> with the ID of the photo you want to be
checked.

<input type='checkbox' name='photos[]' value="<photo_id_here>"/>

And now $_POST['photos'] will be an array with the values being the
IDs of the checked photos.

That is indeed the solution, but I think it's important to explain why this is happening.

Take the example where you have 3 checkboxes, named photo1, photo2 and photo3. Say photo2 and photo3 are checked, the POST data will look like this...

photo2=on&photo3=on

Now consider the same 3 checkboxes, but all named photos[]. Since the browser knows nothing about the square brackets, it creates the POST data in the same way...

photos[]=on&photos[]=on

Now you see the problem. Nothing in that data indicates which checkboxes were checked. By doing what Tijnema suggests you would end up with...

photos[]=2&photos[]=3

Hopefully that explains why what you've implemented is doing what it's doing and will help you avoid problems like this in future.

-Stut

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux