Make the checkbox return false when unchecked

I have a view similar to:

<form> <input type"checkbox" name="checked[(unique_id)]"> <input type"checkbox" name="checked[(unique_id)]"> <input type"checkbox" name="checked[(unique_id)]"> <input type"checkbox" name="checked[(unique_id)]"> </form> 

The number of flags will change from time to time, so when processing this data with PHP I need to loop the _POST['checked'] array.

My problem is that I want to perform actions when the checkbox is checked and when not. But only marked checkboxes will be added to the _POST['checked'] array.

+6
source share
4 answers
 <form> <input type="checkbox" key="1"/> <input type="hidden" name="checked[1]" value="false"> <input type="checkbox" key="2"/> <input type="hidden" name="checked[2]" value="false"> <input type="checkbox" key="3"/> <input type="hidden" name="checked[3]" value="false"> <input type="checkbox" key="4"/> <input type="hidden" name="checked[4]" value="false"> </form> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> $(document).ready(function () { $('[key]').change(function () { var key = $(this).attr('key'); $($('[name="checked[' + key + ']"]')).val($(this).is(':checked') ? 'true' : 'false'); }); }); </script> 

that's what I'm doing

I use two inputs: one flag without a name, so it will not be sent to php, the other is hidden, it will not be shown to the user, but this is what will be sent to php

then with jquery, when the user checks the jquery checkbox, change the value of the hidden input to true, and when you clear the checkbox, change the value to false

so the value will always be sent to php with true or false value as string

can you change the value you want to send to php by changing this .is(':checked')?'true':'false') something like .is(':checked')?1:0) to send 1 and 0 instead of true and false

another solution is rybo111 solution

 <input type="hidden" name="check" value="false"> <input type="checkbox" name="check" value="true"> 

it will send two parameters, but if checked, it will override the first option

but it is not 100% reliable and it will send more data to the server

learn more about this in POSTing Form Fields with the same name attribute

therefore, if you want to use a simple solution without js, use only "html" if you want a 100% reliable solution to use "js"

+1
source

Here is a technique I've seen before:

 <input type="hidden" name="check" value="false"> <input type="checkbox" name="check" value="true"> 

The reason for this is that when values โ€‹โ€‹with the same name are sent more than once, only the last value is accepted.

In my opinion, the best way is to simply use isset($_POST['...']) or in_array($_POST['...']) .

+3
source

Add value="true" to each checkbox entry. And change your PHP code to:

 $checked_arr = []; foreach($_POST["checked"] as $checked){ if($checked == "true"){ // do what you want to do } } 
0
source

Another solution is a server-side solution, which, it seems to me, is too fast and simple.

client side: Just use fresh HTML:

 <input type="checkbox" name="checked1" value="value_checked" ... /> 

server side:

make a global function as follows:

 function getVal(&$var, $default = '') { if (!isset($var) || empty($var) || is_null($var)) $var = $default; return $var; } 

then use where you want to read a value that you don't know whether it is set or not. eg:

 $checked_value = getVal($_POST["checked1"],false); 

or

 $checked_value = getVal($_POST["checked1"],"value_not_checked"); 

I hope that is useful for another.

0
source

Source: https://habr.com/ru/post/989884/


All Articles