1
    Documents     Original            Xerox
-------------------------------------------------
    ABC          <checkbox>          <checkbox>
    DEF          <checkbox>          <checkbox>
    XYZ          <checkbox>          <checkbox>

I have a checkbox structure, as you see it above. But I am not sure, how am I suppose to implement it. I know the working of a normal checkbox, as it has only one checkbox. But in this case, for every document, there are 2 checkbox (original copy and xerox copy)

What I have in mind like, after the submit is clicked, I need to somehow get an array in a form like below

ABC -> [1][1]
DEF -> [0][1]
XYZ -> [1][0]

Where first [] is orginal, and second [] is xerox. Where 1 denotes that it was checked, and 0 denotes unchecked.

I am not getting an idea how should I implement it. This is my code...(It may be horribly wrong, i agree.. but i still i made an attempt.. but couldn't figure it out.)

<table>
  <tr>
  <td>Aadhar</td>
  <td><input type="checkbox" name="document[]" value="1"/></td>
  </tr>
  <tr>
  <td>Pan Card</td>
  <td><input type="checkbox" name="document[]" value="1"/></td>
  </tr>
  <tr>
  <td>Address</td>
  <td><input type="checkbox" name="document[]" value="1"/></td>
  </tr>
  <tr>
  <td>Light Bill</td>
  <td><input type="checkbox" name="document[]" value="1"/></td>
  </tr>
  <tr>
  <td><input type="submit" value="Submit" /></td>
  </tr>
</table>

PHP

$documents = $_POST['document'];
$a = implode($documents);
echo $a;

So far, I am getting 1 (value) correctly.. But I also need the document (key) to which the '1'(value) belongs. Can anyone help me out please.

Badda
  • 1,329
  • 2
  • 15
  • 40
Zhrez Pain
  • 327
  • 1
  • 2
  • 10

1 Answers1

0

Checkboxes only submit WHEN CHECKED, so you'll NEVER get a checkbox submitting a "0" value.

That said, you can know it's NOT checked by simply asking PHP if it was set, like so: isset($_POST['document']['aadhar'][1]) -- if that returns true, it was checked, if not, it was not checked.

One thing to note: You're going to want to specify indexes for your second dimension of the array, so this:

<td><input type="checkbox" name="document[aadhar][]" value="1"/></td>

Becomes this:

<td><input type="checkbox" name="document[aadhar][original]" value="1"/></td>

That way you can track the "column" of which checkbox was selected by asking isset().

Here is a working example: http://myingling.com/random/random/44476276/

Here is the HTML from that example:

<form action='post.php' method='post'>
   <table>
     <tr>
      <td>Aadhar</td>
      <td><input type="checkbox" name="document[aadhar][col1]" value="1"/></td>
      <td><input type="checkbox" name="document[aadhar][col2]" value="1"/></td>
    </tr>
    <tr>
      <td>Address</td>
      <td><input type="checkbox" name="document[light][col1]" value="1"/></td>
      <td><input type="checkbox" name="document[light][col2]" value="1"/></td>
    </tr>
    <tr>
      <td><input type="submit" value="Submit" /></td>
    </tr>
  </table>
</form>

Here is the PHP from the post.php:

<?php
echo "<pre>";
var_dump($_POST);
echo "</pre>";
Bing
  • 3,071
  • 6
  • 42
  • 81
  • Can I use this instead, **** ** – Zhrez Pain Jun 10 '17 at 17:55
  • Absolutely! An easy way to see what you're getting on the other side of the submit is `var_dump($_POST);` – Bing Jun 10 '17 at 17:58
  • actually, its not working [adhaar][].. its not accepting i think. accepting only document[] – Zhrez Pain Jun 10 '17 at 18:00
  • Are you submitting the form with `POST`? Here's a working example: http://myingling.com/random/random/44476276/ You do have a `
    ` element in your HTML, right?
    – Bing Jun 10 '17 at 18:06
  • Yes everything is fine. Used POST. And yes thats exactly what i need :) Can you please edit your answer, and post the code along with it? – Zhrez Pain Jun 10 '17 at 18:12
  • The HTML and PHP, both please :) – Zhrez Pain Jun 10 '17 at 18:14
  • Added the code. If this is the answer, please upvote/accept. If you have other questions, let me know. – Bing Jun 10 '17 at 18:17
  • Hello, are you up? I got it all done. Its going into an array, but I have having problem to display it. – Zhrez Pain Jun 10 '17 at 19:40
  • Sounds like a new question. Create it and link to it and I'll review it. In that new question you'll need (1) to explain what you're trying to do, (2) what code you currently have, (3) anything else you have tried already, (4) any error(s) you're getting. – Bing Jun 10 '17 at 19:46
  • Hello, this is that new link, https://stackoverflow.com/questions/44477622/store-multidimentional-checkbox-in-database – Zhrez Pain Jun 10 '17 at 20:11
  • please have a look – Zhrez Pain Jun 10 '17 at 20:12