0

I'm trying to use checkboxes to update information in a MySQL database.

The 'Sent' column is a Boolean.

Is there a way to put a checkbox inside my php code,tbody so, instead of <td>".$infoItems['Sent']."</td> there's a checkbox instead of zeroes.

<table class="scroll">
  <thead>
    <tr>
      <th>Schools Name</th>
      <th>Schools Email</th>
      <th>Sent</th>
    </tr>
  </thead>

  <tbody>
      <?php

      $execItems = $conn->query("SELECT Name,SchoolMail,Sent FROM Schools");

      while($infoItems = $execItems->fetch_array())
      {
        echo    "
                <tr>
                    <td>".$infoItems['Name']."</td>
                    <td>".$infoItems['SchoolMail']."</td>

                    <td>".$infoItems['Sent']."</td>
                    /* ^Insert a checkbox here instead of this ^ */
                </tr>
            ";
        }
    ?>
    </tbody>
</table>
</body>

</html>
3
  • what checkboxes and what update? Didn't you try anything? Commented Jan 31, 2018 at 16:50
  • Is this two different questions... ? Commented Jan 31, 2018 at 17:00
  • <input type=checkbox...> and so on? Commented Jan 31, 2018 at 17:01

1 Answer 1

1

You could try something like this

echo '<tr>
    <td>'.$infoItems['Name'].'</td>
    <td>'.$infoItems['SchoolMail'].'</td>
    <td>
        <input type="checkbox"'.($infoItems['Sent']?' checked':'').'
               data-mail="'.$infoItems['SchoolMail'].'"
               data-name="'.$infoItems['Name'].'"
               onchange="ajax_call_function(this)"
               />
    </td>
</tr>' ;

It will call a javascript function :

<script type="text/javascript">
    function ajax_call_function(element) {
        console.log(element.getAttribute("data-name"));
        console.log(element.getAttribute("data-mail"));

        // make your AJAX call to a server-side script 
        // that updates your database with these informations.
        //
        // OR
        //
        // Update some hidden fields in a form and submit it. 

    }
</script>
Sign up to request clarification or add additional context in comments.

7 Comments

Yes, but OP also talks about updating information on the database.
Thanks for the input! That helped with the checkboxes, but how do I make it so when I insert the SchoolMail and send the email, the checkbox gets updated?
Tip: Instead of fighting the quotes, either omit them, as you can for most HTML tag attributes with single word values, or use the other kind. For example: <input type=checkbox> or <input type='checkbox'> both work.
@Felmorne I've updated my anwser... But there are so many way to do this.
@Syscall Sorry if I'm being intrusive but is there a way that we can communicate directly, if you're available of course
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.