| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#11
| |||
| |||
| Micah Gersten wrote: > You cannot have anything in the brackets for the name in a checkbox > group. The brackets specify that it is an array. The name of the array > is the key in $_POST that contains the values of the checkbox group that > were checked. You can have as many groups as you like. Eh? Course you can. <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="checkbox" name="data[field_name][]" id="my_checkbox_1" value="1" > Val 1 <input type="checkbox" name="data[field_name][]" id="my_checkbox_2" value="2" > Val 2 <input type="checkbox" name="data[field2][]" id="field1" value="1" > Val 1 (Field 1) <input type="checkbox" name="data[field2][]" id="field2" value="2" > Val 2 (Field 2) <input type="submit"> </form> <pre> <?php print_r($_POST); You end up with a multi-dimensional array. Array ( [data] => Array ( [field_name] => Array ( [0] => 1 ) [field2] => Array ( [0] => 1 [1] => 2 ) ) ) -- Postgresql & php tutorials http://www.designmagick.com/ |
|
#12
| |||
| |||
| On Wed, 27 Aug 2008 22:25:44 -0500, Micah Gersten wrote: >You cannot have anything in the brackets for the name in a checkbox >group. [...] Bollocks. <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post"> <p>option 1 - colour: <input type="text" name="options[colour]"/></p> <p>option 2 - flavour: <input type="text" name="options[flavour]"/></p> <p>option 3 - size: <input type="text" name="options[size]"/></p> <p><input type="submit"/></p> </form> <?php function test() { $colour = $_POST['options']['colour']; echo "<p>colour: $colour</p>\n"; $flavour = $_POST['options']['flavour']; echo "<p>flavour: $flavour</p>\n"; $size = $_POST['options']['size']; echo "<p>size: $size</p>\n"; } ?> NB: no quotes around array key! I found this very handy for having variable product options on a simple shopping cart. -- Ross McKay, Toronto, NSW Australia "The lawn could stand another mowing; funny, I don't even care" - Elvis Costello |
|
#13
| |||
| |||
| More specifically: <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post"> <p>option 1 - colour:<br/> # <input type="checkbox" name="options[colour][]" value="red"/> red<br/> # <input type="checkbox" name="options[colour][]" value="green"/> green<br/> # <input type="checkbox" name="options[colour][]" value="blue"/> blue</p> <p><input type="submit"/></p> </form> <?php foreach($_POST['options']['colour'] as $colour) echo "<p>colour: $colour</p>\n"; ?> -- Ross McKay, Toronto, NSW Australia "If ye cannae see the bottom, dinnae complain if ye droon" - The Wee Book of Calvin |
|
#14
| |||
| |||
| In writing the script, though, there are two points. I don't always use checkboxes, sometimes I just want to update all the records on the form. Eg I have a series of images and related text each with their ID in the database table, on clicking update all the form elements get submitted and I need to relate the relevant image, text, etc with the table ID. So I can't rely on checkboxes which only get posted if ticked. So again I have to iterate through all possible IDs. Which normally can be done but it is longer, eg because the images on the page in the first place may not be the result of a simple select query. So I suppose the solution there is to have a hidden field with all the IDs as a string, explode that and then iterate through that. Eg <input type=hidden value="1_2_3" name="all_IDs"> $IDs=explode("_",$_POST['all_IDs']); and that gives me the table IDs to do update queries on etc. John ioannes wrote: > Actually, you are right, as you just put the checkbox index in the > POST and get the value from there. So you just need the number of > checkboxes...sorry. > > ioannes wrote: >> Yes, Tedd, this does however incur the overhead of find out what i >> is, because it could be a range of IDs from the database, not >> necessarily a count of the checkboxes on the page: >> >> " >> for ($i = 1; $i <= 4; $i++) >> { >> $a = 'a' . $i; >> $b = 'whatever' . $i; >> if($_POST[$a] == 'on') >> { >> my_array[] = $_POST[$b] >> } >> } >> " >> >> John >> > |
|
#15
| |||
| |||
| That solution would probably work just nicely. That being said however, If you want to be sophisticated about this you would make good use of JSON and php's corresponding functions json_decode/encode which is common way to transport arrays as strings - and much more. -----Original Message----- From: ioannes [mailto:ioannes@btinternet.com] Sent: Friday, 29 August 2008 1:22 PM To: php-general@lists.php.net Subject: Re: [php] Re: Variable name as a string In writing the script, though, there are two points. I don't always use checkboxes, sometimes I just want to update all the records on the form. Eg I have a series of images and related text each with their ID in the database table, on clicking update all the form elements get submitted and I need to relate the relevant image, text, etc with the table ID. So I can't rely on checkboxes which only get posted if ticked. So again I have to iterate through all possible IDs. Which normally can be done but it is longer, eg because the images on the page in the first place may not be the result of a simple select query. So I suppose the solution there is to have a hidden field with all the IDs as a string, explode that and then iterate through that. Eg <input type=hidden value="1_2_3" name="all_IDs"> $IDs=explode("_",$_POST['all_IDs']); and that gives me the table IDs to do update queries on etc. John ioannes wrote: > Actually, you are right, as you just put the checkbox index in the > POST and get the value from there. So you just need the number of > checkboxes...sorry. > > ioannes wrote: >> Yes, Tedd, this does however incur the overhead of find out what i >> is, because it could be a range of IDs from the database, not >> necessarily a count of the checkboxes on the page: >> >> " >> for ($i = 1; $i <= 4; $i++) >> { >> $a = 'a' . $i; >> $b = 'whatever' . $i; >> if($_POST[$a] == 'on') >> { >> my_array[] = $_POST[$b] >> } >> } >> " >> >> John >> > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.