3

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?

2
  • 1
    Does a newly added row (through JavaScript) have a productID as well? If so, and if they are unique per row, I'd use the productID as a key to soldout[], i.e. soldout[<?php echo $productId; ?>]. Obviously, with JavaScript, you'd have to analyze the newly created row for its productID then, and add is as a key to the checkbox name. Commented Mar 1, 2013 at 2:28
  • No, they aren't necessarily unique. Commented Mar 1, 2013 at 2:59

4 Answers 4

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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;
    }
}

Comments

0

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;
    }
}

Comments

0

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 />
} 

Comments

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.