Posting Arrays
- kidcardboard
- Forum Members
- Posts: 54
- Joined: Mon Sep 28, 2009 5:25 pm
Posting Arrays
I have a form that contains two lists of check boxes for filtering. Now what I'd like to be able to do is pass all the selected values as a single array so the name for the first list would filterby[category][] and the second would be filterby[neighborhood][]. Now my problem is I can't access the values of the second level array from $params (ignore for the moment that there is no module id on those names, that isn't the issue and have been remove for legibility).
If I call the value of $params['filterby'] I get an array containing two indexes. Great that's what I'm expecting, but if I try $params['filterby']['category'] or $params['filterby']['neighborhood'] all I get returned is 'Array'. The cms seems to be to-stringing the value in the second level array.
Does anyone know a way around this or am I going to have to stick with using $_POST and sanitizing the data myself?
If I call the value of $params['filterby'] I get an array containing two indexes. Great that's what I'm expecting, but if I try $params['filterby']['category'] or $params['filterby']['neighborhood'] all I get returned is 'Array'. The cms seems to be to-stringing the value in the second level array.
Does anyone know a way around this or am I going to have to stick with using $_POST and sanitizing the data myself?
Re: Posting Arrays
You may want to do something like print_r($params['filterby']) and see exactly what is coming back. This will show you exactly how the variable is structured and you can write your code accordingly.
-S
-S
- kidcardboard
- Forum Members
- Posts: 54
- Joined: Mon Sep 28, 2009 5:25 pm
Re: Posting Arrays
It comes back exactly as I described it
Code: Select all
Array
(
[category] => Array
[neighborhood] => Array
)
Re: Posting Arrays
Curious. print_r is support to be recursive. Try
-S
Code: Select all
print_r($params['filterby'].category);
or
print_r($params['filterby']['category']);
- kidcardboard
- Forum Members
- Posts: 54
- Joined: Mon Sep 28, 2009 5:25 pm
Re: Posting Arrays
print_r($params['filterby'].category); returns
print_r($params['filterby']['category']); returns
Code: Select all
Notice: Use of undefined constant category - assumed 'category' in...
Arraycategory
Code: Select all
Array
Re: Posting Arrays
Hmmm. What is returned when you execute this statement...
Code: Select all
echo is_array($params['filterby']['category']) ? 'Array' : 'not an Array';
Nearly all men can stand adversity, but if you want to test a man's character, give him power.
- Abraham Lincoln
- Abraham Lincoln
- kidcardboard
- Forum Members
- Posts: 54
- Joined: Mon Sep 28, 2009 5:25 pm
Re: Posting Arrays
it returns
aswell as which I suppose I should've mentioned before because this message isn't new (I hinted to it in the op though) but it might spark something for you guys
Code: Select all
not an Array
Code: Select all
Notice: Array to string conversion in /var/www/.../httpdocs/lib/misc.functions.php on line 417
Last edited by kidcardboard on Fri Sep 23, 2011 4:52 pm, edited 1 time in total.
Re: Posting Arrays
Hmm. That's what I suspected--not an Array.kidcardboard wrote:it returnsaswell asCode: Select all
not an Array
which I suppose I should've mentioned before because this message isn't new (I hinted to it in the op though) but it might spark something for you guysCode: Select all
Notice: Array to string conversion in /var/www/vhosts/meetingscalgary.com/subdomains/dev/httpdocs/lib/misc.functions.php on line 417
Without knowing the version of CMSMS that you're using, it's hard to tell what code is at line 417 in the lib/misc.functions.php installed on your site.
Nearly all men can stand adversity, but if you want to test a man's character, give him power.
- Abraham Lincoln
- Abraham Lincoln
- kidcardboard
- Forum Members
- Posts: 54
- Joined: Mon Sep 28, 2009 5:25 pm
Re: Posting Arrays
I suppose I could've included that too eh...lol. I'm running 1.9.4.2 "Faanui"
Re: Posting Arrays
I've just come across the same issue so I did some checking.
On the front end, parameters are passed to the cleanParamHash method in misc.functions.php for cleaning. Unfortunately, this method does't recurse down the arrays so they are interpreted as strings, hence the "Array to string..." notice.
It doesn't look to me like there's a way round this, short of adding it as a feature request. I think the best option in this case is to just register the params separately or like you said, use $_POST and sanitize yourself.
On the front end, parameters are passed to the cleanParamHash method in misc.functions.php for cleaning. Unfortunately, this method does't recurse down the arrays so they are interpreted as strings, hence the "Array to string..." notice.
It doesn't look to me like there's a way round this, short of adding it as a feature request. I think the best option in this case is to just register the params separately or like you said, use $_POST and sanitize yourself.
Re: Posting Arrays
@kidcardboardtomgsd wrote:...
It doesn't look to me like there's a way round this, short of adding it as a feature request. I think the best option in this case is to just register the params separately or like you said, use $_POST and sanitize yourself.
I'm not sure if it would help much, but you might explore registering your "array of arrays" param as CLEAN_NONE. You'd still need to clean the array elements yourself, as with $_POST, but that would keep param handling as consistent as possible... until your feature request gets implemented. Just a thought.
Nearly all men can stand adversity, but if you want to test a man's character, give him power.
- Abraham Lincoln
- Abraham Lincoln