Javascript not getting the values of the variables in AJAX MODAL form

Viewed 108

I am just new to AJAX and following some tutorial on youtube but I'm stuck on some things. So I just changed some things here and adjusted it based on my understanding.

This button will get the project ID that the user wants to approve on the table:

<button type="submit" name="approve" id="<?php echo $displayapprovaldetails['projectid'];?>" class="btn btn-primary btn-sm approve_data">Approve</button>

Now, I have this modal form that will ask for remarks first before approving and updating the records connected to the projectid:

<div class="modal fade" id="approve_data_Modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title font-weight-bold text-primary" id="exampleModalLabel">Approve</h5>
          <button class="close" type="button" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">×</span>
          </button>
        </div>
        <form method="post">
        <div class="modal-body">
            <label>Remarks: </label>
            <textarea name="remarks" id="remarks" rows="5" class="form-control"></textarea>
        </div>
        <div class="modal-footer">
          <button class="btn btn-secondary" type="button" data-dismiss="modal">Close</button>
          <button class="btn btn-primary" type="submit" name="approve" id="approve">Confirm</button>
          </form>
        </div>
      </div>
    </div>

I changed the script to get the variables for both on nested functions but it is still not working. I'm not sure if my script is also correct.

Script:

<script>
$(document).ready(function(){

  $(document).on('click', '.approve_data', function(){
     var projectid = $(this).attr("projectid");

     $('#approve_data_Modal').modal('show');
     $(document).on('click', '#approve', function(){
      var remarks = $("#remarks").val();
      var projectid = $projectid;

      if(remarks=="")
      {
        $("#lblRemarks").html("What do you want to say?");
      }

     $.ajax({
        url:"include/approve.php",
        type:"post",
        data:{projectid:projectid, remarks:remarks},
        success:function(data){
          $("#approve_data_Modal").modal('hide');
          }
        });
     });
  });
});
</script>

Can someone help me pinpoint what was wrong with my script and how I could correct it to get the two variables ($projectid and $remarks)?

Thanks.

2 Answers

instead of having a button =

<button type="submit" name="approve" id="<?php echo $displayapprovaldetails['projectid'];?>" data-toggle="modal" data-target="#approve_data_Modal" class="btn btn-primary btn-sm">Approve</button>

Use Input =

<input type='text' id="project_id" value="<?=$displayapprovaldetails['projectid']?>" >

you can also use button for submition but when it comes to sending id to other file i recommend you this

Your HTML :

<div class="modal fade" id="approve_data_Modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title font-weight-bold text-primary" id="exampleModalLabel">Approve</h5>
          <button class="close" type="button" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">×</span>
          </button>
        </div>
        <form method="post" id="approve_form">
        <div class="modal-body">
            <label>Remarks: </label>
            <textarea name="remarks" id="remarks" rows="5" class="form-control"></textarea>
        </div>
        <div class="modal-footer">
          <button class="btn btn-secondary" type="button" data-dismiss="modal">Close</button>
          <button class="btn btn-primary" type="submit" name="approve" id="approve">Confirm</button>
          </form>
        </div>
      </div>
    </div>

AND here's your script file :

    <script>
    $(document).ready(function(){
      $('#approve').on('submit', function(event){
            event.preventDefault();
 var id = $("#project_id").val();
            if($('#remarks').val() == "")
            {
              alert("Remark is required.");
            }
            else
            {
              $.ajax({
                url:"include/approve.php",
                method:"post",
                data:{id:id},
                success: function(data)
                {
                  $('#approve_form')[0].reset();
                  $('#approve_data_Modal').modal('hide');
                }
              });
            }
        });
    });
    </script>

I have this resolved already. It is just calling the function and declaring variables.

Related