POST behaviour with checkboxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paulb
Forum Newbie
Posts: 12
Joined: Tue Sep 29, 2009 12:51 am

POST behaviour with checkboxes

Post by paulb »

Hi,
Can someone tell me if this behaviour is correct. I have several checkboxes of the same name and have a associated value(item) number. The state of these checkboxes are determined from a database, but are set from the form. When the checkboxes are ticked the database is updated. If none of the checkboxes are set in the database, when I select a checkbox the correct item seems to be POST'ed. If one of the checkboxes is set in the database, when I select another checkbox the incorrect item seems to be POST'ed. It seems to be the item that is associated with the one set in the database!!!! The code shows that this cant be possible.

Is this normal behaviour of checkboxes? Is it because they are all grouped in one?

Hope you can help.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: POST behaviour with checkboxes

Post by requinix »

Post code: HTML and PHP.
paulb
Forum Newbie
Posts: 12
Joined: Tue Sep 29, 2009 12:51 am

Re: POST behaviour with checkboxes

Post by paulb »

tasairis wrote:Post code: HTML and PHP.
I gather my page and echo the results at the end.

So at the top I have:
$results .= "<form id=\"myform\" name=\"myform\" method=\"post\" action=\"results.php?item=$item\">";

I then check if the POST option has been set and get the value of it:
// Check if the interested option has been set
if (isset($_POST['option'])) {
$myItem = $_POST['option'];
$pre_results .= "---$myItem = ";
$pre_results .= getInterestedFlag_db($table, $_POST['option']) . "<br>";
}

For the check box I have the following - this one is for when it is checked:
$interestedFormatted = "<input type=\"checkbox\" name=\"option\" value=\"$itemId\" onchange=\"myform.submit()\" checked=\"yes\">";

Thats about it really. Hope you can help.

Cheers.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: POST behaviour with checkboxes

Post by requinix »

Your code will only allow for one checkbox to be checked: the last one checked will be what you get.
Can't handle multiple selections.

Instead,

Code: Select all

<input type="checkbox" name="option[]" value="$itemId">
$_POST["option"] will be an array.
paulb
Forum Newbie
Posts: 12
Joined: Tue Sep 29, 2009 12:51 am

Re: POST behaviour with checkboxes

Post by paulb »

tasairis wrote:Your code will only allow for one checkbox to be checked: the last one checked will be what you get.
Can't handle multiple selections.

Instead,

Code: Select all

<input type="checkbox" name="option[]" value="$itemId">
$_POST["option"] will be an array.
Thanks for your advice : )

With a little searching on the net and a quick test I have got it working.

Thanks again.
paulb
Forum Newbie
Posts: 12
Joined: Tue Sep 29, 2009 12:51 am

Re: POST behaviour with checkboxes

Post by paulb »

paulb wrote:
tasairis wrote:Your code will only allow for one checkbox to be checked: the last one checked will be what you get.
Can't handle multiple selections.

Instead,

Code: Select all

<input type="checkbox" name="option[]" value="$itemId">
$_POST["option"] will be an array.
Thanks for your advice : )

With a little searching on the net and a quick test I have got it working.

Thanks again.
I thought I had got this working but I dont understand the following - maybe its getting too late :banghead:

Code: Select all

for($i=0;$i<$count;$i++) {
            $myInterestedItem = $myInterestedItems[$i];
            $currentStatusItem = getInterestedFlag_db($table, $myInterestedItem) ;
            $pre_results .= "item = $myInterestedItem is $currentStatusItem <br>";
            if ($currentStatusItem == "true") {
                setInterestedFlag_db($table, $myInterestedItem, "false");
            }
            else {
                setInterestedFlag_db($table, $myInterestedItem, "true");
            }
        }
What I find is that the size of the array in the POST is changing. All I want to do is find the associated item with the checkbox that I ticked and set it appropriately. Because the user can check any of the boxes then the item could be anywhere in the list!

I supose it doesnt help with the logic that is applied .ie if it is false then set it true and vice versa etc.

What am I doing wrong!!!!
paulb
Forum Newbie
Posts: 12
Joined: Tue Sep 29, 2009 12:51 am

Re: POST behaviour with checkboxes

Post by paulb »

paulb wrote:
paulb wrote:
tasairis wrote:Your code will only allow for one checkbox to be checked: the last one checked will be what you get.
Can't handle multiple selections.

Instead,

Code: Select all

<input type="checkbox" name="option[]" value="$itemId">
$_POST["option"] will be an array.
Thanks for your advice : )

With a little searching on the net and a quick test I have got it working.

Thanks again.
I thought I had got this working but I dont understand the following - maybe its getting too late :banghead:

Code: Select all

for($i=0;$i<$count;$i++) {
            $myInterestedItem = $myInterestedItems[$i];
            $currentStatusItem = getInterestedFlag_db($table, $myInterestedItem) ;
            $pre_results .= "item = $myInterestedItem is $currentStatusItem <br>";
            if ($currentStatusItem == "true") {
                setInterestedFlag_db($table, $myInterestedItem, "false");
            }
            else {
                setInterestedFlag_db($table, $myInterestedItem, "true");
            }
        }
What I find is that the size of the array in the POST is changing. All I want to do is find the associated item with the checkbox that I ticked and set it appropriately. Because the user can check any of the boxes then the item could be anywhere in the list!

I supose it doesnt help with the logic that is applied .ie if it is false then set it true and vice versa etc.

What am I doing wrong!!!!
Can someone help with this please?
Post Reply