![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi Again, Im a newbie at PHP, so please have patience with another PHP problem. Here is the problem portion of the code: $vendorList = array('Cisco','SUN','Juniper','IBM RS/6000','IBM AS/400'); // array of available vendors if (isset($_POST['brandInquiry'])) { // if form has been refreshed for errors, re- check those items previously selected foreach ($vendorList as $value) { // go through all vendors if ($brandInquiry['value']) { // if this vendor is located in the array, re-check it echo "<input type=\"checkbox\" name=\"brandInquiry[]\" value=\"selling\" checked=\"checked\"> $value \n"; } else { // else it was never checked, so leave blank echo "<input type=\"checkbox\" name=\"brandInquiry[]\" value=\"selling\"> $value \n"; } } What I want to happen ::: $brandInquiry is an array of selected vendors. What I'd like to do is create a "sticky" form that remembers which vendors the user checked, even if the page needs to refresh to hilite errors in the rest of the form. What I thought to do is run a comparison between $brandInquiry and $vendorList. Whichever items in $vendorList that match up to $brandInquiry should be checked, and any other values in $vendorList should be unchecked. What actually happens:::: on refresh ALL the items in $vendorList show up as checked off, no matter which ones I originally checked. Any ideas? Thanks very much! Jacob |
#3
| |||
| |||
|
|
OK! so far....mistake number 1 is I forgot to change the "selling" value to $value...... Im sure there are plenty more errors ![]() |
#4
| |||
| |||
|
|
.oO(Jacob 99) OK! so far....mistake number 1 is I forgot to change the "selling" value to $value...... Im sure there are plenty more errors ![]() A slightly different approach: $vendorList = array('Cisco', 'SUN', 'Juniper', 'IBM RS/6000', 'IBM AS/400'); // $brandInquiry contains the submitted values if available, else an empty array $brandInquiry = isset($_POST['brandInquiry']) ? $_POST['brandInquiry'] : array(); // Now simply loop through the vendor list, print out the checkboxes and // test if a box was previously checked, i.e. if its value can be found // in the array $brandInquiry. for ($i = 0; $i < count($vendorList); $i++) { printf('<label><input type="checkbox" name="brandInquiry[]" value="%u"%s> %s</label>'."\n", $i, in_array($i, $brandInquiry) ? ' checked="checked"' : '', $vendorList[$i]); } http://www.php.net/printf http://www.php.net/sprintf http://www.php.net/in_array Maybe it's of some help. Micha |
#5
| |||
| |||
|
|
......although, I dont understand how you did this....in fact, being very fresh to PHP, I dont understand most of the symbols at all. You replaced my 20 lines of IF...ELSE script with 5 small lines.....ach, my head hurts. |
#6
| |||
| |||
|
|
.oO(Jacob 99) ......although, I dont understand how you did this....in fact, being very fresh to PHP, I dont understand most of the symbols at all. You replaced my 20 lines of IF...ELSE script with 5 small lines.....ach, my head hurts. ;D I used only two "special" features to keep the code small: 1) The printf() function This is very useful to output strings which contain variables. The first parameter is the string itself, containing placeholders (these %s and %u things). The remaining parameters are the variables, which are then used instead of the placeholders in the order they appear. So the first %u is replaced with $i, the next %s is replaced with either checked="checked" or nothing (see below), the last %s is replaced with the vendor name. See the manual on sprintf() for details. http://www.php.net/sprintf 2) The ternary operator ?: This nice little thingy works like an if-else statement, but is more flexible. It acts more like a function with a return value (returns one of two possible values, dependent on a condition), so it can be used in places where if-else is not allowed, for example to calculate a value in a printf() call. It's described in the chapter "Comparison Operators". http://www.php.net/manual/en/language.operators.comparison.php Micha |
#7
| |||
| |||
|
|
Well, it works great for display, but I've found a large problem: instead of the "value" attribute being the name of the vendor, it is the index# where that vendor name is in the array (ie. instead of saying 'IBM', 'SUN' it says '1','2')....since im still anayzing the code, do you know how I can get the "real" value for the value attribute? Thanks Jacob "Michael Fesser" <netizen (AT) gmx (DOT) net> wrote in message news:jcpqf0tgip8ckf3hjla1ilogcooc7dnspl (AT) 4ax (DOT) com... .oO(Jacob 99) ......although, I dont understand how you did this....in fact, being very fresh to PHP, I dont understand most of the symbols at all. You replaced my 20 lines of IF...ELSE script with 5 small lines.....ach, my head hurts. ;D I used only two "special" features to keep the code small: 1) The printf() function This is very useful to output strings which contain variables. The first parameter is the string itself, containing placeholders (these %s and %u things). The remaining parameters are the variables, which are then used instead of the placeholders in the order they appear. So the first %u is replaced with $i, the next %s is replaced with either checked="checked" or nothing (see below), the last %s is replaced with the vendor name. See the manual on sprintf() for details. http://www.php.net/sprintf 2) The ternary operator ?: This nice little thingy works like an if-else statement, but is more flexible. It acts more like a function with a return value (returns one of two possible values, dependent on a condition), so it can be used in places where if-else is not allowed, for example to calculate a value in a printf() call. It's described in the chapter "Comparison Operators". http://www.php.net/manual/en/language.operators.comparison.php Micha |
#8
| ||||
| ||||
|
|
Well, it works great for display, but I've found a large problem: instead of the "value" attribute being the name of the vendor, it is the index# where that vendor name is in the array (ie. instead of saying 'IBM', 'SUN' it says '1','2') |
|
....since im still anayzing the code, do you know how I can get the "real" value for the value attribute? |
|
...and another strange thing happens. When the user submits the form, no matter what is checked before they submit it it will reset to check only the first item. |
|
Michael, can I send you the link by email so you can see the behaviour yourself? |
#9
| |||
| |||
|
|
.oO(Jacob F.) Well, it works great for display, but I've found a large problem: instead of the "value" attribute being the name of the vendor, it is the index# where that vendor name is in the array (ie. instead of saying 'IBM', 'SUN' it says '1','2') Yep, whenever possible I try to use numeric IDs instead of literal values, IMHO they are easier to handle, more reliable and save resources. So I used them and it works, at least in this short out-of-context checkbox snippet. But OK, it's quite possible that it causes problems with the rest of your script. ....since im still anayzing the code, do you know how I can get the "real" value for the value attribute? Three changes to the printf() call: value="%u" --> value="%s" $i, --> $vendorList[$i], in_array($i, --> in_array($vendorList[$i], The first two changes affect the output of the checkboxes, the value will now be the vendor name instead of the ID. The third modifies the test whether a box was checked or not, it now tests on the vendor name. That should do it. To your other problem: ...and another strange thing happens. When the user submits the form, no matter what is checked before they submit it it will reset to check only the first item. Probably a "conflict" between the checkbox snippet and the rest of your form script, as mentioned above. Michael, can I send you the link by email so you can see the behaviour yourself? Yep. But I would have to look at the source code if possible, the output in the browser is not really helpful. You can either mail me the source or (better) store it under a different name on the server (preferred extension is .phps), so that the server won't interpret it. Micha |
#10
| |||
| |||
|
|
Thank you for replying. There are 2 parts so far; the php form handler http://www.jdfmultimedia.com/file_backups/client_pdanorth/site/getquote.phps and the form itself http://www.jdfmultimedia.com/file_backups/client_pdanorth/site/_content/getquote.inc Im still cleaning them up, so I realize there are loads of inefficiencies, but Im still learning so I wrote everything in an easy to understand fashion. Thanks so much for looking through this and helping me understand, Jacob "Michael Fesser" <netizen (AT) gmx (DOT) net> wrote in message news:spcrf0d40ktelhhqrfe86631p8rr2tie11 (AT) 4ax (DOT) com... .oO(Jacob F.) Well, it works great for display, but I've found a large problem: instead of the "value" attribute being the name of the vendor, it is the index# where that vendor name is in the array (ie. instead of saying 'IBM', 'SUN' it says '1','2') Yep, whenever possible I try to use numeric IDs instead of literal values, IMHO they are easier to handle, more reliable and save resources. So I used them and it works, at least in this short out-of-context checkbox snippet. But OK, it's quite possible that it causes problems with the rest of your script. ....since im still anayzing the code, do you know how I can get the "real" value for the value attribute? Three changes to the printf() call: value="%u" --> value="%s" $i, --> $vendorList[$i], in_array($i, --> in_array($vendorList[$i], The first two changes affect the output of the checkboxes, the value will now be the vendor name instead of the ID. The third modifies the test whether a box was checked or not, it now tests on the vendor name. That should do it. To your other problem: ...and another strange thing happens. When the user submits the form, no matter what is checked before they submit it it will reset to check only the first item. Probably a "conflict" between the checkbox snippet and the rest of your form script, as mentioned above. Michael, can I send you the link by email so you can see the behaviour yourself? Yep. But I would have to look at the source code if possible, the output in the browser is not really helpful. You can either mail me the source or (better) store it under a different name on the server (preferred extension is .phps), so that the server won't interpret it. Micha |
![]() |
| Thread Tools | |
| Display Modes | |
| |