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.
POST behaviour with checkboxes
Moderator: General Moderators
Re: POST behaviour with checkboxes
Post code: HTML and PHP.
Re: POST behaviour with checkboxes
I gather my page and echo the results at the end.tasairis wrote:Post code: HTML and PHP.
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
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,
$_POST["option"] will be an array.
Can't handle multiple selections.
Instead,
Code: Select all
<input type="checkbox" name="option[]" value="$itemId">Re: POST behaviour with checkboxes
Thanks for your advice : )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,$_POST["option"] will be an array.Code: Select all
<input type="checkbox" name="option[]" value="$itemId">
With a little searching on the net and a quick test I have got it working.
Thanks again.
Re: POST behaviour with checkboxes
I thought I had got this working but I dont understand the following - maybe its getting too latepaulb wrote:Thanks for your advice : )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,$_POST["option"] will be an array.Code: Select all
<input type="checkbox" name="option[]" value="$itemId">
With a little searching on the net and a quick test I have got it working.
Thanks again.
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");
}
}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
Can someone help with this please?paulb wrote:I thought I had got this working but I dont understand the following - maybe its getting too latepaulb wrote:Thanks for your advice : )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,$_POST["option"] will be an array.Code: Select all
<input type="checkbox" name="option[]" value="$itemId">
With a little searching on the net and a quick test I have got it working.
Thanks again.![]()
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!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"); } }
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!!!!