slimphp / Slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
http://slimframework.com
MIT License
11.94k stars 1.95k forks source link

Problems with checkboxes arrays #981

Closed jrborbars closed 9 years ago

jrborbars commented 9 years ago

Following the problem described here [1], I can't retrieve an entire array passed with a checkbox via post with $app-request->post('sel[]'). Maybe, I think, this is a (silent?) bug.

[1] - http://help.slimframework.com/discussions/problems/8449-problems-with-checkbox

brandisher commented 9 years ago

Hi @jrborbars,

This is very delayed but I had some spare time to contribute. You can access the values by using $app->request->post('sel'). I stood up a test instance and confirmed this myself.

An important thing to note is that if you're getting a POST'd array directly via the request and none of the checkboxes are checked, you simply end up with a null.

jrborbars commented 9 years ago

Hi cwscribner, Thanks (a lot) for your reply, but I think that you not "catch" my problem. Your solution works well with ONLY ONE value selected in checkboxes. If I select multiple values only the LAST value return (checked with var_dump). And yes, I was checked some checkboxes to receive this values...... I never said that the array was returned empty. ;-) Edit (1 min): In plain HTML form and plain PHP this works well.

brandisher commented 9 years ago

@jrborbars no problem :-)

I should've clarified in my original response, I was able to successfully retrieve more than one selected checkbox using $app->response->post('sel'). My tests were:

0 checkboxes checked -- returns null 1 checkbox checked -- returns $_POST['sel'][0]=>on 2 out of 4 checkboxes checked -- returns $_POST['sel'][0]=>on, [1]=>on

jrborbars commented 9 years ago

@cwscribner,

What's your version of Slim? I'm using 2.4.2 and can't do this. 8-( The code: ######## HTML #####################################

######## CONTROLLER/ROUTE ######################## $sel = $app->request()->post('sel'); echo "<pre>"; var_dump($sel); echo "</pre>"; ######## RESULT #################################### Result of var_dump: string(1) "3" ######## FIREBUG ################################### Post: sel 2 sel 3 #################################################### Other inputs in the same form are catched by $app->request()->post() easy (no one have the same name or id!). Other ideas?

Thanks A LOT and best regards.

EDIT: Now I see that you write $app->response->post(). This is a typo?

brandisher commented 9 years ago

I believe I'm using the most recent stable version (2.5). I don't have access to it at the moment so I can't confirm 100%.

One other thing you could try is using $app->request->params() or $app->request->params('sel') just to see if that returns anything. Do you have any of this code in a repo anywhere? I could probably investigate more thoroughly if I could see the code in full context.

jrborbars commented 9 years ago

I will try to post it in a gist soon. So I tested to put individual names to each checkbox ('sel1', 'sel2', 'sel3'), and it WORKS. Worked to if names are 'sel[1]', 'sel[2]', 'sel[3]',etc. But doesn't works if names are 'sel' only (for all checkboxes) or 'sel[ ]' (without the inner space).

NOTE: Now I see my typo in the $app->request above. But it works (believe in me ;-). ) WRONG ------> $app->request()->post('sel'); RIGHT ------> $app->request->post('sel');

brandisher commented 9 years ago

So for my own clarification, fixing the typo fixed the issue you are having?

jrborbars commented 9 years ago

No. The behaviour still wrong. The checkbox with ONE name must return an array. The "solution that works" before is only a workaround that I tried, putting individual names and POST returning individual variables. So, with my typo or not, the POST do the job (with only one name for each variable).

brandisher commented 9 years ago

Ah, okay. Without seeing your full code, I don't think I can diagnose any further. I was successfully able to pass multiple checkboxes with the same name (sel[]) and retrieve them as an array using $app->request->post() and $app->request->post('sel'). This was using Slim 2.4.2.

tassoevan commented 9 years ago

$app->request is an alias for $app->request(); both work. And I confirm the behavior described by @cwscribner, $app->request->post("sel") returns the array, even using 2.4.2 version. To return just the last value, the checkboxes must be <input name="sel"> instead of <input name="sel[]"> or <input name="sel[n]">, the expected behavior.

silentworks commented 9 years ago

I don't think this is a bug as others have pointed out here, this actually works. Can we move the discussion back over to http://help.slimframework.com/discussions/problems/8449-problems-with-checkbox if you wish to continue or jump on irc channel #slimphp on freenode.

jrborbars commented 9 years ago

Hummmmm. This is very strange. I see with more attention the @tassoevan response, that said that $app->response->post('sel') catch the input of name="sel[]". I tried and the result (var_dump) is NULL. So, this is not a server issue. Oh! Now I remember that I'm using HHVM 3.4.2, not Zend PHP, with a Nginx HTTP server. But this type of post array works with raw PHP and, IMHO, this still not a server issue. Continue in help.slimframework,com.