-2

I've got a problem. I don't know how to put the value of my checkbox in my MySQL database with PHP. So :

<input type="checkbox" name="checkiard" value="IARD">

How to put the value "IARD" in my database ONLY if the checkbox is check ? Thank you guys.

  • after posting form you can access the value of this checkobx using $_POST['checkiard'] and can insert it into database – usman ikram Oct 23 '17 at 20:22
  • You can do this in two ways. One is make an ajax call after check the input and the other is submit the form by check the input, then use $_POST['checkiard'] to get the value of this input. – fatih Oct 23 '17 at 20:28
  • Ok, but there is more : ` IARD
    vie place` I've got a lot of checkbox, I want just insert the value **only** if a checkbox is check... And that for all my checkbox !
    – Alessio Cammarata Oct 23 '17 at 20:30
  • Possible duplicate of [How do I insert multiple checkbox values into a table?](https://stackoverflow.com/questions/20176673/how-do-i-insert-multiple-checkbox-values-into-a-table) – Shanteshwar Inde Jan 11 '19 at 09:14

1 Answers1

0

You can access the value of checkbox as any other input types. Its simply

  if(isset($_POST['checkiard'])) {
  $optionArray = implode(",", $_POST["checkiard"]);
  }

Try this.

If you have more than one checkbox then you markup should be something like this

<input type="checkbox" name="checkiard[]" value="IARD-1">
<input type="checkbox" name="checkiard[]" value="IARD-2">
<input type="checkbox" name="checkiard[]" value="IARD-3">

you must keep the name same for all the checkbox.

Rajan Benipuri
  • 1,772
  • 1
  • 9
  • 21