albaserver Posted October 28, 2014 Share Posted October 28, 2014 Hello to everyone, I have a strange problem and I'm not able to solve it by myself and I need your help! I have three simple checkboxes that process via POST their value. But with PHP I cannot have an array with the three indexes (if everyone checked) but only an array with the first checkbox checked. The code is very simple: <?php $scelta = isset($_POST['user_rights']) ? $_POST['user_rights'] : array(); if (isset($_POST["user_rights"])){ print_r($scelta);}else{ ?> <form method="post"><input type="checkbox" name="user_rights[]" value="0"> 0<br><input type="checkbox" name="user_rights[]" value="1"> 1<br><input type="checkbox" name="user_rights[]" value="2"> 2 <input type="submit"></form> <?php} ?> The output if I check all the checkboxes is: Array ( [0] => 0 ) If I try this code on another server it works (it returns the complete array), but if i run it on my web server, it doesn't (it returns only the first item). Do you have any experience with this error? Do you know if there is some configuration to change? Thanks in advance. Rob Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/ Share on other sites More sharing options...
ginerjm Posted October 28, 2014 Share Posted October 28, 2014 I cleaned up your code a bit and it seems to work fine. <?php session_start(); error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); // $scelta = isset($_POST['user_rights']) ? $_POST['user_rights'] : array(); if (isset($_POST["user_rights"])) { echo 'User rights setting:<br>'; print_r($scelta); } else { echo '<form method="post">'; echo '<input type="checkbox" name="user_rights[]" value="0"> 0'; echo "<br>"; echo '<input type="checkbox" name="user_rights[]" value="1"> 1'; echo '<br>'; echo '<input type="checkbox" name="user_rights[]" value="2"> 2'; echo '<br><br><input type="submit">'; echo '</form>'; } exit(); Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1494994 Share on other sites More sharing options...
albaserver Posted October 28, 2014 Author Share Posted October 28, 2014 First of all thanks for your reply. I tried your code but the behaviour it's always the same. On the first server it doesn't work, but if I try on another server it works well. None knows a bug for this case? Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495013 Share on other sites More sharing options...
ginerjm Posted October 28, 2014 Share Posted October 28, 2014 So what's different in each server? PHP version? Both apache? Both *nix machines? Look for significant diffs such as these, then if all the same, look for some confg setting that's different. (I'm not qualified to help you any further with this kind of problem. I'm sure others are.) Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495017 Share on other sites More sharing options...
albaserver Posted October 30, 2014 Author Share Posted October 30, 2014 PHP: 5.3.8 Apache/2.2.12 (Linux/SUSE) Ask me if any other info can be useful Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495222 Share on other sites More sharing options...
jazzman1 Posted October 30, 2014 Share Posted October 30, 2014 Rob, any chance to see the last records of apache error and success files, when the form is being submitted? Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495229 Share on other sites More sharing options...
albaserver Posted October 30, 2014 Author Share Posted October 30, 2014 I have a root access on that server, but I'm not able to get them. Can u tell me how to do that so I give you the infos? Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495234 Share on other sites More sharing options...
ginerjm Posted October 30, 2014 Share Posted October 30, 2014 Add this before your test of user_rights: echo '<pre>' , print_r($_POST,true) , '</pre>'; This will show your entire raw POST array. Let's see what you have. Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495249 Share on other sites More sharing options...
albaserver Posted October 30, 2014 Author Share Posted October 30, 2014 If I check all the checkboxes (three) I see only the first one, here it is: Array( [user_rights] => Array ( [0] => 0 )) Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495251 Share on other sites More sharing options...
ginerjm Posted October 30, 2014 Share Posted October 30, 2014 Always the doubter here, so can we see the full code that you ran to get that now? (The code you post with the additions I asked for.) Humor me and add a name= clause to your submit as well. I'd just like to see something else in the POST array. Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495252 Share on other sites More sharing options...
albaserver Posted October 30, 2014 Author Share Posted October 30, 2014 (edited) The code is very very very simple: <?php session_start(); error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); if (isset($_POST["user_rights"])) { echo '<pre>' , print_r($_POST,true) , '</pre>'; } else { ?> <form method="post"> <input type="hidden" name="nickname" value="Rob"> <input type="checkbox" name="user_rights[]" value="0">0<br> <input type="checkbox" name="user_rights[]" value="1">1<br> <input type="checkbox" name="user_rights[]" value="2">2<br> <br> <input type="submit"></form> <?php } ?> If I check ALL the checkboxes, the result is: Array( [nickname] => Rob [user_rights] => Array ( [0] => 0 )) It could be some wrong configuration of the server? But where?!?! Edited October 30, 2014 by albaserver Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495254 Share on other sites More sharing options...
ginerjm Posted October 30, 2014 Share Posted October 30, 2014 Funny that you couldn't add a name to the submit button, but at least I do see another element of POST. You said earlier that you ran the code that I posted. If you really did run that verbatim (ie, unchanged) I have nothing to offer you here. I know of no reason that POST should not contain multiple elements in the 'user_rights' array when you check all 3 boxes. Below is the EXACT code that I just ran successfully in its entirety and it works as expected for me. Sorry. <?php session_start(); error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); // // if (isset($_POST["btn"]) && $_POST['btn']=='Submit') { echo 'User rights setting:<br>'; echo '<pre>', print_r($_POST['user_rights'],true), '</pre>'; } else { echo '<form method="post">'; echo '<input type="checkbox" name="user_rights[]" value="0"> 0'; echo "<br>"; echo '<input type="checkbox" name="user_rights[]" value="1"> 1'; echo '<br>'; echo '<input type="checkbox" name="user_rights[]" value="2"> 2'; echo '<br><br><input type="submit" name="btn" value="Submit">'; echo '</form>'; } exit(); Good luck! Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495255 Share on other sites More sharing options...
Barand Posted October 30, 2014 Share Posted October 30, 2014 Out of curiosity, as no-one else can recreate this problem, what happens if you specify the indexes of the checkbox array? <input type="checkbox" name="user_rights[0]" value="0">0<br> <input type="checkbox" name="user_rights[1]" value="1">1<br> <input type="checkbox" name="user_rights[2]" value="2">2<br> Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495256 Share on other sites More sharing options...
ginerjm Posted October 30, 2014 Share Posted October 30, 2014 WAD for me. Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495258 Share on other sites More sharing options...
mac_gyver Posted October 30, 2014 Share Posted October 30, 2014 the php version you listed still has register_globals available. any chance that at some point in your testing you set a $_SESSION['user_rights] or a $_COOKIE['user_rights] variable with some values that could be now overwriting your $_POST data? Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495260 Share on other sites More sharing options...
ginerjm Posted October 30, 2014 Share Posted October 30, 2014 Gee, I sure hope your guess is wrong mac_gyver because the OP has not once intimated that there is "more code" that he isn't showing us. That would be a real kick in the b***** Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495267 Share on other sites More sharing options...
albaserver Posted October 31, 2014 Author Share Posted October 31, 2014 (edited) The code is only the one I posted Now I try with this version: <?php session_start(); error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); if (isset($_POST["somethingelse"])) { echo '<pre>' , print_r($_POST,true) , '</pre>'; } else { ?> <form method="post"> <input type="hidden" name="nickname" value="Rob"> <input type="checkbox" name="somethingelse[0]" value="0"> 0<br> <input type="checkbox" name="somethingelse[1]" value="1"> 1<br> <input type="checkbox" name="somethingelse[2]" value="2"> 2 <br><br> <input type="submit"></form> <?php } ?> But the error is still the same. Only the first item of the array come via POST!! What else can I try?! Rob Edited October 31, 2014 by albaserver Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495341 Share on other sites More sharing options...
mac_gyver Posted October 31, 2014 Share Posted October 31, 2014 in your last post, you need to show us what the print_r() output actually is, because it may hold a clue as to the problem. also, when you check only one or two boxes, not all three, is the result correct? Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495343 Share on other sites More sharing options...
albaserver Posted October 31, 2014 Author Share Posted October 31, 2014 in your last post, you need to show us what the print_r() output actually is, because it may hold a clue as to the problem. also, when you check only one or two boxes, not all three, is the result correct? In every case, the script returns only the first checkbox selected If I check the second and the third one (value 1 and 2), I get: Array( [somethingelse] => Array ( [0] => 1 )) Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495347 Share on other sites More sharing options...
jazzman1 Posted October 31, 2014 Share Posted October 31, 2014 Any chance to use non-ascii characters as a name of the name html atribute? for instance, none of these values are being submitted to the server using the following html form: <?php session_start(); error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); if (isset($_POST["somethingelse"])) { echo '<pre>' , print_r($_POST,true) , '</pre>'; } else { ?> <form method="post"> <input type="hidden" namе="nickname" value="Rob"> <input type="checkbox" namе="somethingelse[0]" value="0"> 0<br> <input type="checkbox" name="somethingelsе[1]" value="1"> 1<br> <input type="checkbox" namе="somethingelse[2]" value="2"> 2 <br><br> <input type="submit"></form> <?php } ?> To check the error and access log messages you need to go to /var/log directory and find service's name directory, it would be named apache or httpd (not sure in SUSE). Inside this folder you will find apache log files. Submit the form again and give us the output messages of : tail access_log // and tail error_log Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495352 Share on other sites More sharing options...
ginerjm Posted October 31, 2014 Share Posted October 31, 2014 I just spent 20 minutes googling all the permutations of this post's problem that I could think of. Do you know that in all the results I found NOT ONE of them described this problem? It seems that nobody has ever had a problem like this. This leads me to suspect that there is something else going on here that we are not privy to. Albaserver: Looking at this 'simple script' of yours I have to ask - Where is the rest of it? The little bit of html you have posted can not possibly be your entire web page, can it? No doctype tag, no meta tags no title tag, no headings, - no nothing. It would appear that you have extracted this from somewhere and placed it here to isolate your 'problem' for our benefit, but I really think you must have some other code that is messing with this. Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495370 Share on other sites More sharing options...
albaserver Posted November 3, 2014 Author Share Posted November 3, 2014 Sorry but I think I will give up. Never seen this bug and I don't know how to fix it. Today I wrote to the sysadmins to have a new VM in our Intranet 'cause no one I asked for was able to help me Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495592 Share on other sites More sharing options...
ginerjm Posted November 3, 2014 Share Posted November 3, 2014 So my last post didn't trigger any "ah ha!' moment? I really think there is some more "code" happening here than what we are seeing. Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495620 Share on other sites More sharing options...
mikosiko Posted November 3, 2014 Share Posted November 3, 2014 Rob, if you have access to your php.ini check how the variable max_input_vars is defined... maybe it has a wrong limit... I was able to replicate the behavior that you are seeing setting that variable to 1 (the default is 1000).... maybe is not your case, but should be investigated. According to some others posts in the net, it could be also an issue of the suhosin extension (if it is installed), basically also due to wrong configuration of some variables on it. hope this help Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495626 Share on other sites More sharing options...
albaserver Posted November 5, 2014 Author Share Posted November 5, 2014 to mikosiko: max_input_vars is 1000 to ginerjim: no more code, only that one I posted in a blank page, nothin'else Quote Link to comment https://forums.phpfreaks.com/topic/292109-checkboxes-via-post-dont-create-array/#findComment-1495760 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.