Reputation: 47
i try to get an array of value from checkbox like this :
if ($valueCommande['COMMANDE']["VALUE_CHECKBOX"] == "true")
{
echo "<input class='send_checkbox' checked name='test_send_checkbox[]' value='true' type='checkbox'>";
}
else
{
echo "<input class='send_checkbox' name='test_send_checkbox[]' value='false' type='checkbox'>";
}
but in my php i get only "true", when is "false" its null, and i don't understand why.
My php :
$value_send_checkbox = $_POST['test_send_checkbox'];
if i have 3 checkbox : true, false and true, i have only an array with true then true, how get the "false" ?
Upvotes: 0
Views: 86
Reputation: 383
The client sends a value from the check box field to the server only if the check box is checked. Accordingly, you can use one of two ways:
value="false"
before checkbox fieldecho "<input class='send_checkbox' name='test_send_checkbox[]' value='false' type='hidden'>";
if ($valueCommande['COMMANDE']["VALUE_CHECKBOX"] == "true")
{
echo "<input class='send_checkbox' checked name='test_send_checkbox[]' value='true' type='checkbox'>";
}
else
{
echo "<input class='send_checkbox' name='test_send_checkbox[]' value='true' type='checkbox'>";
}
Upvotes: 0
Reputation: 637
The values of the checkboxes are only sent if they are checked, since yours is an array (name='test_send_checkbox[]
) you have to check if the value is in the array if it is present at all
if ( !empty($valueCommande['COMMANDE']["VALUE_CHECKBOX"])
&& in_array('true', $valueCommande['COMMANDE']["VALUE_CHECKBOX"]) ) {
echo "<input class='send_checkbox' checked name='test_send_checkbox[]' value='true' type='checkbox'>";
} else {
echo "<input class='send_checkbox' name='test_send_checkbox[]' value='true' type='checkbox'>";
}
EDIT:
you also have to modify the checkbox value; since you are only interested in if it is checked, the value doesn't matter, but it should be the same.
If you are only using one checkbox in this page, you should consider rewiting it in a simpler way:
$isChecked = !empty($valueCommande['COMMANDE']["test_send_checkbox"]) ?
" checked" : "";
echo "<input class='send_checkbox' {$isChecked} name='test_send_checkbox' value='1' type='checkbox'>";
Note that I've changed the checkbox value, and name to.
Upvotes: 1