7

I have a form looking like:

<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 checkboxes will variate from time to time so when processing this data with PHP I have to loop the _POST['checked'] array.

My problem is that I want to take actions both when a checkbox is checked and when it's not. But only the the checked checkboxes will be added to the _POST['checked'] array.

Gurgy
  • 95
  • 1
  • 1
  • 6
  • 1
    you can't do it with html so you have 2 option 1 use javascript , 2 use drop down list instead of checkbox – Robert Jun 28 '15 at 10:39
  • 1
    why not add this to your php: $checked = isset(_POST['checked']) && _POST['checked'] == 1; – sabkaraja Jun 28 '15 at 10:41
  • @robert I suspected as much, a drop down list is not an alternative in this case but I'm more then open to a solution based on JS. How would I go about doing this with JS? (I have a very limited experience with JS) – Gurgy Jun 28 '15 at 10:43

5 Answers5

4
<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>

here is what i'm doing

i'm using two inputs one is checkbox without name so it won't be sent to php , the other is hidden won't be shown to the user but it is what will be sent to php

then with jquery when the user check the box jquery change the value of the hidden input to true and when uncheck it change the value to false

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

you can change the value you want to send to php by changing this .is(':checked')?'true':'false') to something like that .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 the two options but if the checkbox is checked it will override the first option

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

read more about that in POSTing Form Fields with same Name Attribute

so if you want to use simple solution without js use the "html only" if you want 100% reliable solution use the "js"

Community
  • 1
  • 1
Robert
  • 2,342
  • 2
  • 24
  • 41
4

Here's a technique I've seen before:

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

The reason this works is because when values with the same name are sent more than once, only the last value is accepted.

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

rybo111
  • 12,240
  • 4
  • 61
  • 70
  • +1 it's good solution, but there are one problem that this solution is not reliable because some browser may send the first value – Robert Jun 28 '15 at 11:32
  • 2
    @robert do you have any data on what browsers send the first value? – rybo111 Jun 28 '15 at 11:34
  • 1
    i don't know if there are some browsers do that , but for example i faced a problem when i used `` to send data to php but now the type submit won't be send to the server – Robert Jun 28 '15 at 11:40
  • 2
    @robert Submit buttons are handled differently. But as far as I'm aware, this is a safe solution until someone can prove otherwise. I would argue it supports more users than those who use JavaScript. – rybo111 Jun 28 '15 at 11:44
  • if i think your answer is bad i wouldn't upvote it :D , but i said it's not reliable answer meaning that there are chance of 1% that this may not work as expected – Robert Jun 28 '15 at 11:45
  • @robert more than 1% don't use JavaScript :) – rybo111 Jun 28 '15 at 11:47
  • by the way chrome send both values `test.html?check=false&check=true` – Robert Jun 28 '15 at 11:47
  • so the php will replace the first one by the second one is that what you mean ? – Robert Jun 28 '15 at 11:49
  • http://stackoverflow.com/questions/2203430/posting-form-fields-with-same-name-attribute – Robert Jun 28 '15 at 12:02
0

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

$checked_arr = [];
foreach($_POST["checked"] as $checked){
    if($checked == "true"){
        // do what you want to do 
    }
}
Pancake_M0nster
  • 267
  • 5
  • 18
  • 4
    Correct me if I'm wrong but wouldn't `$_POST["checked"]` still just contain the checked checkboxes? – Gurgy Jun 28 '15 at 10:50
0

Another Solution is server-side solution that I think is too fast and easy.

client side: Just use fresh HTML :

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

server side:

make global function like this:

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

then use where you want to read some value that you don't know is set or not. like this:

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

or

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

I hope useful to another one.

Armin.G
  • 381
  • 2
  • 12
0

This is best approach according to my experience

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