Jump to content

checkboxes via POST don't create array


albaserver
Go to solution Solved by albaserver,

Recommended Posts

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

Link to comment
Share on other sites

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();
Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

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 by albaserver
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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 by albaserver
Link to comment
Share on other sites

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        ))
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.