RE: [PHP] Re: Variable name as a string
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:[EMAIL PROTECTED] 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: Variable name as a string
On 28 August 2008 04:26, Micah Gersten advised: 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. So how come I have several million *working* forms that do exactly what you say I can't? (OK, so I exaggerate, but it's still significantly more than none! ;) Cheers! Mike -- Mike Ford, Electronic Information Developer, C507, Leeds Metropolitan University, Civic Quarter Campus, Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 812 4730 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: Variable name as a string
On 28 August 2008 00:04, tedd advised: At 12:07 AM +0200 8/28/08, Maciek Sokolewicz wrote: input type=check name=my_checkboxes[1] value=1 / 1br / input type=check name=my_checkboxes[2] value=1 / 1br / input type=check name=my_checkboxes[3] value=1 / 1br / input type=check name=my_checkboxes[4] value=1 / 1br / $my_checked_checkboxes = $_REQUEST['my_checkboxes']; // whichever you wish, $_GET or $_POST, I don't care right now; you choose. 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. Why??? form name=my_form ... input type=checkbox name=my_checkboxes[] ... /form script language=Javascript checkboxes = document.my_form[my_checkboxes[]]; /script Cheers! Mike -- Mike Ford, Electronic Information Developer, C507, Leeds Metropolitan University, Civic Quarter Campus, Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 812 4730 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: Variable name as a string
At 11:50 AM +0100 8/28/08, Ford, Mike wrote: On 28 August 2008 00:04, tedd advised: 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. Why??? form name=my_form ... input type=checkbox name=my_checkboxes[] ... /form script language=Javascript checkboxes = document.my_form[my_checkboxes[]]; /script Cheers! Mike Mike: There is no Why?, this is just another way to do it. In fact, there are other ways to accomplish this than what both of us have shown. What's nice about the technique I described here, at least for me, is that the value attribute applies to both name and id. PHP uses name and javascript uses id -- it's simple. However, I've been leaning to using getElementById() to create unobtrusive javascript, which can be used to isolate and use name as well, thus reducing the html code. I think my next demo will be to show these various techniques. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: Variable name as a string
The reason why... (I think) Id defines the element as a part of the DOM - it is the identifier of the element Name tells the browser the name of the parameter (i.e. the mane to associate with the value), not of the element. PHP and JS are looking for two different things - one wants the element, one the parameter - so it makes sense (sort of) to use two different terms. Simcha Younger -Original Message- From: tedd [mailto:[EMAIL PROTECTED] Sent: Thursday, August 28, 2008 4:40 PM To: php-general@lists.php.net Subject: RE: [PHP] Re: Variable name as a string At 11:50 AM +0100 8/28/08, Ford, Mike wrote: On 28 August 2008 00:04, tedd advised: 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. Why??? form name=my_form ... input type=checkbox name=my_checkboxes[] ... /form script language=Javascript checkboxes = document.my_form[my_checkboxes[]]; /script Cheers! Mike Mike: There is no Why?, this is just another way to do it. In fact, there are other ways to accomplish this than what both of us have shown. What's nice about the technique I described here, at least for me, is that the value attribute applies to both name and id. PHP uses name and javascript uses id -- it's simple. However, I've been leaning to using getElementById() to create unobtrusive javascript, which can be used to isolate and use name as well, thus reducing the html code. I think my next demo will be to show these various techniques. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php No virus found in this incoming message. Checked by AVG - http://www.avg.com Version: 8.0.138 / Virus Database: 270.6.10/1638 - Release Date: 27/08/2008 19:06 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
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
Re: [PHP] Re: Variable name as a string
Perhaps this example may help. Eg: a form with checkboxes and submit button, a few are checked and I want to delete the corresponding records from the database. The database table has an ID column: for each ($argv as $key=$value) { //$key is named cb_1 $value is checked //to get 1 from the key name, then //needed: function like nameof() $var=nameof($key); $ID=substr($var,3); $query=delete * from dbtable where ID='$ID'; //etc } For database use I do the following in forms: name=data[field_name] Or for multiple table that you update together: name=data[table_name][field_name] -Shawn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Variable name as a string
At 4:55 PM +0100 8/27/08, JOHN DILLON wrote: Perhaps this example may help. Eg: a form with checkboxes and submit button, a few are checked and I want to delete the corresponding records from the database. The database table has an ID column: for each ($argv as $key=$value) { //$key is named cb_1 $value is checked //to get 1 from the key name, then //needed: function like nameof() $var=nameof($key); $ID=substr($var,3); $query=delete * from dbtable where ID='$ID'; //etc } Okay, your data is coming in from a form and you want to translate that data to a php array -- here's how to do it: In your form you use: input type=checkbox name=a1 input type=hidden name=whatever1 value=103 input type=checkbox name=a2 input type=hidden name=whatever2 value=206 input type=checkbox name=a3 input type=hidden name=whatever3 value=1187 input type=checkbox name=a4 input type=hidden name=whatever4 value=6101 In your receiving php script, you use: for ($i = 1; $i = 4; $i++) { $a = 'a' . $i; $b = 'whatever' . $i; if($_POST[$a] == 'on') { my_array[] = $_POST[$b] } } If a user clicks any/all of the checkboxes, then those checkboxes will be turned 'on' and the values associated with the hidden fields will come into play and be recorded in the my_array[]. A count(my_array) will provide you with the number of checkboxes that were actually checked. Sure you can do this in while statements if you wish, but the idea of how to translate checkboxes to a php array is here. The hidden values above could just as easily be values taken from a database corresponding to record deletions, such as: input type=hidden name=whatever4 value=?php echo($record_ID);? The point is that you can determine what the user clicked and tied it to whatever you presented. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Variable name as a string
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
Re: [PHP] Re: Variable name as a string
At 7:08 PM +0100 8/27/08, 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 John: Yes, and I thought that I showed you how to handle that -- that an easy thing to do. You simply list all the items you want to expose to the user for the user's consideration to delete. Then you accept what the user has selected and delete them accordingly. (However, you should work out a way to clean this information before doing anything). I only added the count thing IF you wanted to know how many deletions the user selected. Please review what I said and consider. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Variable name as a string
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
Re: [PHP] Re: Variable name as a string
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 Either I'm missing what you're trying to do, or this has become incredibly over complicated! -Shawn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Variable name as a string
At 1:58 PM -0500 8/27/08, Shawn McKenzie wrote: 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. for ($i = 1; $i = 4; $i++) { $a = 'a' . $i; $b = 'whatever' . $i; if($_POST[$a] == 'on') { my_array[] = $_POST[$b] } } John Either I'm missing what you're trying to do, or this has become incredibly over complicated! -Shawn It's not over complicated, but just a method of passing checked checkbox values to a php array. Do you have something better? Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Variable name as a string
tedd wrote: At 1:58 PM -0500 8/27/08, Shawn McKenzie wrote: 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. for ($i = 1; $i = 4; $i++) { $a = 'a' . $i; $b = 'whatever' . $i; if($_POST[$a] == 'on') { my_array[] = $_POST[$b] } } John Either I'm missing what you're trying to do, or this has become incredibly over complicated! -Shawn It's not over complicated, but just a method of passing checked checkbox values to a php array. Do you have something better? Cheers, tedd Well, this seems easier/cleaner to me: input type=check name=my_checkboxes[1] value=1 / 1br / input type=check name=my_checkboxes[2] value=1 / 1br / input type=check name=my_checkboxes[3] value=1 / 1br / input type=check name=my_checkboxes[4] value=1 / 1br / $my_checked_checkboxes = $_REQUEST['my_checkboxes']; // whichever you wish, $_GET or $_POST, I don't care right now; you choose. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Variable name as a string
First, the type is checkbox, not check. Second, you cannot put a value in the brackets for a checkbox group. A checkbox group is passed to PHP automatically as an array. Thank you, Micah Gersten onShore Networks Internal Developer http://www.onshore.com Maciek Sokolewicz wrote: Well, this seems easier/cleaner to me: input type=check name=my_checkboxes[1] value=1 / 1br / input type=check name=my_checkboxes[2] value=1 / 1br / input type=check name=my_checkboxes[3] value=1 / 1br / input type=check name=my_checkboxes[4] value=1 / 1br / $my_checked_checkboxes = $_REQUEST['my_checkboxes'];// whichever you wish, $_GET or $_POST, I don't care right now; you choose. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Variable name as a string
At 12:07 AM +0200 8/28/08, Maciek Sokolewicz wrote: tedd wrote: At 1:58 PM -0500 8/27/08, Shawn McKenzie wrote: 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. for ($i = 1; $i = 4; $i++) { $a = 'a' . $i; $b = 'whatever' . $i; if($_POST[$a] == 'on') { my_array[] = $_POST[$b] } } Either I'm missing what you're trying to do, or this has become incredibly over complicated! -Shawn It's not over complicated, but just a method of passing checked checkbox values to a php array. Do you have something better? Cheers, tedd Well, this seems easier/cleaner to me: input type=check name=my_checkboxes[1] value=1 / 1br / input type=check name=my_checkboxes[2] value=1 / 1br / input type=check name=my_checkboxes[3] value=1 / 1br / input type=check name=my_checkboxes[4] value=1 / 1br / $my_checked_checkboxes = $_REQUEST['my_checkboxes']; // whichever you wish, $_GET or $_POST, I don't care right now; you choose. 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.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Variable name as a string
tedd wrote: At 1:58 PM -0500 8/27/08, Shawn McKenzie wrote: 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. for ($i = 1; $i = 4; $i++) { $a = 'a' . $i; $b = 'whatever' . $i; if($_POST[$a] == 'on') { my_array[] = $_POST[$b] } } John Either I'm missing what you're trying to do, or this has become incredibly over complicated! -Shawn It's not over complicated, but just a method of passing checked checkbox values to a php array. Do you have something better? Cheers, tedd I guess you missed my last post, it was maybe the first or second reply to this thread: input type=checkbox name=data[field_name] id=my_checkbox_1 value=1 Then on post you have a $_POST['data'] array that contains the field_names as keys and the checkbox values as values. That's why I was wondering if I was missing something. -Shawn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Variable name as a string
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. Thank you, Micah Gersten onShore Networks Internal Developer http://www.onshore.com Shawn McKenzie wrote: I guess you missed my last post, it was maybe the first or second reply to this thread: input type=checkbox name=data[field_name] id=my_checkbox_1 value=1 Then on post you have a $_POST['data'] array that contains the field_names as keys and the checkbox values as values. That's why I was wondering if I was missing something. -Shawn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Variable name as a string
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/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Variable name as a string
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 poption 1 - colour: input type=text name=options[colour]//p poption 2 - flavour: input type=text name=options[flavour]//p poption 3 - size: input type=text name=options[size]//p pinput type=submit//p /form ?php function test() { $colour = $_POST['options']['colour']; echo pcolour: $colour/p\n; $flavour = $_POST['options']['flavour']; echo pflavour: $flavour/p\n; $size = $_POST['options']['size']; echo psize: $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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Variable name as a string
More specifically: form action=?php echo $_SERVER['SCRIPT_NAME']; ? method=post poption 1 - colour:br/ # input type=checkbox name=options[colour][] value=red/ redbr/ # input type=checkbox name=options[colour][] value=green/ greenbr/ # input type=checkbox name=options[colour][] value=blue/ blue/p pinput type=submit//p /form ?php foreach($_POST['options']['colour'] as $colour) echo pcolour: $colour/p\n; ? -- Ross McKay, Toronto, NSW Australia If ye cannae see the bottom, dinnae complain if ye droon - The Wee Book of Calvin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php