how do I get checkbox values into an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
metuz
Forum Newbie
Posts: 4
Joined: Wed May 15, 2002 5:03 am
Location: sweden
Contact:

how do I get checkbox values into an array

Post by metuz »

got a bit of a problem, I use opendir() and readdir() to get a listing
of the contents in a directory, the echo'ing it out into a table formated
page with a checkbox infront of every item , the name of the checkboxes
are generated with the $file value I get from readdir(), so every item
listed should get a uniqe name, now the problem appaers, I want to gather
the values of all the checkboxes that are marked and loop them into an
array so I can use the array in a unlink() function. Am I making any sense at all?
:wink:
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Yep, perfect sense.

When you call your next script the server makes available $HTTP_SERVER_VARS (Top of my head that is what it is called, call a script with phpinfo(); in it to see all the server variables).

This is an array of all variables passed, so just loop through this array picking out the values you want.

Make sure your check boxes are named so that you can easily find the checkboxes rather than another field, for instance

name your checkboxes chk-filename-file, you then loop through the SERVER_VARS array picking up everything beginning with 'chk' and ending in 'file' using string functions. When you find one use the explode() function passing a '-' as the seperator. Your filename will then be in the second array item.

Mike
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Probably didn't make sense so here is an example

Code: Select all

<?php
/**
 *
 * @version v1, 17/05/2002
 */
    while ($ServerArray = each($HTTP_POST_VARS))&#123;
           //check for different field types
           //each field has a name like chkfile-filename
         if (StrPos($ServerArray&#1111;'key'],'chkfile')===0)&#123;
             $FieldName = $ServerArray&#1111;'key'];
             $NameArray = explode("-",$FieldName);
             //Filename will always be in array&#1111;1]
             $FileNameToProcess = $NameArray&#1111;1];
             if ($FileNameToProcess)&#123;
                 ...do work on file...;
             &#125;
         &#125;
	&#125;


?>
Mike
User avatar
cwcollins
Forum Commoner
Posts: 79
Joined: Thu May 16, 2002 3:51 pm
Location: Milwaukee, WI, USA

Re: how do I get checkbox values into an array

Post by cwcollins »

another way, maybe easier to understand...

Each of the check boxes should look something like this:

Code: Select all

<input type=checkbox name=FilesToDelete&#1111;] value=$filename>
All of the checkboxes will have the exact same name.... notice the [] after the name of the checkbox? This tell PHP that this is an array, and sets the value of the next array item to the value of the checkbox (i.e. filename) if it is checkedS... make sense?
if you use the GET method, your address bar will look something like:

Code: Select all

http://what.com/test.html?FilesToDelete%5B%5D=file1.ext&FilesToDelete%5B%5D=file2.ext&FilesToDelete%5B%5D=&FilesToDelete%5B%5D=file4.ext
a little more readable:

Code: Select all

http://what.com/test.html?FilesToDelete&#1111;]=file1.ext&FilesToDelete&#1111;]=file2.ext&FilesToDelete&#1111;]=&FilesToDelete&#1111;]=file4.ext
PHP sees:

Code: Select all

$FilesToDelete&#1111;]="file1.ext";
$FilesToDelete&#1111;]="file2.ext";
$FilesToDelete&#1111;]="";
$FilesToDelete&#1111;]="file4.ext";
and you get:

Code: Select all

$FilesToDelete&#1111;0]="file1.ext";
$FilesToDelete&#1111;1]="file2.ext";
$FilesToDelete&#1111;2]="file4.ext";
this is the way i deal with checkboxes in this kind of situation, but as usual, there is more than one way to do it.

c.w.collins
Post Reply