Jump to content

I need to open a Bootstrap modal and pass a php var using jquery ajax


rdkurth

Recommended Posts

I need help with adding some ajax to my jquery script to pass a php var to a modal. I can open the modal with the bit of jquery below but I am at a lose on how to add any ajax to this jquery script to take the $id from the link to the modal. 

 

 

The jquery I am opening the modal with

$(document).ready(function(){

    $(".launch-modal").click(function(){

        $("#editexpenses").modal({

            keyboard: false

        });

    }); 

});

I am opening the modal with this link to the jquery

<a id="<?php $id ?>" title="Edit this item" class="launch-modal" href="#editexpenses">edit</a>

The modal

<div class="modal fade" id="editexpenses" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
      
      
      
        
     
     	<?php $id = $_POST['expID'];
                echo 'id' . $id;
                
                
                var_dump($_POST); 
                 ?>	 
     
     
     
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


Why would you need any Ajax for this?

 

$(".launch-modal").click(function() {
  var id = $(this).attr('id');
});

I just tried your code and the problem is it just shows the last id number in a list of records. The leak that I am using to go to the modal is a list of item and the link grabs the id number that is selected and opens the modal to shows all of the fields for that id number so it can be edited.Thats way I need to do it with ajax

This is what I have tried but it does not pass the id it does open the modal

$(document).ready(function(){

    $(".btn").click(function(){

        $("#myModal").modal('show');
        
        var id = $(‘id’).val();
        $.ajax({
        type: "POST",
        url: expenses.php,
        data: "id=" + id,
        success : function(data){$('#id').val(data)},
        dataType: html
});
    });

});
  • 5 months later...

Hi rdkurth,

I'm having the exact same problem a few months later (I can pass the variable but it's always the last in the list of results rather than the id for each result). I wonder if you found out how to do this? Any help much appreciated!

 

cheers

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.