Pass parameter with AJAX to PHP when opening a modal

Asked

Viewed 51 times

0

I need to pass the id value of each link to a modal.

You are returning this error in the console: What I need to resolve?

custom.js:5 Uncaught TypeError: $.ajax is not a function
    at HTMLAnchorElement.<anonymous> (custom.js:5)
    at HTMLAnchorElement.dispatch (jquery-3.2.1.slim.min.js:3)
    at HTMLAnchorElement.q.handle (jquery-3.2.1.slim.min.js:3)
(anonymous) @ custom.js:5
dispatch @ jquery-3.2.1.slim.min.js:3
q.handle @ jquery-3.2.1.slim.min.js:3

I’m using the AJAX.

Link

<a href="#" id="<?php echo $row['id_usuario'];  ?>" class="push" data-toggle="modal" data-target="#abrir">click</a>

AJAX

$("a[data-toggle=modal]").click(function() 
{   
    var essay_id = $(this).attr('id');

    $.ajax({
        cache: false,
        type: 'POST',
        url: 'ajax_paciente_id.php',
        data: 'EID='+essay_id,
        success: function(data) 
        {
            $('#abrir').show();
            $('#modalContent').show().html(data);
        }
    });
});

Modal

<div class="modal fade" id="abrir" tabindex="-1" role="dialog" aria-labelledby="abrir" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body modalContent">      


      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

PHP

<?

echo $_POST['essay_id']; 

?>

1 answer

2


Jquery Slim does not have the ajax function, you need to use the full version of Jquery, try with this file:

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  • i switched to the full version, and did not return the error, but is not passing the id value yet.

  • @Wagner changes your line data: 'EID='+essay_id, for data:{EID:essay_id},

  • I did that and it hasn’t passed the id value yet. It looks like it’s not even calling the php file ajax_paciente_id.php

  • puts a console.log() inside the Success and look at the console to see if it is calling or not, or if it is returning error

  • Did not return anything, the problem must be the one that is not calling the file.

  • may also be that it is calling but is returning error, does the following: takes the essay_id and puts a fixed value, and gives a look if it returns

  • strange, does not return anything tbm did so date:{EID:'ssss'}, is the problem not here? $("a[data-toggle=modal]"). click(Function()

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.