-1

i have this kind of table where I have to store if checkbox is ticked or not because I use them like boolean

name isEmployed isSoloParent
john 1 0

I have to make the form dynamic, therefore people can add many rows as they want to fill up many at a single time.

There's no problem doing it for text inputs which can be easily done with <input type="text" name="name[]"> and some standard PHP $name=$_POST['name']; however I can't seem to apply the same concept to checkboxes. it doesnt receive unchecked values (NULL in sql) nor get read by php ISSET.

  • would you post your code? because It seems, `radio` would suitable for you case instead of `checkbox` – sukalogika Oct 18 '21 at 04:56
  • I have 7 checkboxes actually. i think radio would be quite overwhelming, no? i tried to handle the code in javascript/jquery but it didnt work on my end (i already asked a previous question about it) so now im seeking for php. however if this is truly breath taking, I will definitely make the switch to radio – inNeedOfHelp Oct 18 '21 at 04:59
  • Does this answer your question? [POST unchecked HTML checkboxes](https://stackoverflow.com/questions/1809494/post-unchecked-html-checkboxes) – sukalogika Oct 18 '21 at 05:12
  • unfortunately it didnt. the top answer there works for an already recognized amount of checkboxes. mine is dynamic and I tried the other solutions from there but i havent found a working one – inNeedOfHelp Oct 18 '21 at 05:16
  • From you mentioning a dynamic form, and from your general description of the problem, it looks like PHP may be a wrong tool here. PHP does not offer front-end flexibility. It's accomplished with JS. If you're adamant PHP is the way here, you'll have to add more context to the question. – BNazaruk Oct 18 '21 at 22:00

1 Answers1

0

In php a checkbox is present (and contains the value) in the $_REQUEST (or $_GET or $_POST) if it is checked. Otherwise it is not present. So to get whether your checkbox is checked or not you can use the following:

$is_checked = isset($_POST["checkbox_name"]);

To print the checkbox value use the checked html attribute. If it is given, the checkbox will be ticked.

print(
    "<input ".
        "type='checkbox' ".
        "name='checkbox_name' ".
        (isset($_POST["checkbox_name"]) ? "checked='checked' " : "").
    " />"
);

To work with a dynamic number of elements I use arrays in the $_POST (or $_REQUEST). You can do this by adding [ and ] to the end of the name attributes of your inputs (e.g. like in https://www.php.net/manual/reserved.variables.post.php#87650). An alternative is to use suffixes or prefixes (e.g. checkbox_name_1) but in my opinion this is just a little bit more work to do.

To add new inputs your javascript may look like the following snippet. The name is always the same, just a [<index>] is appended to tell php to handle this as an array. The <index> is the numeric index to use. This is important, otherwise you cannot use isset($_POST[...][$index]) because dynamic indices will "fill up" your checkbox values.

$("#add").on("click", function(){
  let i = $("#table tr").length;
  
  $("#table").append(
    "<tr>" + 
      "<td>Name <input type='text' name='name[" + i + "]' /></td>" + 
      "<td>Is solo parent <input type='checkbox' name='is_solo_parent[" + i + "]' /></td>" + 
      "<td>Is employeed <input type='checkbox' name='is_employeed[" + i + "]' /></td>" + 
    "</tr>"
  );
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="add">Add</button>
<table id="table">
</table>

In php then you can iterate over the requested variables and check if they exist:

foreach($_POST["name"] as $i => $name){
    // the "name" exists, if no name is given, no entry in the database is wanted

    $is_solo_parent = isset($_POST["is_solo_parent"][$i]); // true or false
    $employeed = isset($_POST["employeed"][$i]); // true or false

    $database->insert($name, $is_solo_parent, $employeed);
}

$database->insert() is of course a function that fills the data into your database. Make sure to escape user input to prevent script injection and best use prepared statements to prevent sql injection.

miile7
  • 2,547
  • 3
  • 23
  • 38
  • but I have to get the unchecked values too, i need them to get stored as 0 but PHP cant read them – inNeedOfHelp Oct 19 '21 at 16:02
  • You have the unchecked checkboxes. `isset($_POST["checkbox_name"])` returns `true` if the checkbox is there and checked and returns **`false` if the checkbox is not checked** (because it's not there). If you have three rows with name and employeed, and for row 1 and row 3 there is employeed, and for row 2 it's missing, you know that the employeed was unchecked for row 2. That's the idea. That's why you need to fix the `i` in javascript. – miile7 Oct 20 '21 at 06:50