0

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" ?

2
  • 3
    Un-checked checkboxes are not sent to the server, so $_POST['test_send_checkbox'] would not exist. Commented Apr 10, 2018 at 12:29
  • ...because of that ^, there's also no reason to have different values for the checkboxes. If the parameter exists in the posted data, it was checked. If it doesn't exist, it wasn't checked. Commented Apr 10, 2018 at 12:36

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your answer, i have create an field hidden like say Quazer and its ok :)
if you want to become a good programmer, than you should never stop when the response/result is "ok" - the proposed solution is more like a hack than a real solution - you may get the expected result while having a completely wrong implementation. Think it through, try to find best practices, try to optimize your implementation
0

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:

  1. Create the hidden field with same name and value="false" before checkbox field
echo "<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'>"; 
}
  1. Or check of null value.

2 Comments

adding an other fields that sometimes overrides the post-data is not a clean solution
i known it, but Zend Framework use that style and it developer of php :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.