samedi 10 juin 2017

PHP - getting key and value using checkbox

    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 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[aadhar][]" value="1"/></td>
  </tr>
  <tr>
  <td>Pan Card</td>
  <td><input type="checkbox" name="document[pan][]" value="1"/></td>
  </tr>
  <tr>
  <td>Address</td>
  <td><input type="checkbox" name="document[address][]" value="1"/></td>
  </tr>
  <tr>
  <td>Light Bill</td>
  <td><input type="checkbox" name="document[lightbill][]" 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.




Aucun commentaire:

Enregistrer un commentaire