Casey
Casey

Reputation: 1981

Make checkbox appear in $_POST array even if unchecked

I have an invoice in a HTML table. You select item name then item lot and the item #. There is a check box at the end to mark if it is sold out.

<input type="checkbox" name="soldout[]">

When I process the form the $_POST array of check boxes obviously don't contain the unchecked boxes. That leads me to problems when matching up the check boxes to the item

foreach($productID as $key=>$id){
    $p = $id;
    if(isset($soldout[$key])){
        $so = true;
    }
}

So if I have three line items and the first and last are marked sold out it processes as the first 2 sold out and the last one not sold out.

I don't think i can do this:

<input type="checkbox" name="soldout[<?php echo $i; ?>]">

because I am adding empty invoice lines with javascript when needed and wouldn't know how to increment the array key.

Any ideas on how to handle this?

Upvotes: 3

Views: 2062

Answers (4)

19cool53
19cool53

Reputation: 1

Java free solution:

&lt;input type="hidden" name="hsoldout[]" value="$value"&gt;
&lt;input type="checkbox" name="soldout[]" value="$value"&gt;<br />

After submit, if a difference between soldout and hsoldout appears, it means that the checkbox was engaged. In fact, you need only one hidden key for each form:

if(array_key_exists($_POST[hsoldout],$i)) { <br/>
    $_POST[soldout][$i] = $_POST[soldout][$i];   // makes sure it is set as a 
    $_POST var <br />
    .... <br />
} 

Upvotes: 0

Michael L.
Michael L.

Reputation: 618

Why not do something like this?

<input type="checkbox" name="soldout[]" value="<?= $productID[0] ?>">
<input type="checkbox" name="soldout[]" value="<?= $productID[1] ?>">

Then in PHP

foreach($productID as $key=>$id) {
    $p = $id;
    if(in_array($id, $soldout) {
        $so = true;
    }
}

Upvotes: 0

Mike
Mike

Reputation: 24383

There is no straight HTML solution for this. If Javascript is an option, you could look there, but if you want a straight HTML solution, an unchecked checkbox isn't going to show up in PHP, but a radio button will.

<input type="radio" name="soldout[<?php echo $i; ?>]" value="true"> Yes
<input type="radio" name="soldout[<?php echo $i; ?>]" value="false"> No

Then in PHP you can access it like

foreach($productID as $key=>$id){
    $p = $id;
    if($soldout[$key] == "true"){
        $so = true;
    }
    else {
        $so = false;
    }
}

Just remember the quotes around "true" because it's a string


Then again, if you are populating the checkboxes server-side you should also presumably know which values should exist and if they are not present in $_POST, assume they are unchecked.

foreach ($availableProductIDs as $id) {
    if(isset($_POST['soldout'][$id]){
        $so = true;
    }
    else {
        $so = false;
    }
}

Upvotes: 1

user1765062
user1765062

Reputation:

When you increase the array using JS just change the name to soldout_new and then increase a variable.

Something like this:

var soldout_new = 0;

function add()
{
'<input type="checkbox" name="soldout_new[' + soldout_new + ']">';

soldout_new += 1;
}

or also you can just set the variable to the id from php:

var soldout = <?php echo $i; ?>;

function add()
{
'<input type="checkbox" name="soldout[' + soldout + ']">';

soldout += 1;
}

Upvotes: 1

Related Questions