0

How to set a = 0 value for each checkbox unchecked

Error:

Warning: array_combine() [function.array-combine]: Both parameters should have an equal number of elements in

PHP:


$combined = array_combine($_POST['name'], $_POST['day']);

foreach($combined as $name => $value) {
     echo $name.' => '.$value;
}

HTML:

  <form method="post" action="result.php">
  <p>
        <input type="text" name="name[]" value="seth" />
        <input type="checkbox" name="day[]" value="7" />
  </p>

  <p>
        <input type="text" name="name[]" value="jack" />
        <input type="checkbox" name="day[]" value="4" />
  </p>

  <p>
        <input type="text" name="name[]" value="david" />
        <input type="checkbox" name="day[]" value="3" />
  </p>

 </form>

PS:
I won't use:

        <select name="day[]" size="1">
           <option value="0"> Non </option>
           <option value="1"> Yes </option>
        </select>

3 Answers 3

2

Your problem is that unchecked checkboxes aren't even sent to the server, so as it is your 2 $_POST arrays won't line up unless they are all checked. You should consider using radios and setting the value of the key in name attribute.

ie do this for each input:

<form>
<p>
    <input type="text" name="name[3]" value="david" />
    <input type="radio" name="day[3]" value="yes" /> <label>Yes</label>
    <input type="radio" name="day[3]" value="no" /><label>No</label>
</p>
</form>

Then do print_r($_POST) and you will see your arrays line up. Another way is to do a hidden input (with exactly the same name="" and it will act as a default value.

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

6 Comments

What I am saying is that if a user doesn't tick a checkbox and submit the form, it won't appear in your $_POST array.
@Naiimim What Dunhamzzz means is if a checkbox is unchecked, its not included in the $_POST request sent to the server, meaning instead of sending a false value indicating the check-box is unchecked, the browser sends nothing.
@Dunhamzzz when all checkbox are checkked I get the values. But when one is not checked is get the error above
@Saad Imran: Thanks I understand. How can I set a 0 value for this checkbox unsent - I tried to use isset() ...
@Naamim that's what I suggest you use Radios, or you can use hidden inputs, read the last line of my answer.
|
1

Here you go. Hope this works for you. It's a bit more work then what you were hoping for probably.

result.php

<?PHP

$days = array();
$namecount = count($_POST['name']);
for($i = 0; $i < $namecount; $i++){
    $days[$i] = isset($_POST['day'][$i]) ? $_POST['day'][$i] : 0;
}

$arr = array_combine($_POST['name'], $days);
print_r($arr);

?>

form.html

<form method="post" action="result.php">
    <p>
        <input type="text" name="name[]" value="seth" />
        <input type="checkbox" name="day[0]" value="7" />
    </p>

    <p>
        <input type="text" name="name[]" value="jack" />
        <input type="checkbox" name="day[1]" value="4" />
    </p>

    <p>
        <input type="text" name="name[]" value="david" />
        <input type="checkbox" name="day[2]" value="3" />
    </p>
    <input type='submit' value='Submit' />
</form>

Comments

0

I can think of only 3 ways:

  1. Move to <input type=radio>s with the same name, this will give you extra control over the content that is sent through. (Given in a previous answer)
  2. Add another input, a hidden one, <input type=hidden>, which has the same name as your checkbox, if the checkbox is not selected, the hidden input's value will be sent. (Given in a previous answer)
  3. Use JavaScript to submit your form, and loop through the checkboxes to manually test each one for selected state, and send that along with the POST request.

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.