Page 1 of 1

POST behaviour with checkboxes

Posted: Sun Nov 01, 2009 12:34 pm
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.

Re: POST behaviour with checkboxes

Posted: Sun Nov 01, 2009 1:25 pm
by requinix
Post code: HTML and PHP.

Re: POST behaviour with checkboxes

Posted: Sun Nov 01, 2009 2:03 pm
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.

Re: POST behaviour with checkboxes

Posted: Sun Nov 01, 2009 2:47 pm
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.

Re: POST behaviour with checkboxes

Posted: Sun Nov 01, 2009 3:22 pm
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.

Re: POST behaviour with checkboxes

Posted: Sun Nov 01, 2009 4:25 pm
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!!!!

Re: POST behaviour with checkboxes

Posted: Tue Nov 03, 2009 2:07 am
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?