0

I am trying to create a form where a client can check off their tasks that automatically saves (AJAX). When the client checks the form, the value of the task's ID is sent to the PHP page. However, if the client unchecks it, even with the exact same code, the value disappears and I don't understand why. Here is my code:

index.php

<form method="post" action="this.form.submit();" name="form1">
  <div class="client-list">
    <div class="checkbox-list">

       <?php if(empty($clientTask['status'])): ?>
          <input type="checkbox" id="task0" name="task0" onclick="function1();" value="1">
       <?php else: ?>
          <input type="checkbox" id="task0" name="task0" onclick="function1();" value="1" checked>
       <?php endif; ?>
       <label for="task0">Task 0 Name</label>

       <?php if(empty($clientTask['status'])): ?>
          <input type="checkbox" id="task1" name="task1" onclick="function1();" value="2">
       <?php else: ?>
          <input type="checkbox" id="task1" name="task1" onclick="function1();" value="2" checked>
       <?php endif; ?>
       <label for="task1">Task 1 Name</label>

       <?php if(empty($clientTask['status'])): ?>
          <input type="checkbox" id="task2" name="task2" onclick="function1();" value="3">
       <?php else: ?>
          <input type="checkbox" id="task2" name="task2" onclick="function1();" value="3" checked>
       <?php endif; ?>
       <label for="task2">Task 2 Name</label>
    </div>
  </div>
</form>

script.js

<script>
    function function1() {
    var data = $("[name='form1']").serialize()
    $.ajax({
        url: "client-tasks.php",
        type: "POST",
        async: true,
        cache: false,
        data: data, 
        success: function(data){ 
            alert(data) 
        }
    });
}
</script>

I tried 3 different ways to see what works and what doesn't in client-tasks.php:

  1. $_POST['task0'] works both ways when the id is hard-coded.
  2. $_POST['task1'] only works in the if statement, but the value disappears in the else statement. It doesn't matter if the status is set to 1 or 0.
  3. The whole code with $_POST['task2'] will work even though the $task2 is an empty string.

From what I found, the else statement never works unless it is hard-coded. So, I need to find a way to get it working.

<?php
   include("../../path.php");
   include(ROOT_PATH . "/app/database/config.php");
   

    if (isset($_POST['task0'])) {
        $sql1="UPDATE client_tasks SET status = 1 WHERE id = 1";        
    } else {
        $sql1="UPDATE client_tasks SET status = 0 WHERE id = 1";
    }
    $result=$conn->query($sql1);
  
    if (isset($_POST['task1'])) {
        $sql2="UPDATE client_tasks SET status = 1 WHERE id = " . $_POST['task1'] . "";
    } else {
        $sql2="UPDATE client_tasks SET status = 0 WHERE id = " . $_POST['task1'] . "";
    }
    $result=$conn->query($sql2);

    $task2 = $_POST['task2'];
    if (isset(task2)) {
        $sql3="UPDATE client_tasks SET status = 1 WHERE id = " . $task2 . "";
    } else {
        $sql3="UPDATE client_tasks SET status = 0 WHERE id = '$task2'";
    }
    $result=$conn->query($sql3);

When it works When it doesn't work (empty string helps execute code)

I would really appreciate any help or advice, thank you!

10
  • Another really old trick is to add an hidden input before every checkbox with the un-checked value... <input type="hidden" name="task0" value="0" /> Commented Nov 27, 2023 at 23:51
  • I'm fairly certain your issue is that an unchecked checkbox does not send any value to a server, whereas a checked checkbox does. An unchecked checkbox is also not serialized, and nothing is received by PHP. Commented Nov 27, 2023 at 23:52
  • @Phil I was just about to say the exact same thing. Commented Nov 27, 2023 at 23:53
  • 1
    Please also familiarise yourself with prepared statements. This MySQLi tutorial is highly recommended (or the PDO equivalent if that's what you're using) Commented Nov 27, 2023 at 23:56
  • You could cut down on a lot of boilerplate by simply using $checked = !empty($clientTask['status']) ? ' checked' : ''; and <input ... <?= $checked ?>> Commented Nov 27, 2023 at 23:59

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.