Re: [PHP] How to pass unknown number of variables to a function?
Thank's for the help everybody! I used something different (a little like this nice one from Terence (thank you!) but with JS: I named my form 'test' and included a hidden field 'collector'. At the checkboxes the name is the value I need. The box, when checked, gets the value 1. In the boxtags I added: onclick=add_values($name) where name is the value I want. Then I wrote this little script: $js = script type=text/javascript function add_values(val) { document.test.'collector'.value += val; document.test.'collector'.value += \,\; } /script; It produces a comma separated list in the field 'collector' whith only the checked values. When sending the form, only this field is transmitted. This is just what I wanted and it works fine. Uwe Terence Kearns schrieb in Nachricht [EMAIL PROTECTED]... This is a nice easy one :) It couldn't be simpler. Just put empty square brackets (as used in arrays) in front of your checkbox name. The example below assumes you have PHP 4.1 or greater (which uses $_POST to contain form posted data) notice how name=ck[] html head titleUntitled/title /head body ?php if(count($_POST)) { foreach ($_POST[ck] as $varval) { print(div.$varval./div); } } ? form action=test.php method=post input type=checkbox name=ck[] value=val1br input type=checkbox name=ck[] value=val2br input type=checkbox name=ck[] value=val3br input type=checkbox name=ck[] value=val4br input type=checkbox name=ck[] value=val5br input type=submit /form /body /html in earlier versions of PHP, $_POST[ck] would be equal to $ck (although, the former is safer) On Thu, 4 Jul 2002 10:52:37 +0200, Uwe Birkenhain [EMAIL PROTECTED] said: Hi everybody on this rainy morning! My problem: I give the user a form with a list of options with checkboxes. The list is long and not predictable - the values change always. I want to put the checked values (or all, that doesn't matter) in an array, to pass it to my function. How can I write those keys and values in an array? I suppose it will be best done with JS - but since I don't know JS ... Has anybody a ready solution? Thank's a lot! Uwe -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php [TK] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to pass unknown number of variables to a function?
On Thursday 04 July 2002 16:52, Uwe Birkenhain wrote: Hi everybody on this rainy morning! My problem: I give the user a form with a list of options with checkboxes. The list is long and not predictable - the values change always. I want to put the checked values (or all, that doesn't matter) in an array, to pass it to my function. How can I write those keys and values in an array? I suppose it will be best done with JS - but since I don't know JS ... Has anybody a ready solution? Search archives for checkbox array -- Jason Wong - Gremlins Associates - www.gremlins.com.hk Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * /* For your penance, say five Hail Marys and one loud BLAH! */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to pass unknown number of variables to a function?
Uwe Birkenhain wrote: Hi everybody on this rainy morning! Rainy!? It's damn hot in Kiev :) My problem: I give the user a form with a list of options with checkboxes. The list is long and not predictable - the values change always. I want to put the checked values (or all, that doesn't matter) in an array, to pass it to my function. How can I write those keys and values in an array? Suppose you have a number of checkboxes. First, you name them sequentially (like active_0,active_1 and so on) and you assign it your value. Then you process posted data like this. BEWARE! you need at least an array of hidden variables to know exactly how many checkboxes you must process. Then you just rebuild it the structure and pass it thru. # put the received input into an ordered structure ## $count = 0; while ( isset($id[$count]) ) { # Build variable names; ### $w_active = active_.$count; # get actual form values $active[$count] = $$w_active; $count++; } Hope it solves the problem. I've been using this for ages and it never gave me problems. Alberto Kiev -- -_=}{=_-@-_=}{=_--_=}{=_-@-_=}{=_--_=}{=_-@-_=}{=_--_=}{=_- LoRd, CaN yOu HeAr Me, LiKe I'm HeArInG yOu? lOrD i'M sHiNiNg... YoU kNoW I AlMoSt LoSt My MiNd, BuT nOw I'm HoMe AnD fReE tHe TeSt, YeS iT iS ThE tEsT, yEs It Is tHe TeSt, YeS iT iS ThE tEsT, yEs It Is... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] How to pass unknown number of variables to a function?
Hi Uwe, If you define the checkbox list as an array (OptionList[] in the example below), only the checked values will be returned as an array $OptionList when you post the form. You can then use this as an array or pass it as an array to a function in the script: ? $i = 0; while (isset($OptionList[$i])) { echo You have chosen option .$OptionList[$i].br; $i++; } ? html head titleUntitled Document/title meta http-equiv=Content-Type content=text/html; charset=iso-8859-1 /head body bgcolor=#FF text=#00 form name=form1 method=post action=test.php input type=checkbox name=OptionList[] value=something1Option 1/inputbr input type=checkbox name=OptionList[] value=something2Option 2/inputbr input type=checkbox name=OptionList[] value=something3Option 3/inputbr input type=submit name=Submit value=Test /form /body /html Regards Rudolf Visagie Principal Software Developer Digital Healthcare Solutions -Original Message- From: Uwe Birkenhain [mailto:[EMAIL PROTECTED]] Sent: Thursday, July 04, 2002 10:53 AM To: [EMAIL PROTECTED] Subject: [PHP] How to pass unknown number of variables to a function? Hi everybody on this rainy morning! My problem: I give the user a form with a list of options with checkboxes. The list is long and not predictable - the values change always. I want to put the checked values (or all, that doesn't matter) in an array, to pass it to my function. How can I write those keys and values in an array? I suppose it will be best done with JS - but since I don't know JS ... Has anybody a ready solution? Thank's a lot! Uwe -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to pass unknown number of variables to a function?
This is a nice easy one :) It couldn't be simpler. Just put empty square brackets (as used in arrays) in front of your checkbox name. The example below assumes you have PHP 4.1 or greater (which uses $_POST to contain form posted data) notice how name=ck[] html head titleUntitled/title /head body ?php if(count($_POST)) { foreach ($_POST[ck] as $varval) { print(div.$varval./div); } } ? form action=test.php method=post input type=checkbox name=ck[] value=val1br input type=checkbox name=ck[] value=val2br input type=checkbox name=ck[] value=val3br input type=checkbox name=ck[] value=val4br input type=checkbox name=ck[] value=val5br input type=submit /form /body /html in earlier versions of PHP, $_POST[ck] would be equal to $ck (although, the former is safer) On Thu, 4 Jul 2002 10:52:37 +0200, Uwe Birkenhain [EMAIL PROTECTED] said: Hi everybody on this rainy morning! My problem: I give the user a form with a list of options with checkboxes. The list is long and not predictable - the values change always. I want to put the checked values (or all, that doesn't matter) in an array, to pass it to my function. How can I write those keys and values in an array? I suppose it will be best done with JS - but since I don't know JS ... Has anybody a ready solution? Thank's a lot! Uwe -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php [TK] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php