Re: Re: Variable name as a string
| From: | tedd | Date: | Wed, 27 Aug 2008 23:03:59 +0000 |
| Subject: | Re: Re: Variable name as a string | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-278846@lists.php.net to get a copy of this message | ||
At 12:07 AM +0200 8/28/08, Maciek Sokolewicz wrote:
tedd wrote:Yeah, I remember that -- but a bit different. Don't use indexes, but rather just my_checkboxes[] and on the php side $my_checked_checkboxes = $_REQUEST['my_checkboxes']; The array $my_checked_checkboxes equals the $_REQUEST$_/$_POST/$_GET array -- all the indexes will match (i.e., $my_checked_checkboxes[3] is the same as $_POST[3]). The only problem I have with that method is that the [] becomes confusing with dealing with javascript that can also handles the form. One of the ways to get around this is to: <input type="checkbox" name="my_checkboxes[]" id="my_checkbox_1" value="1" > That way php will use "name" and javascript will use "id". But, there are lot's of ways to do this. Cheers, tedd -- ------- http://sperling.com http://ancientstones.com http://earthstones.comAt 1:58 PM -0500 8/27/08, Shawn McKenzie wrote:Well, this seems easier/cleaner to me: <input type="check" name="my_checkboxes[1]" value="1" /> 1<br /> <input type="check" name="my_checkboxes[2]" value="1" /> 1<br /> <input type="check" name="my_checkboxes[3]" value="1" /> 1<br /> <input type="check" name="my_checkboxes[4]" value="1" /> 1<br /> $my_checked_checkboxes = $_REQUEST['my_checkboxes']; // whichever you wish, $_GET or $_POST, I don't care right now; you choose.ioannes wrote:It's not over complicated, but just a method of passing checked checkbox values to a php array. Do you have something better? Cheers, teddEither I'm missing what you're trying to do, or this has become incredibly over complicated! -ShawnActually, 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. for ($i = 1; $i <= 4; $i++) { $a = 'a' . $i; $b = 'whatever' . $i; if($_POST[$a] == 'on'){ my_array[] = $_POST[$b] }}