I'm wondering how to update my database using a modal and multiple data from another form.

This is how it looks like,
1.png

When you click any checkbox there, the release request button will enable.
2.png

When it was clicked there's a modal will pop up:
3.png

Now, the modal has form in it and my table too. What I would like to do is when they click the release button the reason selected or other reasons will be saved in my database based on what was selected in my table.
4.png

I still don't have the code for the release button coz I'm not sure how to write it exactly.
These are my code so far.

<body>

    <div class="container mt-5">
        <div class="row">
            <div class="col-md-12 mt-4">

            <h1 class="title"> <span>Assignment Mandates</span></h1>
            <?php
                $select_profile = $conn->prepare("SELECT * FROM `users` WHERE id = ?");
                $select_profile->execute([$user_id]);
                $fetch_profile = $select_profile->fetch(PDO::FETCH_ASSOC);
            ?>
            <?php include('message.php');?>    

            <div class="flex-btn mb-2">    
<button type="button" id="rr" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#exampleModal" data-whatever="@getbootstrap">Request to release</button></div>

<!-- Modal for Release Request Form -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Release Request Form</h5>
        <button type="button" class="btn-close" data-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">

        <form action="" method="POST">

          <div class="mb-3">
          <div class="form-floating">
            <select class="form-select" id="floatingSelectGrid" required name="reloption" onchange="if (this.selectedIndex== 11){
                    document.getElementById('other').style.visibility='visible'}else{
                    document.getElementById('other').style.visibility='hidden'}" aria-label="Floating label select example">
               <option selected>-select-</option>
               <option value="">Being served by another therapist</option>
               <option value="">Can't fit into my schedule</option>
               <option value="">Extended student absence</option>
               <option value="">No longer receives counseling services</option>
               <option value="">No longer receives speech services</option>
               <option value="">No longer receives OT services</option>
               <option value="">Not on my caseload</option>
               <option value="">Parent uncooperative</option>
               <option value="">Student no longer attending school</option>
               <option value="">Student transfered to another school</option>
               <option value="">Other</option>
            </select>
            <label for="floatingSelectGrid">Reason</label>
          </div></div>

          <div class="mb-3" id="other" name="other" style="visibility:hidden;">
            <label for="message-text" class="col-form-label">Other Reason:</label>
            <textarea class="form-control" name="other"></textarea>
          </div>



      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary" name="release">Send request</button>
      </div>
      </form>
    </div>
  </div>
</div>

            <form action="code.php" id="form1" method="POST">

                    <table id="mandatesidtable" class="table table-striped display nowrap mt-5" style="width:100%">
                      <thead class="text-light bg-dark text-center">
                        <tr>
                        <th><input class="myCheckBox" type="checkbox" id="select-all"/></th>
                        <th class="text-center align-middle">Provider</th>
                        <th class="text-center align-middle">NYCID</th>
                        <th class="text-center align-middle">Lastname</th>                    
                        <th class="text-center align-middle">Name</th>
                        <th class="text-center align-middle">Ind/Grp</th>
                        <th class="text-center align-middle">Grp size</th>
                        <th class="text-center align-middle">Freq</th>
                        <th class="text-center align-middle">Dur</th>

                        </tr>
                      </thead>
                          <tbody>
                            <?php
                                $query = "SELECT m.* FROM mandates m WHERE '".$fetch_profile['name']."' = m.provider";
                                $query_run = mysqli_query($con, $query);

                                if(mysqli_num_rows($query_run)>0)
                                {
                                   foreach($query_run as $mandates)
                                   {                                     
                                      ?>
                                        <tr>
                                        <td><input class="myCheckBox" type="checkbox" name="check[]" value="<?=$mandates['id'];?>"/></td>
                                          <td class="text-center align-middle"><?=$mandates['provider'];?></td>
                                          <td class="text-center align-middle"><?=$mandates['nycid'];?></td>
                                          <td class="text-center align-middle"><?=$mandates['lastname'];?></td>
                                          <td class="text-center align-middle"><?=$mandates['firstname'];?></td>
                                          <td class="text-center align-middle"><?=$mandates['ig'];?></td>
                                          <td class="text-center align-middle"><?=$mandates['gsize'];?></td>
                                          <td class="text-center align-middle"><?=$mandates['freq'];?></td>
                                          <td class="text-center align-middle"><?=$mandates['dur'];?></td>
                                        </tr>
                                      <?php
                                   } 
                                }
                                else
                                {
                                  echo"<h5?>No Record Found!</h5>";
                                }
                            ?>

                          </tbody>
                    </table>   
                        </div>
                        </form>
                    </div>
                    </div>

<script>
  $(document).ready(function(){
    $("#form1 #select-all").click(function(){
      $("#form1 input[type='checkbox']").prop('checked',this.checked);
    });
  });
</script>

<script>
var checkBoxes = $('tbody .myCheckBox');
checkBoxes.change(function () {
    $('#rr').prop('disabled', checkBoxes.filter(':checked').length < 1);
});
$('tbody .myCheckBox').change();

var checkBox = $('thead .myCheckBox');
checkBox.change(function () {
    $('#rr').prop('disabled', checkBox.filter(':checked').length < 1);
});
$('thead .myCheckBox').change();
</script>

I have checked your code, I think the rest is not very difficult, I was facing the similar problem. Now clicking on "Release Request Form" opens up the modal (I have tested it is okay). Assuming the opened modal displays the data from the database as in the images in your post.(Actually I don't have your database)

Now make a new file with all the relevant php code for inserting the form data into database, right?
Now use jquery that processes your form without refreshing the page. In this way, the data will be inserted into database when user may click on "Send Request" button in modal.

Jquery Code for submitting the form without refresh

<!-- File to add -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
<script src="https://malsup.github.io/jquery.form.js"></script>

<!-- Jquery Code -->

<script>
$(document).ready(function() { 
 $('form').ajaxForm(function() { 
 }); 
});
</script>

I think it would help. Try and tell me if doesn't work.

Hello, thank you for your answer. I basically figured out how to insert the data in my database but the problem I'm facing right now is that, when I select multiple data in my table where I want to put the notes, it only saves the first one selected and not all of what I selected.

I just transferred my modal to the primary form to make it not so complicated. I was successful in doing that.

Here's my updated code:

<body>

    <div class="container mt-5">
        <div class="row">
            <div class="col-md-12 mt-4">

            <h1 class="title"> <span>Assignment Mandates</span></h1>
            <?php
                $select_profile = $conn->prepare("SELECT * FROM `users` WHERE id = ?");
                $select_profile->execute([$user_id]);
                $fetch_profile = $select_profile->fetch(PDO::FETCH_ASSOC);
            ?>
            <?php include('message.php');?>    

            <div class="flex-btn mb-2">    
              <button type="button" id="rr" name="rr" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#exampleModal" data-whatever="@getbootstrap">Request to release
              </button>
            </div>

            <form action="code.php" id="form1" method="POST">

                    <table id="mandatesidtable" class="table table-striped display nowrap mt-5" style="width:100%">
                      <thead class="text-light bg-dark text-center">
                        <tr>
                        <th><input class="myCheckBox" type="checkbox" id="select-all"/></th>
                        <th class="text-center align-middle">Provider</th>
                        <th class="text-center align-middle">NYCID</th>
                        <th class="text-center align-middle">Lastname</th>                    
                        <th class="text-center align-middle">Name</th>
                        <th class="text-center align-middle">Ind/Grp</th>
                        <th class="text-center align-middle">Grp size</th>
                        <th class="text-center align-middle">Freq</th>
                        <th class="text-center align-middle">Dur</th>

                        </tr>
                      </thead>
                          <tbody>
                            <?php
                                $query = "SELECT m.* FROM mandates m WHERE '".$fetch_profile['name']."' = m.provider";
                                $query_run = mysqli_query($con, $query);

                                if(mysqli_num_rows($query_run)>0)
                                {
                                   foreach($query_run as $mandates)
                                   {                                     
                                      ?>
                                        <tr>
                                        <td><input class="myCheckBox" type="checkbox" name="check[]" value="<?=$mandates['id'];?>"/></td>
                                          <td class="text-center align-middle"><?=$mandates['provider'];?></td>
                                          <td class="text-center align-middle"><?=$mandates['nycid'];?></td>
                                          <td class="text-center align-middle"><?=$mandates['lastname'];?></td>
                                          <td class="text-center align-middle"><?=$mandates['firstname'];?></td>
                                          <td class="text-center align-middle"><?=$mandates['ig'];?></td>
                                          <td class="text-center align-middle"><?=$mandates['gsize'];?></td>
                                          <td class="text-center align-middle"><?=$mandates['freq'];?></td>
                                          <td class="text-center align-middle"><?=$mandates['dur'];?></td>

</td>
                                        </tr>
                                      <?php
                                   } 
                                }
                                else
                                {
                                  echo"<h5?>No Record Found!</h5>";

                                }
                            ?>

                          </tbody>
                    </table>   
                        </div>

                        <!-- Modal for Release Request Form -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Release Request Form</h5>
        <button type="button" class="btn-close" data-dismiss="modal" aria-label="Close"></button>
      </div>

      <div class="modal-body">
          <div class="mb-3">
          <div class="form-floating">
            <select class="form-select" id="reloption" required name="reloption" onchange="if (this.selectedIndex== 11){
                    document.getElementById('other').style.visibility='visible'}else{
                    document.getElementById('other').style.visibility='hidden'}" aria-label="Floating label select example">
               <option selected>-select-</option>
               <option value="Being served by another therapist">Being served by another therapist</option>
               <option value="Can't fit into my schedule">Can't fit into my schedule</option>
               <option value="Extended student absence">Extended student absence</option>
               <option value="No longer receives counseling services">No longer receives counseling services</option>
               <option value="No longer receives speech services">No longer receives speech services</option>
               <option value="No longer receives OT services">No longer receives OT services</option>
               <option value="Not on my caseload">Not on my caseload</option>
               <option value="Parent uncooperative">Parent uncooperative</option>
               <option value="Student no longer attending school">Student no longer attending school</option>
               <option value="Student transfered to another school">Student transfered to another school</option>
               <option value="Other">Other</option>
            </select>
            <label for="reloption">Reason</label>
          </div></div>

          <div class="mb-3" id="other" name="other" style="visibility:hidden;">
            <label for="message-text" class="col-form-label">Other Reason:</label>
            <textarea class="form-control" name="other"></textarea>
          </div>

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary" id="release" name="release">Send request</button>
      </div>

    </div>
  </div>
</div>  

                        </form>

                    </div>
                    </div>

Then, my release button code is:

if(isset($_POST['release']))
{

    $reason = mysqli_real_escape_string($con,$_POST['reloption']);
    $other = mysqli_real_escape_string($con,$_POST['other']);

    $allid = $_POST['check'];
    $xtract = implode(',',$allid);

 $query =  "UPDATE mandates SET id='$xtract', enable = 1, releaseNote='$reason' WHERE id ='$xtract'";

    $query_run = mysqli_query($con,$query);
    if($query)
    {   
        $_SESSION['message'] = "Request has been sent!";
        header("Location: umandates.php");
        exit(0);
    }
    else
    {
        $_SESSION['message'] = "Request not sent!";
        header("Location: umandates.php");
        exit(0);
    }

}

This code is working but the releaseNote is being added on the first selected data.I was searching and based on what I've read while I'm looking for answers, they said that update function in mysql online updates one row and not gonna update multiple rows in database. I'm not sure what to use now and I'm stuck in this problem but I now it's possible, I just dont know how.

Hello, thank you for your answer.

Welcome

I basically figured out how to insert the data in my database

So now parts of script like selection of any/multiple records from starting page table, passing their ids in modal, re-sending them in code.php are working well. Okay?
The problem is only one record is updating in database and not all selected rows are updating.

Then, my release button code is:

This may be "Send Request" button code. (According to prior codes)

$other = mysqli_real_escape_string($con,$_POST['other']);

$other is not being used in this page.

You can do this through the use of loop. If you use for loop then you will need total number of checkboxes checked.

For example:

You need to get the count of checkboxes clicked at starting page.

    $('#someid').click(function () {
                  var check = $('#otherid').find('input.checkbox:checked"]').length;
                  $(modal).append("<input name='chk_count' value='"+check+"'  type='hidden' />")
            return false;
            }):

(or whatever strategy you use for it)

Now you have all ids of checkboxes checked at first page and you have count of checkboxes checked.

Now wrap the update query in a for loop and give the ending condition up to chk_count.

Here I am writing an example code using foreach

if(isset($_POST['release']))
{

    $reason = mysqli_real_escape_string($con,$_POST['reloption']);
    $other = mysqli_real_escape_string($con,$_POST['other']);
    $allid = $_POST['check'];
    $xtract = implode(',',$allid);


// Start a loop and get all checkboxex ids as $values

foreach ($_POST as $key => $value) {

$query =  "UPDATE mandates SET id='$value', enable = 1";


    $query_run = mysqli_query($con,$query);
    if($query)
    {   
    // Do something on success
    }
    else
    {
    // Do something
    }

}
}

I thought may be this would be of some help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.