kongondo Posted May 21, 2013 Share Posted May 21, 2013 (edited) Hi, This is not PW-specific so not sure if I am posting in the right board. I have a PW page with a jQuery ajax call that serializes some values and sends them by POST like so: list[7]=root&list[2]=root&list[5]=2&list[4]=root&list[3]=4&list[6]=root&list[8]=6&list[1]=8 These are sent to another PW page for processing. I have the following code. It works fine but I get an "Invalid argument supplied for foreach() in..." error (in the example I haven't sanitized anything; will do so in final version ). $i = 0; foreach ($input->post ['list']as $key => $value) {//values to be sanitized; this is a basic example $x = $key; $y = $value; $z = $i; $i++; if ($y == "foo") { $y = "bar"; } //do other stuff with $x, $y and $z } Although the code works, I'd like to get rid of the warning. I know foreach expects an array in this case so ideally, I would want to convert the serialized string to an array first. That's where I am getting stuck (searched StackOverflow, etc ). I also know I could use jQuery toArray but that sends some other value pairs that I don't need. I think "serialize" is simpler to use. Maybe explode the string? Overkill for this? Thanks for any pointers! Cheers /k Edit: Just to clarify. The warning would occur even outside PW. It had nothing to do with PW methods. Edited May 22, 2013 by kongondo Link to comment Share on other sites More sharing options...
adrian Posted May 21, 2013 Share Posted May 21, 2013 Does parse_str do what you need? http://www.php.net/manual/en/function.parse-str.php Link to comment Share on other sites More sharing options...
diogo Posted May 21, 2013 Share Posted May 21, 2013 Shouldn't it be simply foreach ($input->post as $key => $value) ? Link to comment Share on other sites More sharing options...
Soma Posted May 21, 2013 Share Posted May 21, 2013 Just use isset($input->post->list) 1 Link to comment Share on other sites More sharing options...
kongondo Posted May 21, 2013 Author Share Posted May 21, 2013 Does parse_str do what you need? http://www.php.net/manual/en/function.parse-str.php Think I tried this earlier but can't remember the outcome; tried so many things! Thx. Shouldn't it be simply foreach ($input->post as $key => $value) ? This takes away the Warning but the code doesn't work; cannot save to DB Just use isset($input->post->list) Works like a charm, thanks! For fellow-newbies, I used this like so if (isset($input->post->list){//access as object property $i = 0; foreach($input->post->list as $key => $value){ blah blah... } //this also works //if (isset($input->post['list']))//access as array index } <cliche>I owe you one Soma!</cliche> ps: If you have a minute, please explain why this works....and I thought I knew what isset does Link to comment Share on other sites More sharing options...
Wanze Posted May 21, 2013 Share Posted May 21, 2013 I think that Pw returns null if a POST variable isn't set. So this should also work: if (!is_null($input->post->list)) {} if ($input->post->list !== null) {} // Simplest but be careful when list contains the value '0' - this is casted to false if ($input->post->list) {} You get the warning from PHP if your array is empty and you want to iterate over it - that's why you always should first check if the variable is set / contains values. Quote from the docs: $input returns NULL if you access a variable that doesn't exist (no need to use isset() like with PHP's superglobals) http://processwire.com/api/variables/input/ 1 Link to comment Share on other sites More sharing options...
kongondo Posted May 21, 2013 Author Share Posted May 21, 2013 (edited) Thanks Wanze for the additions. In my case, I am sure that the array was not empty so the warning was due to foreach not liking the serialized string... Edit: Just to clarify. The warning would occur even outside PW. It had nothing to do with PW methods. Edited May 22, 2013 by kongondo Link to comment Share on other sites More sharing options...
kongondo Posted May 21, 2013 Author Share Posted May 21, 2013 Btw, how can I easily sanitize the $key=> $value pairs? The $key is straightforward because it will always be an integer. So I can do this? $x = (int) $key; or should I sanitize earlier at the $input->post->list? The $value is tricky because it could be an integer or it could be the string "root". Maybe use an if on this one? Thanks. Link to comment Share on other sites More sharing options...
ryan Posted May 23, 2013 Share Posted May 23, 2013 or should I sanitize earlier at the $input->post->list? I think it's probably just whatever you find most useful in your situation. But my preference is usually to sanitize on first access, unless I potentially need an unsanitized version to determine how it should be sanitized (which might be the case here). The $value is tricky because it could be an integer or it could be the string "root". Maybe use an if on this one? You could sanitize first with pageName (which sanitizes to [-_.a-z0-9], and then use ctype_digit to determine if it's an integer or string: $value = $sanitizer->pageName($input->post->something); if(ctype_digit("$value")) { // it's an integer $value = (int) $value; } else { // it's a string } 1 Link to comment Share on other sites More sharing options...
kongondo Posted May 23, 2013 Author Share Posted May 23, 2013 Thanks Ryan. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now